Introduction to Standardization

Standardization is a crucial process in data preprocessing that adjusts the features of different scales to have the same scale. This means converting the values to have a mean of zero and a standard deviation of one. It's particularly important for algorithms that rely on the scale of the data, such as logistic regression and K-means clustering, ensuring that all features contribute equally to the model performance.

Imagine you're comparing the weight and height of individuals where weight is in kilograms and height is in centimeters. If not standardized, the model might wrongly consider height more significant due to its larger range of values.

Selecting Numerical Features for Standardization

While standardizing features can be beneficial, especially for algorithms sensitive to the scale of data, it's not a universal requirement. The decision to standardize should be based on the nature of your data and the specific algorithms you plan to use.

In our case, we will standardize all numeric features in the Diamonds dataset for demonstrative purposes: carat, depth, table, x, y, z, and price.

Here’s how we select these numerical features:

# Selecting numerical features for standardization
num_features = ['carat', 'depth', 'table', 'x', 'y', 'z', 'price']
Applying StandardScaler to the Data

The StandardScaler from the sklearn.preprocessing module standardizes the features by removing the mean and scaling to unit variance. It ensures that each feature contributes equally to the model.

First, import the StandardScaler:

from sklearn.preprocessing import StandardScaler

Initialize and fit the StandardScaler to the selected numerical features, then transform the data:

# Initializing StandardScaler
scaler = StandardScaler()

# Fitting and transforming the numerical features
diamonds[num_features] = scaler.fit_transform(diamonds[num_features])

The fit_transform method computes the mean and standard deviation for scaling, fitting it to our data, then transforms our data based on these statistics.

Verifying the Standardized Data

Finally, we’ll verify the standardized data to ensure it has been transformed correctly. This step is crucial to confirm that our preprocessing step was successful.

We print the first few rows of the standardized dataset:

# Display the first few rows to verify standardization
print(diamonds.head())

The output will be:

      carat      cut color clarity     depth     table     price         x  \
0 -1.198168    Ideal     E     SI2 -0.174092 -1.099672 -0.904095 -1.587837   
1 -1.240361  Premium     E     SI1 -1.360738  1.585529 -0.904095 -1.641325   
2 -1.198168     Good     E     VS1 -3.385019  3.375663 -0.903844 -1.498691   
3 -1.071587  Premium     I     VS2  0.454133  0.242928 -0.902090 -1.364971   
4 -1.029394     Good     J     SI2  1.082358  0.242928 -0.901839 -1.240167   

          y         z  
0 -1.536196 -1.571129  
1 -1.658774 -1.741175  
2 -1.457395 -1.741175  
3 -1.317305 -1.287720  
4 -1.212238 -1.117674  

By verifying this, you can see that the numerical values now have their mean close to zero and the standard deviation close to one, effectively standardizing the dataset.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal