Skip to main content

Welcome to a Grasp lesson

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

Lesson 34

Feature Scaling with StandardScaler and MinMaxScaler

Recommended resources

Following our last lesson on encoding categorical variables, we now turn our attention to the other fundamental data type you'll encounter: numerical features. While numbers are already in a format that models can understand, their scale can significantly impact a model's performance.

This lesson focuses on scaling numerical features using two of the most common techniques: StandardScaler and MinMaxScaler. You will learn why features with vastly different ranges can cause problems for certain algorithms and how to use these scalers to create a more stable and effective modeling process. We will continue to build on the scikit-learn pipeline concepts from our previous discussion to create robust, end-to-end preprocessing workflows.

1. Why is Feature Scaling Necessary?

Imagine a dataset with two features: a person's age (ranging from 20 to 70) and their annual_income (ranging from $30,000 to $250,000). The annual_income values are orders of magnitude larger than the age values. For many machine learning algorithms, this disparity in scales can be problematic. Let's explore the two primary reasons why.

Standardization vs Normalization Clearly Explained!

The video "Standardization vs Normalization Clearly Explained!" by Normalized Nerd offers a great visual intuition for why scaling is so important.

Watch the section on why we scale. Pay close attention to the two key scenarios discussed: Faster Convergence in Gradient Descent: The video explains how features on different scales can cause the optimization path to oscillate, slowing down the process of finding the optimal model weights. Fair Distance Calculations: For algorithms like K-Nearest Neighbors (KNN), the feature with the larger scale will dominate the distance calculation, effectively making the smaller-scale feature irrelevant.

To summarize the key points from the video:

  • Gradient-Based Algorithms (e.g., Linear/Logistic Regression, Neural Networks): These models learn by updating their weights based on the error. The size of this update is influenced by the scale of the input features. If one feature is much larger than another, its corresponding weight will receive much larger updates, which can lead to an unstable training process and slower convergence.
  • Distance-Based Algorithms (e.g., K-Nearest Neighbors, K-Means, SVMs): These algorithms rely on calculating distances between data points. If features are on different scales, the feature with the larger range will disproportionately influence the distance metric. For example, a difference of 5 years in age is negligible compared to a difference of $50,000 in income, so the distance calculation would be almost entirely driven by income.

Conversely, tree-based algorithms like Decision Trees, Random Forests, and Gradient Boosting are generally immune to feature scaling. This is because they make decisions by splitting features based on threshold values, and the location of these splits is not affected by the feature's scale.


2. Core Scaling Techniques: Standardization and Normalization

There are two primary methods for bringing numerical features onto a common scale. While the terms are sometimes used interchangeably, they refer to distinct transformations.

Normalization (Min-Max Scaling)

Normalization, often called Min-Max scaling, rescales data to a fixed range, typically [0, 1].

The formula for this transformation is:

Here, and are the minimum and maximum values of the feature in the training data.

  • Key Idea: Every data point is expressed as a proportion of the total range. The minimum value becomes 0, the maximum becomes 1, and all other values fall somewhere in between.
  • When to use it: This is useful for algorithms that require inputs within a bounded range, such as neural networks and in image processing, where pixel intensities are scaled between 0 and 1.
  • Caveat: Min-Max scaling is very sensitive to outliers. A single extreme value can shrink the range of the rest of the data, compressing most values into a very small sub-interval.

Standardization (Z-score Normalization)

Standardization rescales data to have a mean of 0 and a standard deviation of 1.

The formula for this transformation is:

Here, is the mean and is the standard deviation of the feature in the training data. The resulting value is often called a "z-score."

  • Key Idea: It centers the data around 0 and scales it based on its standard deviation. It doesn't bind values to a specific range.
  • When to use it: This is the most common scaling technique and can be considered the default choice. It is particularly effective for algorithms that assume zero-centered data or use regularization, like Linear Regression, Logistic Regression, and SVMs.
  • Common Misconception: Standardization does not make your data follow a normal (Gaussian) distribution. It only changes the mean and standard deviation; the fundamental shape of the distribution remains the same.

The following image provides a great visual summary of how these two techniques affect the distribution of data.

This figure compares the distributions of two features before and after applying Min-Max and Standard scaling. Notice how Min-Max scaling brings both features into the [0, 1] range, while Standard scaling centers them around 0 with a similar spread.

3. Deeper Dive and Implementation

Now that you have the conceptual understanding, let's explore these techniques more deeply with some reading and code.

About Feature Scaling and Normalization

This article by Sebastian Raschka provides a clear, concise explanation of both techniques and discusses when you might choose one over the other.

Read the following sections to solidify your understanding: "About standardization": This section explains the formula and reinforces why scaling is critical for algorithms using gradient descent or distance metrics. "About Min-Max scaling": This section introduces the formula for normalization and highlights its key characteristic: scaling data to a bounded range. "Z-score standardization or Min-Max scaling?": This provides practical guidance. For instance, PCA and clustering often benefit from standardization, while neural networks and image processing typically use Min-Max scaling.


