Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 29

Feature Scaling: StandardScaler & MinMaxScaler

Recommended resources

Hello! Welcome to the next lesson in our module on Data Visualization and Preparation.

In our last lesson, we used Seaborn to create box plots and heatmaps, which helped us visualize the distributions and correlations within our data. You saw that different features can have vastly different scales and ranges (e.g., a feature for 'age' might range from 18-70, while 'income' could range from 30,000-150,000).

Today, we will address this issue directly. Many machine learning algorithms are sensitive to the scale of input features. This lesson focuses on a critical preprocessing step: feature scaling. You will learn how to apply two of the most common scaling techniques, StandardScaler and MinMaxScaler, using scikit-learn.

This is a fundamental skill that ensures your features contribute fairly to a model's learning process, often leading to better performance and faster training.

1. Why is Feature Scaling Necessary?

Imagine you are building a model to predict house prices using two features: the number of rooms (ranging from 1 to 10) and the house's size in square feet (ranging from 500 to 5000). If you feed this data directly into certain algorithms, the sheer magnitude of the 'size' feature could cause it to dominate the 'number of rooms' feature, not because it's more important, but simply because its values are larger. Feature scaling prevents this.

There are two primary reasons why scaling is crucial:

  1. For Gradient-Based Algorithms: Algorithms like linear regression, logistic regression, and neural networks use an optimization process called gradient descent. If features are on different scales, the optimization path can become inefficient, oscillating back and forth and taking longer to find the best solution. Scaling creates a more direct path to the solution, speeding up convergence.
  2. For Distance-Based Algorithms: Algorithms like K-Nearest Neighbors (KNN), K-Means clustering, and Support Vector Machines (SVM) rely on calculating distances between data points. If one feature has a much larger range than others, it will disproportionately influence the distance calculation.

To get a clear visual and conceptual understanding of these two points, please watch the following video.

Standardization vs Normalization Clearly Explained!

This video from the Normalized Nerd channel provides excellent animated explanations for why scaling is important for both gradient descent and distance-based algorithms.

Watch the segments from 02:55 to 05:16. The first part (02:55 - 04:00) explains how scaling helps gradient descent converge faster. The second part (04:00 - 05:16) shows why it's essential for distance calculations in algorithms like K-NN.

As the video showed, failing to scale your features can cause your models to perform poorly or take an unnecessarily long time to train. Now, let's look at how to solve this.


2. The Two Main Techniques: Standardization and Normalization

We will focus on two popular scaling methods available in scikit-learn. While the terms are sometimes used interchangeably, they refer to distinct processes.

A. Standardization (StandardScaler)

Standardization rescales data to have a mean () of 0 and a standard deviation () of 1. This is also known as Z-score normalization.

The formula for each feature value is:

After standardization, the features will be centered around 0. Most of the data will lie between -3 and 3, but there's no guaranteed range, which means it is less sensitive to outliers than normalization.

B. Normalization (MinMaxScaler)

Normalization (specifically Min-Max scaling) rescales the data to a fixed range, usually [0, 1].

The formula for each feature value is:

This method guarantees that all scaled feature values will be between 0 and 1. However, if your data has extreme outliers, they can squeeze the rest of the data into a very small range, which might not be ideal.

The following video provides a great walkthrough of both techniques, including their formulas, scikit-learn implementation, and the visual result.

Normalization Vs. Standardization (Feature Scaling in Machine Learning)

This video by Prof. Ryan Ahmed provides a clear comparison of Normalization and Standardization, complete with formulas, code examples, and visualizations.

Please watch from 01:48 to 10:05. The first part covers Normalization (MinMaxScaler), and the second part covers Standardization (StandardScaler). Pay attention to the formulas and the statistical summaries of the data before and after scaling.

To reinforce this, the image below shows how the distributions of two features change after applying MinMaxScaler and StandardScaler.

Comparison of MinMax Scaling and Standard Scaling using Box Plots
This figure visually contrasts the effect of MinMax and Standard scaling. Notice how MinMaxScaler forces both features into the [0, 1] range, while StandardScaler centers them around 0 with a similar spread, but doesn't confine them to a strict range.

3. Practical Implementation: The Golden Rule of Scaling

Applying these scalers in Python is straightforward with scikit-learn. However, there is one critically important rule you must follow to prevent a common but serious error called data leakage.

Golden Rule: You must fit your scaler only on the training data. Then, use that same fitted scaler to transform your training, validation, and test sets.

