TABLE OF CONTENTS
Features on a different scale is a common issue data scientists encounter. Some algorithms can handle it but some don’t and if features are not scaled properly beforehand, we will have a hard time finding the optimal solution. So, why does it matter and how can we solve it?
First, let’s think about KNN, which uses Euclidean distance to determine the similarity between points. When calculating the distance, features with a bigger magnitude will influence much more than the ones with a smaller magnitude, which leads to a solution dominated by the bigger features. Another example is algorithms using gradient descent. Features on a different scale will have different step sizes and it will take a longer time to converge as shown below.
Gradient descent without scaling (left) and with scaling (right) (Image source)
The almost only exception is tree-based algorithms that use Gini impurity or information gain which are not influenced by feature scale. Here is some examples of machine learning models sensitive and non-sensitive to feature scale:
ML Models sensitive to feature scale
ML models not sensitive to feature scale
How can we scale features then? There are two types of scaling techniques depending on their focus: 1) standardization and 2) normalization.
Standardization focuses on scaling the variance in addition to shifting the center to 0. It comes from the standardization in statistics, which converts a variable into z−score that represents the number of standard deviations away from the mean no matter what the original value is.
Normalization focuses on scaling the min-max range rather than variance. For example, the original value range of [100, 200] is simply scaled to be [0, 1] by substracting the minimum value and dividing by the range. There are a few variations of normalization depending on whether it centers the data and what min/max value it uses: 1) min-max normalization, 2) max-abs normalization, 3) mean normalization, and 4) median-quantile normalization.
Each scaling method has its own advantages and limitations and there is no method that works for every situation. We should understand each method, implement them, and see which one works best for a specific problem. In the remaining sections of this post, I will explain the definition, advantages, limitations, and Python implementation of all of the mentioned scaling methods.
# define functions used in this post
def kdeplot(df, scaler_name):
fix, ax = plt.subplots(figsize=(7, 5))
for feature in df.columns:
sns.kdeplot(df[feature], ax=ax)
ax.set_title(scaler_name)
ax.set_ylabel('Density')
ax.set_xlabel('Feature value')
plt.tight_layout()
def kdeplot_with_zoom(df, scaler_name, xlim):
fix, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# original
for feature in df.columns:
sns.kdeplot(df[feature], ax=ax1)
ax1.set_title(scaler_name)
ax1.set_ylabel('Density')
ax1.set_xlabel('Feature value')
# zoomed
for feature in features:
sns.kdeplot(df[feature], ax=ax2)
ax2.set_xlim(xlim)
ax2.set_title(scaler_name + ' (zoomed)')
ax2.set_ylabel('Density')
ax2.set_xlabel('Feature value')
plt.tight_layout()