# Feature Scaling: MinMax, Standard and Robust Scaler

Feature Scaling is performed during the Data Preprocessing step. Also known as normalization, it is a method that is used to standardize the range of features of data. Most of the Machine Learning algorithms (for example, Linear Regression) give a better performance when numerical input variables (i.e., numerical features) are scaled to a standard range.

Feature Scaling is important as the scale of the input variables of the data can have varying scales. Python’s sklearn library provides a lot of scalers such as MinMax Scaler, Standard Scaler, and Robust Scaler.

## MinMax Scaler

MinMax Scaler is one of the most popular scaling algorithms. It transforms features by scaling each feature to a given range, which is generally [0,1], or [-1,-1] in case of negative values.

For each feature, the MinMax Scaler follows the formula:

It subtracts the mean of the column from each value and then divides by the range, i.e, max(x)-min(x).

This scaling algorithm works very well in cases where the standard deviation is very small, or in cases which don’t have Gaussian distribution.

Let’s look at an example of MinMax Scaler in Python. Consider, the following data:

Now, let’s scale this data using sklearn’s MinMax Scaler:

Output:

We can see that the data points have been scaled to values between 0 and 1.

We can also modify the code to scale the values between other range, such as [-2,-2], as:

Output:

## Standard Scaler

Standard Scaler follows the Standard Normal Distribution, i.e., it assumes a normal distribution for data within each feature. The scaling makes the distribution centered around 0, with a standard deviation of 1 and the mean removed.

For a data sample x, its score for Standard Scaler is calculated as:

Where sd is the standard deviation of x.

Consider the same previous data:

Now, let’s scale this data using sklearn’s Standard Scaler:

Output:

Let’s take another example, a more properly distributed data for this case:

Fit Standard Scaler onto this data:

We can see the mean value, as:

Output:

Now apply the transformation:

Output:

## Robust Scaler

Robust Scaler algorithms scale features that are robust to outliers. The method it follows is almost similar to the MinMax Scaler but it uses the interquartile range (rather than the min-max used in MinMax Scaler). The median and scales of the data are removed by this scaling algorithm according to the quantile range.

It, thus, follows the following formula:

Where Q1 is the 1st quartile, and Q3 is the third quartile.

Let’s see the effect of applying the Robust Scaler on the original data:

Output:

Robust Scaler uses the Inter Quartile Range by default, which is the range between the 1st quartile and the 3rd quartile. The quantile range can also be manually specified by using the quantile_range parameter, as:

Output:

## Summary

In this article, we looked at Feature Scaling, and different scalers such as MinMax, Standard, and Robust Scaler. In the next article, we will focus on Cross-Validation in scikit learn.