Fitting the scaler on the test set (or the entire dataset before splitting) would mean your model is "peeking" at the test data during the training phase. The scaler would learn the minimum, maximum, mean, or standard deviation from the test data, which is information the model should not have access to. This leads to overly optimistic performance estimates that don't reflect how the model will perform on truly unseen data.

This article explains this concept perfectly and shows the correct workflow.

ML Series: Day 47 — Scaling and Normalization

This article from Medium clearly explains the importance of the train-test split in scaling and provides the correct workflow to avoid data leakage.

Read the section titled '5. Handling Train, Validation, and Test Sets'. Focus on the 'Why Scaling Differently...' and 'Proper Scaling Workflow' subsections. This is one of the most important practical concepts in data preprocessing.

Applying Scalers in Python

Here is the standard workflow using scikit-learn:

  1. Split your data into training and testing sets.
  2. Create an instance of the scaler (e.g., StandardScaler).
  3. Fit and transform the training data using scaler.fit_transform(X_train). This learns the scaling parameters (mean/std or min/max) from the training data and then applies the transformation.
  4. Transform the test data using scaler.transform(X_test). This applies the same transformation using the parameters learned from the training data.

Let's see this in action with a full code demonstration.

Normalization Vs. Standardization (Feature Scaling in Machine Learning)

Let's return to Prof. Ryan Ahmed's video for a practical demonstration in a Jupyter Notebook. This will walk you through the entire process from loading data to applying both scalers and verifying the results.

Watch the code walkthrough from 12:51 to 19:40. Follow along as he loads the data, visualizes it, and then applies both MinMaxScaler and StandardScaler, paying close attention to the fit_transform and transform steps (although in this demo he applies it to the whole dataset for simplicity, the principle of fitting on train and transforming test is what we must follow in practice).

Test your understanding!

You have split your data into X_train and X_test. You decide to use StandardScaler. Which of the following is the correct sequence of operations?

A:

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.fit_transform(X_test)

B:

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X) # where X is the full dataset
X_train_scaled, X_test_scaled, ... = train_test_split(X_scaled, ...)

C:

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
Show answer

Option C is the correct answer.

  • Option A is incorrect because it calls fit_transform on the test data. This refits the scaler using the mean and standard deviation of the test set, which constitutes data leakage and applies an inconsistent transformation.
  • Option B is incorrect because it fits the scaler on the entire dataset before splitting. This is also data leakage, as information from the test set is used to determine the scaling parameters for the training set.
  • Option C is correct because it learns the scaling parameters (fit) exclusively from the training data and then applies that exact same transformation (transform) to both the training and test sets, simulating a real-world scenario.

4. Which Scaler Should I Use?

There's no single "best" scaler for all situations, but here are some general guidelines:

  • StandardScaler is often a good default choice. It's effective for many algorithms, especially those that assume data is centered at zero, like SVMs and PCA. It is also more robust to outliers than MinMaxScaler.
  • MinMaxScaler is useful when you need your feature values to be within a specific bounded interval (e.g., [0, 1]). This is common for image data (where pixel values are 0-255) and is often recommended for neural networks.

The blog post below provides an excellent summary table comparing these methods.

Feature Scaling: Standardization vs. Normalization And ...

This blog post offers a very clear summary of different scaling methods, their benefits, and their limitations.

Scroll down to the 'Summary' section and review the table. Focus on the columns for 'Standardization' and 'Min-max Normalization' to quickly compare their concepts, benefits, and limitations.


Conclusion

You've now learned one of the most common and important steps in preparing data for machine learning. By putting your features on a comparable scale, you help your algorithms perform more efficiently and effectively.

Key Takeaways:

  • Feature scaling is necessary for many ML algorithms to prevent features with large ranges from dominating the learning process.
  • Standardization (StandardScaler) transforms data to have a mean of 0 and a standard deviation of 1. It's a robust default choice.
  • Normalization (MinMaxScaler) transforms data to a fixed range, typically [0, 1]. It's useful when you need bounded inputs, like in neural networks.
  • The most critical rule is to fit the scaler only on the training data and then transform all data splits (train, validation, test) with that same fitted scaler to prevent data leakage.

Preview of the Next Lesson:

We have now dealt with numerical features. But what about features that represent categories, like color: ('Red', 'Green', 'Blue') or city: ('New York', 'London', 'Tokyo')? Machine learning models require all inputs to be numeric. In the next lesson, we will tackle this by learning how to encode categorical variables using techniques like One-Hot Encoding and Label Encoding.