4. Practical Implementation with Scikit-learn

As with categorical encoding, scikit-learn provides easy-to-use tools for scaling, and they integrate perfectly into the Pipeline workflow we learned about in the previous lesson.

The video below demonstrates how to use MinMaxScaler and StandardScaler in Python.

Standardization Vs Normalization- Feature Scaling

In his video "Standardization Vs Normalization- Feature Scaling," Krish Naik walks through the practical implementation.

Watch these two segments to see the code in action: MinMaxScaler: Observe how MinMaxScaler is imported and used with .fit_transform() to scale the data between 0 and 1. StandardScaler: See the same process applied with StandardScaler to achieve a mean of 0 and a standard deviation of 1.

Integrating Scalers into a Preprocessing Pipeline

The true power of these tools is realized when you combine them with the encoders from our last lesson using ColumnTransformer. This allows you to build a single, comprehensive preprocessing object that handles all your features correctly.

Let's imagine you have a dataset with both numerical and categorical features. Here is how you would construct a pipeline to apply StandardScaler to the numerical columns and OneHotEncoder to the categorical ones.

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.linear_model import LogisticRegression




# Sample data
data = {
    'age': [25, 45, 35, 50, 23],
    'fare': [7.25, 71.28, 7.92, 53.1, 8.05],
    'embarked': ['S', 'C', 'S', 'S', 'Q'],
    'survived': [0, 1, 1, 1, 0]
}
df = pd.DataFrame(data)

X = df[['age', 'fare', 'embarked']]
y = df['survived']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)




# Identify numerical and categorical columns
numerical_features = ['age', 'fare']
categorical_features = ['embarked']




# Create the preprocessing pipeline for both data types
preprocessor = ColumnTransformer(
    transformers=[
        ('num', StandardScaler(), numerical_features),
        ('cat', OneHotEncoder(handle_unknown='ignore'), categorical_features)
    ])




# Create the full modeling pipeline
model_pipeline = Pipeline(steps=[
    ('preprocessor', preprocessor),
    ('classifier', LogisticRegression())
])




# Fit the pipeline to the training data
model_pipeline.fit(X_train, y_train)




# Evaluate on the test data
accuracy = model_pipeline.score(X_test, y_test)
print(f"Model Accuracy: {accuracy:.2f}")




# The pipeline handles all preprocessing and prediction in one step
print("\nPrediction for a new data point:")
new_data = pd.DataFrame([{'age': 40, 'fare': 100, 'embarked': 'C'}])
prediction = model_pipeline.predict(new_data)
print(f"Predicted class: {prediction[0]}")

This example brings together everything from the last two lessons. The ColumnTransformer is your central tool for applying the right transformation to the right columns, creating a clean, reproducible, and error-free preprocessing workflow.


5. Decision Framework: Which Scaler to Choose?

Scenario Recommended Scaler Reason & Key Considerations
General Purpose / Unsure StandardScaler This is the safe, default choice. It works well for most algorithms and is less sensitive to outliers than MinMaxScaler.
Data has a Gaussian (Normal) Distribution StandardScaler It will center the distribution at 0, which aligns with the assumptions of many linear models.
Algorithm is sensitive to outliers StandardScaler Because it uses mean and standard deviation, it is not bounded and therefore less affected by extreme outlier values.
Algorithm requires inputs in a bounded range (e.g., [0, 1]) MinMaxScaler Ideal for neural networks and algorithms that use distance metrics on sparse data where preserving zero values is important.
Data has many outliers Neither (Consider RobustScaler) If outliers are a major issue, a RobustScaler (which uses medians and quartiles) is often a better choice. We may explore this in a future lesson.
Using Tree-Based Models (Decision Tree, Random Forest) Scaling is Optional These models are not sensitive to feature scale. However, it is still good practice to include scaling in your pipeline for consistency and if you plan to experiment with other models.

Conclusion

In this lesson, you've learned how to handle numerical features by scaling them, a critical step for many machine learning algorithms. You now understand not just how to apply these techniques, but more importantly, why and when to apply them.

Key Takeaways:

  • Feature scaling is crucial for algorithms that use gradient descent or distance metrics to prevent features with large scales from dominating the model.
  • Standardization (StandardScaler) transforms data to have a mean of 0 and a standard deviation of 1. It is the most common and robust scaling method.
  • Normalization (MinMaxScaler) transforms data into a fixed range, usually [0, 1]. It's useful for specific algorithms like neural networks but is sensitive to outliers.
  • The most professional and robust way to apply preprocessing is by integrating encoders and scalers into a single scikit-learn Pipeline using ColumnTransformer.

With the completion of this lesson, you can now handle the two most common data types in any dataset. In our next lesson, we will move on to a more creative aspect of feature engineering: constructing new features from existing ones, such as extracting information from datetime columns or combining features to capture interactions.