# Feature Engineering: Scaling, Normalization, and Standardization (Updated 2023)

Feature engineering is a critical step in building accurate and effective machine learning models. One key aspect of feature engineering is scaling, normalization, and standardization, which involves transforming the data to make it more suitable for modeling. These techniques can help to improve model performance, reduce the impact of outliers, and ensure that the data is on the same scale. In this article, we will explore the concepts of scaling, normalization, and standardization, including why they are important and how to apply them to different types of data. By the end of this article, you’ll have a thorough understanding of these essential feature engineering techniques and be able to apply them to your own machine learning projects.

## Table of contents

- What is Feature Scaling?
- Why Should we Use Feature Scaling?
- What is Normalization?
- What is Standardization?
- The Big Question – Normalize or Standardize?
- Implementing Feature Scaling in Python
- Comparing Unscaled, Normalized, and Standardized Data
- Applying Scaling to Machine Learning Algorithms
- Frequently Asked Questions

## What is Feature Scaling?

Feature scaling is a data preprocessing technique used to transform the values of features or variables in a dataset to a similar scale. The purpose is to ensure that all features contribute equally to the model and to avoid the domination of features with larger values.

Feature scaling becomes necessary when dealing with datasets containing features that have different ranges, units of measurement, or orders of magnitude. In such cases, the variation in feature values can lead to biased model performance or difficulties during the learning process.

There are several common techniques for feature scaling, including standardization, normalization, and min-max scaling. These methods adjust the feature values while preserving their relative relationships and distributions.

By applying feature scaling, the dataset’s features can be transformed to a more consistent scale, making it easier to build accurate and effective machine learning models. Scaling facilitates meaningful comparisons between features, improves model convergence, and prevents certain features from overshadowing others based solely on their magnitude.

## Why Should we Use Feature Scaling?

Some machine learning algorithms are sensitive to feature scaling, while others are virtually invariant. Let’s explore these in more depth:

#### 1. Gradient Descent Based Algorithms

**Machine learning algorithms like ****linear regression****, logistic regression, neural network, PCA (principal component analysis), etc., that use gradient descent as an optimization technique require data to be scaled.** Take a look at the formula for gradient descent below:

The presence of feature value X in the formula will affect the step size of the gradient descent. The difference in the ranges of features will cause different step sizes for each feature. To ensure that the gradient descent moves smoothly towards the minima and that the steps for gradient descent are updated at the same rate for all the features, we scale the data before feeding it to the model.

Having features on a similar scale can help the gradient descent converge more quickly towards the minima.

#### 2. Distance-Based Algorithms

Distance algorithms like KNN, K-means clustering, and SVM(support vector machines) are most affected by the range of features. This is because, behind the scenes, **they are using distances between data points to determine their similarity.**

For example, let’s say we have data containing high school CGPA scores of students (ranging from 0 to 5) and their future incomes (in thousands Rupees):

Since both the features have different scales, there is a chance that higher weightage is given to features with higher magnitudes. This will impact the performance of the machine learning algorithm; obviously, we do not want our algorithm to be biased towards one feature.

Therefore, we scale our data before employing a distance based algorithm so that all the features contribute equally to the result.

The effect of scaling is conspicuous when we compare the Euclidean distance between data points for students A and B, and between B and C, before and after scaling, as shown below:

- Distance AB before scaling =>
- Distance BC before scaling =>
- Distance AB after scaling =>
- Distance BC after scaling =>

#### 3. Tree-Based Algorithms

Tree-based algorithms, on the other hand, are fairly insensitive to the scale of the features. Think about it, a decision tree only splits a node based on a single feature. The decision tree splits a node on a feature that increases the homogeneity of the node. Other features do not influence this split on a feature.

So, the remaining features have virtually no effect on the split. This is what makes them invariant to the scale of the features!

## What is Normalization?

Normalization is a data preprocessing technique used to adjust the values of features in a dataset to a common scale. This is done to facilitate data analysis and modeling, and to reduce the impact of different scales on the accuracy of machine learning models.

**Normalization is a scaling technique in which values are shifted and rescaled so that they end up ranging between 0 and 1. It is also known as Min-Max scaling.**

Here’s the formula for normalization:

