Understanding Standardization in Machine Learning: A Comprehensive Guide
Machine learning involves training models to learn patterns and relationships from data in order to make predictions. However, raw data often contains features with varying scales and distributions that can negatively impact model performance if left untreated. One of the most important data preprocessing techniques to address this issue is known as standardization.
In this in-depth guide, we‘ll dive into what exactly standardization is, why it‘s so important in machine learning, how to implement it in Python, and best practices to follow. After reading, you‘ll have a solid understanding of standardization and be able to apply it effectively in your own machine learning projects.
What is Standardization?
Standardization is a scaling technique that transforms features to have a mean of 0 and a standard deviation of 1. In other words, it centers the feature values around 0 and scales them to have unit variance. This is achieved by subtracting the mean value of each feature and then dividing by the standard deviation of the feature:
z = (x – μ) / σ
where z is the standardized value, x is the original feature value, μ is the mean of the feature values, and σ is the standard deviation of the feature values.
For example, let‘s say we have a dataset with a feature called "age" that has the following values:
[25, 30, 35, 40, 45]The mean of these values is 35 and the standard deviation is 7.91. To standardize the "age" feature, we would perform the following calculation on each value:
z = (x – 35) / 7.91
So the standardized "age" values would be:
[-1.26, -0.63, 0.0, 0.63, 1.26]As you can see, the standardized values are centered around a mean of 0 and have a standard deviation of 1. Visually, if you were to plot the distribution of the original "age" values compared to the standardized values, it would look something like this:
[Image showing original age distribution vs standardized distribution]The original distribution is shifted to have a mean of 35 while the standardized distribution is centered at 0. The shape and spread of the distribution is maintained after standardization, just on a different scale.
Why is Standardization Important?
Many machine learning algorithms perform much better when numerical input features are scaled to a standard range. There are a few main reasons for this:
-
Machine learning algorithms typically use Euclidean distance between data points in their computations. Features on larger scales can have a much larger effect on the distance metric than features on smaller scales, even if the smaller scale features are more informative for the prediction task. Standardizing the features to be on similar scales prevents certain features from dominating the algorithm and allows it to learn from all features equally.
-
Gradient descent converges much faster with standardized features. Gradient descent is an optimization algorithm used to minimize the loss function by iteratively adjusting the model‘s parameters. It converges faster when features are on similar scales because parameter updates are proportional to the gradient of the loss function with respect to the parameters. Features on larger scales can cause the gradient to be very large in some dimensions and very small in others, leading to slow convergence. Standardizing the features helps balance the scale of the gradients and allows gradient descent to take more direct paths to the optimum parameters.
-
Many regularization techniques assume that features are centered around 0 with unit variance. Regularization adds a penalty term to the loss function to constrain the model parameters and prevent overfitting. Commonly used L1 and L2 regularization techniques are most effective when features are standardized.
-
Standardization helps the model learn a proper weighting of features. With features on different scales, the model may overweight larger scale features even if they are less informative. Standardizing the features puts them on equal footing and allows the model to learn the proper relative importance of each one for the prediction task.
In general, when using machine learning algorithms that are distance-based or employ gradient descent optimization, it‘s a good idea to standardize your data first. Some specific algorithms where standardization is often recommended include:
- Linear regression
- Logistic regression
- Support vector machines
- Neural networks
- K-nearest neighbors
- K-means clustering
- Principal component analysis (PCA)
There are some algorithms that are scale-invariant and don‘t require standardization of input features, such as decision trees, random forests, and Naive Bayes. However, it never hurts to standardize your data and can often still improve results.
Implementing Standardization in Python
Standardizing your data is straightforward to do in Python using the scikit-learn library. First you‘ll need to install scikit-learn if you haven‘t already:
pip install scikit-learn
Then you can import the StandardScaler class:
from sklearn.preprocessing import StandardScaler
To fit the StandardScaler to your data and transform it to be standardized:
scaler = StandardScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
It‘s important that you only fit the scaler to the training data to avoid leakage of information from the test set. But you use the same scaler fit on the training set to then transform both the training and test data.
Here‘s a full example on a sample dataset:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Load data
data = pd.read_csv(‘data.csv‘)
# Split into features and target
X = data.drop(‘target‘, axis=1)
y = data[‘target‘]
# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Initialize standard scaler
scaler = StandardScaler()
# Fit to training data
scaler.fit(X_train)
# Transform training and test data
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
After this, X_train_scaled and X_test_scaled will contain your standardized features that you can use to train and evaluate your machine learning models.
Effect of Standardization on Machine Learning Algorithms
To see the benefit of standardization, let‘s look at its effect on a couple different machine learning algorithms. We‘ll use the sample data and code from above and compare model performance with and without standardization.
First, let‘s try logistic regression:
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# Fit logistic regression model on original data
lr = LogisticRegression()
lr.fit(X_train, y_train)
lr_pred = lr.predict(X_test)
print(f‘Logistic regression accuracy without standardization: {accuracy_score(y_test, lr_pred):.3f}‘)
# Fit logistic regression model on standardized data
lr_std = LogisticRegression()
lr_std.fit(X_train_scaled, y_train)
lr_std_pred = lr_std.predict(X_test_scaled)
print(f‘Logistic regression accuracy with standardization: {accuracy_score(y_test, lr_std_pred):.3f}‘)
Output:
Logistic regression accuracy without standardization: 0.753
Logistic regression accuracy with standardization: 0.792
We see that standardizing the features improved the accuracy of logistic regression from 75.3% to 79.2% on this dataset.
Now let‘s try the same thing with a decision tree classifier:
from sklearn.tree import DecisionTreeClassifier
# Fit decision tree on original data
dt = DecisionTreeClassifier(random_state=42)
dt.fit(X_train, y_train)
dt_pred = dt.predict(X_test)
print(f‘Decision tree accuracy without standardization: {accuracy_score(y_test, dt_pred):.3f}‘)
# Fit decision tree on standardized data
dt_std = DecisionTreeClassifier(random_state=42)
dt_std.fit(X_train_scaled, y_train)
dt_std_pred = dt_std.predict(X_test_scaled)
print(f‘Decision tree accuracy with standardization: {accuracy_score(y_test, dt_std_pred):.3f}‘)
Output:
Decision tree accuracy without standardization: 0.814
Decision tree accuracy with standardization: 0.814
For the decision tree, we see that standardization had no effect on the model‘s accuracy. This is expected since decision trees are scale-invariant and don‘t depend on the scaling of the features.
These examples demonstrate how standardization can significantly improve models like logistic regression that are sensitive to feature scaling, while not affecting models like decision trees that are scale-invariant. To get the most out of your machine learning models, it‘s worth trying out standardization, especially if using algorithms sensitive to feature scales.
Standardization vs Normalization
Standardization and normalization are both techniques to transform features to be on similar scales, but they work in slightly different ways. As we‘ve seen, standardization transforms features to have a mean of 0 and standard deviation of 1. Normalization is a more general term that encompasses many types of scaling, but a common type called min-max scaling transforms features to have a minimum value of 0 and a maximum value of 1.
The formula for min-max normalization is:
X_norm = (X – X_min) / (X_max – X_min)
where X_min is the minimum value of the feature and X_max is the maximum value.
Some key differences between standardization and min-max normalization:
- Standardization centers values around 0 while normalization shifts values to have a min of 0 and max of 1
- Standardization maintains the shape of the original distribution while min-max normalization does not
- Standardization is less sensitive to outliers than min-max normalization
In general, standardization is recommended over min-max normalization, especially if your data has outliers or you want to preserve the original distribution. The main advantage of min-max normalization is that all values are guaranteed to be between 0 and 1, which can be desirable for some algorithms. But overall standardization is more commonly used and a better default choice.
Effect of Standardization on Outliers
An outlier is a data point that is significantly different from the other data points in a feature. Outliers can have a large effect on the mean and standard deviation of a feature, which in turn affects the standardized values.
Standardization does not remove outliers from the data, but it does reduce their influence compared to the original feature scales. Since standardization scales values based on the standard deviation, outliers that are many standard deviations from the mean will still be outliers in the standardized data, but they won‘t have as extreme values relative to the other data points.
For example, let‘s say we have a feature with the following values:
[1, 2, 3, 4, 100]The last value of 100 is an extreme outlier. The mean of this feature is 22 and the standard deviation is 43.8. The standardized values would be:
[-0.48, -0.46, -0.43, -0.41, 1.78]The outlier value of 100 got standardized to 1.78, which is still an outlier relative to the other values, but not nearly as extreme as being almost 100x the value of the other data points which is the case in the unstandardized feature.
So while standardization reduces the impact of outliers to some degree by scaling them relative to the variation in the data, it does not remove them completely. If you have extreme outliers that you believe are invalid or erroneous data points, it‘s best to remove or correct them before standardizing.
Standardization Best Practices
Here are some best practices to keep in mind when standardizing your data for machine learning:
-
Only fit the standardization scaler to your training data, then use the same fitted scaler to transform the test data. This prevents information leakage from the test set into the training data.
-
If you have a separate validation set, be sure to also use the scaler fit on the training set to transform the validation data. Don‘t fit the scaler to the validation data.
-
When doing cross-validation, make sure to fit a new scaler in each fold using only that fold‘s training data. Don‘t fit the scaler to the whole dataset upfront.
-
After standardizing, check that each feature indeed has a mean close to 0 and standard deviation close to 1 to verify the transformation worked as expected.
-
Be careful when applying standardization to sparse data with many zeros. It can significantly change the sparsity structure of the data.
-
If your data has extreme outliers, consider removing or capping them before applying standardization, as they can negatively affect the standardized values of the non-outlier data points.
-
Don‘t standardize your target variable y, only the input features X. Also don‘t standardize categorical features, dummy variables, or binary features.
-
If you‘re not sure whether standardization will help, try training your model both with and without standardization and compare performance. For algorithms sensitive to feature scaling, standardization is likely to help, but it‘s always good to verify empirically.
Conclusion
Standardization is an important data preprocessing technique for machine learning that transforms features to center them around a mean of 0 with a standard deviation of 1. It‘s an essential step for algorithms that are sensitive to the scales of input features, such as those that use distance metrics or gradient descent optimization. Standardizing features can significantly improve the convergence speed and performance of these algorithms.
However, standardization is not necessary for all machine learning algorithms, such as decision trees and Naive Bayes classifiers. It‘s also not a cure-all for data issues and does not remove outliers. Feature scaling via standardization should be done carefully following best practices.
The key steps for standardization are:
- Splitting data into train and test sets
- Fitting the standardization scaler on the training data only
- Transforming the training and test data using the scaler fit on the training data
- Verifying the transformed features have 0 mean and unit variance
- Training your machine learning model on the standardized features
By understanding standardization and following these steps and best practices, you can unlock significant improvements in your machine learning models. It‘s a core concept to master for any data scientist and machine learning practitioner.