Feature Engineering: Scaling, Normalization, and Standardization
Last Updated :
17 Jan, 2025
Feature Scaling is a technique to standardize the independent features present in the data. It is performed during the data pre-processing to handle highly varying values. If feature scaling is not done then machine learning algorithm tends to use greater values as higher and consider smaller values as lower regardless of the unit of the values. For example it will take 10 m and 10 cm both as same regardless of their unit. In this article we will learn about different techniques which are used to perform feature scaling.
1. Absolute Maximum Scaling
This method of scaling requires two-step:
- We should first select the maximum absolute value out of all the entries of a particular measure.
- Then after this we divide each entry of the column by this maximum value.
[Tex]X_{\rm {scaled }}=\frac{X_{i}-\rm{max}\left(|X|\right)}{\rm{max}\left(|X|\right)}[/Tex]
After performing the above-mentioned two steps we will observe that each entry of the column lies in the range of -1 to 1. But this method is not used that often the reason behind this is that it is too sensitive to the outliers. And while dealing with the real-world data presence of outliers is a very common thing.
For the demonstration purpose we will use the dataset which you can download from here. This dataset is a simpler version of the original house price prediction dataset having only two columns from the original dataset. The first five rows of the original data are shown below:
Python
import pandas as pd
df = pd.read_csv('SampleFile.csv')
print(df.head())
Output:
LotArea MSSubClass
0 8450 60
1 9600 20
2 11250 60
3 9550 70
4 14260 60
Now let’s apply the first method which is of the absolute maximum scaling. For this first, we are supposed to evaluate the absolute maximum values of the columns.
Python
import numpy as np
max_vals = np.max(np.abs(df))
max_vals
Output:
LotArea 215245
MSSubClass 190
dtype: int64
Now we are supposed to subtract these values from the data and then divide the results from the maximum values as well.
Python
print((df - max_vals) / max_vals)
Output:
LotArea MSSubClass
0 -0.960742 -0.684211
1 -0.955400 -0.894737
2 -0.947734 -0.684211
3 -0.955632 -0.631579
4 -0.933750 -0.684211
... ... ...
1455 -0.963219 -0.684211
1456 -0.938791 -0.894737
1457 -0.957992 -0.631579
1458 -0.954856 -0.894737
1459 -0.953834 -0.894737
[1460 rows x 2 columns]
2. Min-Max Scaling
This method of scaling requires below two-step:
- First we are supposed to find the minimum and the maximum value of the column.
- Then we will subtract the minimum value from the entry and divide the result by the difference between the maximum and the minimum value.
[Tex]X_{\rm {scaled }}=\frac{X_{i}-X_{\text {min}}}{X_{\rm{max}} – X_{\rm{min}}}[/Tex]
As we are using the maximum and the minimum value this method is also prone to outliers but the range in which the data will range after performing the above two steps is between 0 to 1.
Python
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(df)
scaled_df = pd.DataFrame(scaled_data,
columns=df.columns)
scaled_df.head()
Output:
LotArea MSSubClass
0 0.033420 0.235294
1 0.038795 0.000000
2 0.046507 0.235294
3 0.038561 0.294118
4 0.060576 0.235294
3. Normalization
This method is more or less the same as the previous method but here instead of the minimum value we subtract each entry by the mean value of the whole data and then divide the results by the difference between the minimum and the maximum value.
[Tex]X_{\rm {scaled }}=\frac{X_{i}-X_{\text {mean}}}{X_{\rm{max}} – X_{\rm{min}}}[/Tex]
Python
from sklearn.preprocessing import Normalizer
scaler = Normalizer()
scaled_data = scaler.fit_transform(df)
scaled_df = pd.DataFrame(scaled_data,
columns=df.columns)
print(scaled_df.head())
Output:
LotArea MSSubClass
0 0.999975 0.007100
1 0.999998 0.002083
2 0.999986 0.005333
3 0.999973 0.007330
4 0.999991 0.004208
4. Standardization
This method of scaling is basically based on the central tendencies and variance of the data.
- First we should calculate the mean and standard deviation of the data we would like to normalize it.
- Then we are supposed to subtract the mean value from each entry and then divide the result by the standard deviation.
This helps us achieve a normal distribution of the data with a mean equal to zero and a standard deviation equal to 1.
[Tex]X_{\rm {scaled }}=\frac{X_{i}-X_{\text {mean }}}{\sigma}[/Tex]
Python
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaled_data = scaler.fit_transform(df)
scaled_df = pd.DataFrame(scaled_data,
columns=df.columns)
print(scaled_df.head())
Output:
LotArea MSSubClass
0 -0.207142 0.073375
1 -0.091886 -0.872563
2 0.073480 0.073375
3 -0.096897 0.309859
4 0.375148 0.073375
5. Robust Scaling
In this method of scaling, we use two main statistical measures of the data.
After calculating these two values we are supposed to subtract the median from each entry and then divide the result by the interquartile range.
[Tex]X_{\rm {scaled }}=\frac{X_{i}-X_{\text {median }}}{IQR}[/Tex]
Python
from sklearn.preprocessing import RobustScaler
scaler = RobustScaler()
scaled_data = scaler.fit_transform(df)
scaled_df = pd.DataFrame(scaled_data,
columns=df.columns)
print(scaled_df.head())
Output:
LotArea MSSubClass
0 -0.254076 0.2
1 0.030015 -0.6
2 0.437624 0.2
3 0.017663 0.4
4 1.181201 0.2
In conclusion scaling, normalization and standardization are essential feature engineering techniques that ensure data is well-prepared for machine learning models. They help improve model performance, enhance convergence and reduce biases. Choosing the right method depends on your data and algorithm.
Why use Feature Scaling?
In machine learning feature scaling is used for number of purposes:
- Range: Scaling guarantees that all features are on a comparable scale and have comparable ranges. This process is known as feature normalisation. This is significant because the magnitude of the features has an impact on many machine learning techniques. Larger scale features may dominate the learning process and have an excessive impact on the outcomes.
- Algorithm performance improvement: When the features are scaled several machine learning methods including gradient descent-based algorithms, distance-based algorithms (such k-nearest neighbours) and support vector machines perform better or converge more quickly. The algorithm’s performance can be enhanced by scaling the features which prevent the convergence of the algorithm to the ideal outcome.
- Preventing numerical instability: Numerical instability can be prevented by avoiding significant scale disparities between features. For examples include distance calculations where having features with differing scales can result in numerical overflow or underflow problems. Stable computations are required to mitigate this issue by scaling the features.
- Equal importance: Scaling features makes sure that each characteristic is given the same consideration during the learning process. Without scaling bigger scale features could dominate the learning producing skewed outcomes. This bias is removed through scaling and each feature contributes fairly to model predictions.
Get IBM Certification and a 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.
Master Machine Learning, Data Science & AI with this complete program and also get a 90% refund. What more motivation do you need? Start the challenge right away!
Similar Reads
Normalization vs Standardization
Feature scaling is one of the most important data preprocessing step in machine learning. Algorithms that compute the distance between the features are biased towards numerically larger values if the data is not scaled. Tree-based algorithms are fairly insensitive to the scale of the features. Also, feature scaling helps machine learning, and deep
3 min read
Instance Normalization vs Batch Normalization
Instance normalization and batch normalization are techniques used to make machine learning models train better by normalizing data, but they work differently. Instance normalization normalizes each input individually focusing only on its own features. This is more like giving personalized feedback to each student without comparing them to others.
5 min read
Normalization and Scaling
Normalization and Scaling are two fundamental preprocessing techniques when you perform data analysis and machine learning. They are useful when you want to rescale, standardize or normalize the features (values) through distribution and scaling of existing data that make your machine learning models have better performance and accuracy. This guide
9 min read
What is Standardization in Machine Learning
In Machine Learning we train our data to predict or classify things in such a manner that isn't hardcoded in the machine. So for the first, we have the Dataset or the input data to be pre-processed and manipulated for our desired outcomes. Any ML Model to be built follows the following procedure: Collect DataPerform Data Munging/Cleaning (Feature S
6 min read
Logistic Regression and the Feature Scaling Ensemble
Logistic Regression is a widely used classification algorithm in machine learning. However, to enhance its performance further specially when dealing with features of different scales, employing feature scaling ensemble techniques becomes imperative. In this guide, we will dive depth into logistic regression, its significance and how feature sealin
9 min read
Python | How and where to apply Feature Scaling?
Feature Scaling or Standardization: It is a step of Data Pre Processing that is applied to independent variables or features of data. It helps to normalize the data within a particular range. Sometimes, it also helps in speeding up the calculations in an algorithm. Package Used: sklearn.preprocessing Import: from sklearn.preprocessing import Sta
2 min read
ML | Feature Scaling - Part 1
Feature Scaling is a technique to standardize the independent features present in the data in a fixed range. It is performed during the data pre-processing. Working: Given a data-set with features- Age, Salary, BHK Apartment with the data size of 5000 people, each having these independent data features. Each data point is labeled as: Class1- YES
3 min read
Do Clustering Algorithms Need Feature Scaling in the Pre-Processing Stage?
Answer: Yes, clustering algorithms typically require feature scaling to ensure equal distance consideration across all features.Without scaling, features with larger scales dominate the distance calculations, leading to biased clusters. Here's a comparison table to illustrate the impact of feature scaling on different clustering algorithms: Cluster
1 min read
Feature Scaling - Part 3
Prerequisite - Feature Scaling | Set-1 , Set-2 Feature Scaling is one of the most important steps of Data Preprocessing. It is applied to independent variables or features of data. The data sometimes contains features with varying magnitudes and if we do not treat them, the algorithms only take in the magnitude of these features, neglecting the un
4 min read
Does Linear Regression Require Feature Scaling?
Linear regression does not always require feature scaling, but scaling can improve its performance and help in specific cases. The main reason is that linear regression models calculate the relationship between input variables (features) and the target variable, and they are generally not sensitive to feature scales as it computes weights (coeffici
3 min read