Here, Xmax and Xmin are the maximum and the minimum values of the feature, respectively.

- When the value of X is the minimum value in the column, the numerator will be 0, and hence X’ is 0
- On the other hand, when the value of X is the maximum value in the column, the numerator is equal to the denominator, and thus the value of X’ is 1
- If the value of X is between the minimum and the maximum value, then the value of X’ is between 0 and 1

## What is Standardization?

Standardization is another scaling method where the values are centered around the mean with a unit standard deviation. This means that the mean of the attribute becomes zero, and the resultant distribution has a unit standard deviation.

Here’s the formula for standardization:

is the mean of the feature values and is the standard deviation of the feature values. Note that, in this case, the values are not restricted to a particular range.

Now, the big question in your mind must be when should we use normalization and when should we use standardization? Let’s find out!

## The Big Question – Normalize or Standardize?

Normalization | Standardization |
---|---|

Rescales values to a range between 0 and 1 | Centers data around the mean and scales to a standard deviation of 1 |

Useful when the distribution of the data is unknown or not Gaussian | Useful when the distribution of the data is Gaussian or unknown |

Sensitive to outliers | Less sensitive to outliers |

Retains the shape of the original distribution | Changes the shape of the original distribution |

May not preserve the relationships between the data points | Preserves the relationships between the data points |

Equation: (x – min)/(max – min) | Equation: (x – mean)/standard deviation |

However, at the end of the day, the choice of using normalization or standardization will depend on your problem and the machine learning algorithm you are using. There is no hard and fast rule to tell you when to normalize or standardize your data. **You can always start by fitting your model to raw, normalized, and standardized data and comparing the performance for the best results.**

*It is a good practice to fit the scaler on the training data and then use it to transform the testing data. This would avoid any data leakage during the model testing process. Also, the scaling of target values is generally not required.*

## Implementing Feature Scaling in Python

Now comes the fun part – putting what we have learned into practice. I will be applying feature scaling to a few machine-learning algorithms on the Big Mart dataset. I’ve taken on the DataHack platform.

I will skip the preprocessing steps since they are out of the scope of this tutorial. But you can find them neatly explained in this article. Those steps will enable you to reach the top 20 percentile on the hackathon leaderboard, so that’s worth checking out!

So, let’s first split our data into training and testing sets:

**Python Code:**

Before moving to the feature scaling part, let’s glance at the details of our data using the **pd.describe()** method:

We can see that there is a huge difference in the range of values present in our numerical features: **Item_Visibility**, **Item_Weight, Item_MRP, **and **Outlet_Establishment_Year**. Let’s try and fix that using feature scaling!

*Note: You will notice negative values in the Item_Visibility feature because I have taken log-transformation to deal with the skewness in the feature.*

#### Normalization Using sklearn (scikit-learn)

To normalize your data, you need to import the MinMaxScaler from the sklearn library and apply it to our dataset. So, let’s do that!

Let’s see how normalization has affected our dataset:

All the features now have a minimum value of 0 and a maximum value of 1. Perfect!

Try out the above code in the live coding window below!!

Next, let’s try to standardize our data.

#### Standardization Using sklearn

To standardize your data, you need to import the StandardScaler from the sklearn library and apply it to our dataset. Here’s how you can do it:

You would have noticed that I only applied standardization to my numerical columns, not the other One-Hot Encoded features. Standardizing the One-Hot encoded features would mean assigning a distribution to categorical features. You don’t want to do that!

But why did I not do the same while normalizing the data? Because One-Hot encoded features are already in the range between 0 to 1. So, normalization would not affect their value.

Right, let’s have a look at how standardization has transformed our data:

The numerical features are now centered on the mean with a unit standard deviation. Awesome!

## Comparing Unscaled, Normalized, and Standardized Data

It is always great to visualize your data to understand the distribution present. We can see the comparison between our unscaled and scaled data using boxplots.

*You can learn more about data visualization *here*.*

You can notice how scaling the features brings everything into perspective. The features are now more comparable and will have a similar effect on the learning models.

## Applying Scaling to Machine Learning Algorithms

