Sitemap

Feature Scaling

3 min readSep 21, 2023

--

Press enter or click to view image in full size

Feature scaling is a data preprocessing technique frequently used in data-oriented fields such as data mining, data analysis and machine learning. This technique helps eliminate problems that may arise when different features are at different scales. Ensures that features have the same scale range or distribution. In a way, it aims to evaluate all features under equal conditions. This allows some machine learning algorithms to perform better. In distance-based methods, variables with large values exhibit dominance. In this case, feature scaling prevents bias and eliminates dominance.

Press enter or click to view image in full size

As seen in the figure, in the first case the variables have different distribution measures. After the scaling process, we can see that they are in the same scale range.

Press enter or click to view image in full size

On the other hand, another important detail is that algorithms using Gradient Descent shorten train times. In the case of scaled features, the size of the errors produced by these scaled features and the effort to reduce them iteratively are faster when the variables are standard. Therefore, if the scales are different from each other, when we use the gradient descent method to reach the minimum point over these variables, the duration of a in the figure takes longer. Because the size of the errors resulting from scale differences will be large in each iteration, the minimum point will be reached in a longer time.

What Are Feature Scaling Methods and How Are They Implemented?

· StandardScaler

First, let’s start with “StandardScale”, that is, classical standardization. Classical standardization can simply be expressed as “subtract the mean, divide by the standard deviation”. Let’s take a look at the python code that will do this.

The indispensable “scikit-learn” library will be with us here too. Let’s start by importing our library. Let’s load our dataset and create a standardization object. Let’s standardize our data with “.fit_transform()“.

from sklearn.preprocessing import StandardScaler
df = load()
scaler = StandardScaler()
scaled_data = scaler.fit_transform(df["variable_name"])

· RobustScaler

Get M. Can Benli’s stories in your inbox

Join Medium for free to get updates from this writer.

“Subtract the median and divide by IQR” Both the mean and the standard deviation are metrics that are affected by outliers in the data set. If median is used instead of mean and IQR (Interquartile Range) is used instead of standard deviation, we can standardize by taking into account the central tendency and change.

Note: StandardScaler attempts to center the data at 0 (mean) and standard deviation at 1 (unit variance). However, this process is highly sensitive to outliers, and outliers can greatly affect mean and standard deviation calculations.

RobustScaler, on the other hand, is more resistant to outliers. RobustScaler scales data using median (middle value) and IQR (Interquartile Range). This causes outliers to have limited impact on median and IQR calculations.

So, the difference between StandardScaler and RobustScaler is the way they react to outliers. StandardScaler is sensitive to outliers, but RobustScaler is more robust and reduces the impact of outliers. Therefore, if you have outliers in your data set or you want outliers to not disrupt data analysis, it may be more appropriate to use RobustScaler.

from sklearn.preprocessing import RobustScaler
rs = RobustScaler()
scaled_data = rs.fit_transform(df["variable_name"])

· MinMaxScaler

Converts each property to a specific range. It usually scales in the range [0, 1] or a custom range specified so that all features of the data have the same scale range.

Xstd = (X — X.min(axis=0)) / (X.max(axis=0) — X.min(axis=0))
Xscaled = Xstd * (max — min) + min

from sklearn.preprocessing import MinMaxScaler
mms= MinMaxScaler()
scaled_data = mms.fit_transform(df["variable_name"])

In this post, I mentioned about Feature Scaling. Thank you for reading.

--

--