It’s now time to train some machine learning algorithms on our data to compare the effects of different scaling techniques on the algorithm’s performance. I want to see the effect of scaling on three algorithms in particular: K-Nearest Neighbors, Support Vector Regressor, and Decision Tree.

#### K-Nearest Neighbors

As we saw before, KNN is a distance-based algorithm that is affected by the range of features. Let’s see how it performs on our data before and after scaling:

You can see that scaling the features has brought down the RMSE score of our KNN model. Specifically, the normalized data performs a tad bit better than the standardized data.

*Note: I am measuring the RMSE here because this competition evaluates the RMSE.*

#### Support Vector Regressor

SVR is another distance-based algorithm. So let’s check out whether it works better with normalization or standardization:

We can see that scaling the features does bring down the RMSE score. And the standardized data has performed better than the normalized data. Why do you think that’s the case?

The sklearn documentation states that SVM, with RBF kernel, assumes that all the features are centered around zero and variance is of the same order. This is because a feature with a variance greater than that of others prevents the estimator from learning from all the features. Great!

#### Decision Tree

We already know that a Decision tree is invariant to feature scaling. But I wanted to show a practical example of how it performs on the data:

You can see that the RMSE score has not moved an inch on scaling the features. So rest assured when you are using tree-based algorithms on your data!

## Build Effective Machine Learning Models

This tutorial covered the relevance of using feature scaling on your data and how normalization and standardization have varying effects on the working of machine learning algorithms. Remember that there is no correct answer to when to use normalization over standardization and vice-versa. It all depends on your data and the algorithm you are using.

To enhance your skills in feature engineering and other key data science techniques, consider enrolling in our Data Science Black Belt program. Our comprehensive curriculum covers all aspects of data science, including advanced topics such as feature engineering, machine learning, and deep learning. With hands-on projects and mentorship, you’ll gain practical experience and the skills you need to succeed in this exciting field. Enroll today and take your data science skills to the next level!

## Frequently Asked Questions

**Q1. How is Standardization different from Normalization feature scaling?**

A. Standardization centers data around a mean of zero and a standard deviation of one, while normalization scales data to a set range, often [0, 1], by using the minimum and maximum values.

**Q2. Why is Standardization used in machine learning?**

A. Standardization ensures algorithmic stability and prevents sensitivity to the scale of input features, improves optimization algorithms’ convergence and search efficiency, and enhances the performance of certain machine learning algorithms.

**Q3. Why is Normalization used in machine learning?**

A. Normalization helps in scaling the input features to a fixed range, typically [0, 1], to ensure that no single feature disproportionately impacts the results. It preserves the relationship between the minimum and maximum values of each feature, which can be important for some algorithms. It also improves the convergence and stability of some machine learning algorithms, particularly those that use gradient-based optimization.

**Q4. Why do we normalize values?**

A. We normalize values to bring them into a common scale, making it easier to compare and analyze data. Normalization also helps to reduce the impact of outliers and improve the accuracy and stability of statistical models.

**Q5. How do you normalize a set of values?**

A. To normalize a set of values, we first calculate the mean and standard deviation of the data. Then, we subtract the mean from each value and divide by the standard deviation to obtain standardized values with a mean of 0 and a standard deviation of 1. Alternatively, we can use other normalization techniques such as min-max normalization, where we scale the values to a range of 0 to 1, or unit vector normalization, where we scale the values to have a length of 1.

## 19 thoughts on "Feature Engineering: Scaling, Normalization, and Standardization (Updated 2023)"

## Ali says: April 12, 2020 at 11:18 pm

Excelent article! Thank you very much for sharing. I have one question. In the post you say: "It is a good practice to fit the scaler on the training data and then use it to transform the testing data.", but I didn't see that in the code you posted. Am I wrong? How would one "fit the scaler on the training data and then use it to transform the testing data"? Thanks a lot again## Aniruddha Bhandari says: April 13, 2020 at 11:44 am

Hi Ali You fit the scaler on the training data so that it can calculate the necessary parameters, like mean and standard deviation for standardization, and store it for later use using the fit() method. Later you use the transform() function to apply the same transformation on both, train and test dataset. I have used this approach for both, normalization and standardization, in the article in the gists "NormalizationVsStandarization_2.py" and "NormalizationVsStandarization_3.py" respectively. I hope this cleared your doubt. Thanks## sahil kamboj says: April 28, 2020 at 4:25 pm

Good article! Thank you very much for sharing. I have one question. What the difference between sklearn.preprocessing import MinMaxScaler Normalization and sklearn.preprocessing.Normalizer? When to use MinMaxScaler and when to Normalize?## Aniruddha Bhandari says: April 29, 2020 at 2:15 pm

Hi I hope MinMaxScaler is already clear from the article. Normalizer is also a normalization technique. The only difference is the way it computes the normalized values. By default, it is calculating the l2 norm of the row values i.e. each element of a row is normalized by the square root of the sum of squared values of all elements in that row. As mentioned in the documentation, it is useful in text classification where the dot product of two Tf-IDF vectors gives a cosine similarity between the different sentences/documents in the dataset. Other than that, as I mentioned in the article, there is no sure way to know which scaling technique should be used when. The best way is to create multiple scaled copies of the data and then try them out and see which one gives the best result. Hope this helps.## Subhash Kumar Nadar says: May 23, 2020 at 11:25 am

Excellent article! Easy to understand and good coverage One question: I see that there is a scale() funtion as well from sklearn and short description suggest it to be similar to StandardScaler i.e. scaling to unit variance I could not find more than this explanation. Please can you suggest which on to use which scenario? Thanks in advance!## Golla kedarkumar says: May 24, 2020 at 9:56 am

Hi ANIRUDDHA, If we use the same scaler for train and testing, does it affect the testing data because in standardization we need to use the mean of the data. If we take the mean of the train data and scale the test data, it will influence the test data, right?## Aniruddha Bhandari says: May 24, 2020 at 12:11 pm

Scaling your test data according to the train data makes sure that the test data is on the same scale as the training data on which our model was trained on. This way our model will be able to apply the learnings from the training dataset on the testing dataset, which is exactly what we want! If instead, we scale the test data differently, then our model might not be able to discern that difference, thereby giving us incorrect outputs. That way we will never know how well our model is performing. I hope this helps!## Aniruddha Bhandari says: May 24, 2020 at 12:41 pm

Hi Subhash I notice two differences between the two functions. First was that the scale() function allows you to standardize your data along any axis. This means that you could even standardize your data row-wise as opposed to feature-wise, which is what happens in StandardScaler(). Second difference was that the scale function has no fit and transform methods, so you cannot apply the same scaling to your test dataset. I would suggest using the StandardScaler() function as I have never used the scale() personally. I hope this helps!## Inas says: May 27, 2020 at 6:42 am

Excellent article, thank you for sharing.## soumadip roy says: July 04, 2020 at 12:59 am

This is an excellent write up. Thanks for this.## HARSHVARDHAN BHATT says: July 05, 2020 at 3:03 am

That graphs really helps in putting things in perspective...thanks !## Arnob says: July 05, 2020 at 4:13 pm

Hey bro! Great article. It covered a lots of topics that were unclear to me before. I have a basic question. How can I check my data after normalization. You have mentioned to use pd.describe() in "Normalization using sklearn' section. But when I use it I get an error - " module 'pandas' has no attribute 'describe'". Can you tell me how to check my data after normalization? Thank you for your time.## deeps says: July 15, 2020 at 7:59 pm

Excellent article !## Kunal says: July 31, 2020 at 1:19 pm

Thanks for Great Article..!!!## Zineb says: August 17, 2020 at 3:29 pm

Thanks Bhandari. Easy to understand and very helpful.## Aniruddha Bhandari says: August 22, 2020 at 8:06 pm

Hi Arnob, glad you liked the article. The command you are looking for is df.describe() not pd.describe(). Try using that, it should work.## Tony says: December 06, 2021 at 9:12 am

Hi Aniruddha!Quick question - I've seen a few people already mention that standardization is good if the feature follows Gaussian distribution but don't really get why. Mind shedding more light on that?## Jigme T says: October 24, 2022 at 2:32 pm

Hi !! I would like to share a sample of my data and seek your advice on the featuring scaling methods.## Geofrey says: December 13, 2022 at 4:36 pm

Quite an outstanding article! It answered most of my pressing questions in regards to scaling... Thank You so much.