Hello! Let's dive into our next lesson in the Data Preprocessing and Feature Engineering module.
In the last lesson, we successfully converted all categorical features into a numerical format. Our dataset is now fully numeric, which is a big step! However, if you look at a typical dataset, you'll notice that different columns can have vastly different scales. For instance, an 'age' feature might range from 18 to 80, while an 'income' feature could range from 30,000 to 200,000. This discrepancy can cause problems for many machine learning algorithms.
Today, we'll address this exact issue. Your learning outcome is to apply feature scaling techniques (standardization and normalization) and explain when each is appropriate. By the end of this 60-minute lesson, you'll understand why scaling is critical and be able to choose and implement the right technique for your specific project.
1. Why is Feature Scaling Necessary?
Imagine you're training a K-Nearest Neighbors (KNN) model, which relies on calculating the distance between data points. If one feature (like income) has a much larger range than another (like age), the distance calculation will be completely dominated by the income feature. The age feature's contribution will be almost negligible, and the model will fail to learn from it properly.
Feature scaling puts all features on a similar scale, ensuring that each one contributes proportionately to the model's learning process.
To start, let's watch a short video that clearly illustrates this problem with a practical example.
Normalization Vs. Standardization (Feature Scaling in Machine Learning)
Professor Ryan Ahmed provides a great explanation of why feature scaling is so important, using a dataset with features on vastly different scales to make the point.
Watch the beginning of the video until timestamp 01:48. Focus on how features like 'interest rate' and 'S&P 500' have different scales and why this might cause one feature to dominate the others during model training.
Which Algorithms Are Affected?
It's important to know that not all algorithms require feature scaling.
-
Sensitive Algorithms: Algorithms that use distance measures or are based on gradient descent are highly sensitive to feature scales. This includes:
- Linear Regression & Logistic Regression
- K-Nearest Neighbors (KNN)
- Support Vector Machines (SVM)
- K-Means Clustering
- Principal Component Analysis (PCA)
- Neural Networks (Deep Learning)
-
Insensitive Algorithms: Tree-based algorithms are generally not sensitive to feature scaling because they make splitting decisions based on single features at a time. This includes:
- Decision Trees
- Random Forest
- Gradient Boosting models (like XGBoost, LightGBM)
Let's reinforce this with a quick read.
How to Use StandardScaler and MinMaxScaler Transforms in Python
For a more detailed explanation, this article from Machine Learning Mastery elaborates on which algorithms are sensitive to feature scales and why.
Read the section titled 'The Scale of Your Data Matters'. Pay attention to the types of algorithms mentioned (e.g., those using weighted sums or distance measures).
2. The Two Main Techniques: Normalization and Standardization
Now that we understand the "why," let's focus on the "how." The two most common scaling techniques are Normalization and Standardization. This image provides an excellent high-level comparison.

Let's break each one down.
Normalization (Min-Max Scaling)
Normalization scales your data into a fixed range, typically between 0 and 1.
- Formula:
- Result: The minimum value of the feature becomes 0, the maximum value becomes 1, and all other values fall somewhere in between.
- Scikit-learn Class:
MinMaxScaler
Standardization (Z-score Scaling)
Standardization rescales data to have a mean of 0 and a standard deviation of 1.
- Formula:
(where is the mean and is the standard deviation) - Result: The feature will be "centered" around 0. Most values will lie between -3 and 3, but there's no fixed upper or lower bound.
- Scikit-learn Class:
StandardScaler
To see how these are implemented in practice, let's watch a hands-on demonstration.
Standardization Vs Normalization- Feature Scaling
This video by Krish Naik will walk us through the formulas and the practical implementation of both Normalization and Standardization using scikit-learn.
Watch from 04:05 to 09:00. First, you'll see how MinMaxScaler (Normalization) transforms data to the [0, 1] range. Then, you'll see how StandardScaler (Standardization) transforms data to have a mean of 0 and a standard deviation of 1.
Hands-On Practice
Here is a Python example demonstrating how to apply these scalers. Notice the crucial pattern: we use .fit_transform() on the training data to learn the scaling parameters (min/max or mean/std) and then use .transform() on the test data to apply those same parameters. This prevents data leakage from the test set into our training process.
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler, StandardScaler
# Sample data with features on different scales
data = {'age': [25, 30, 45, 35, 50, 22],
'salary': [50000, 60000, 120000, 80000, 150000, 45000]}
df = pd.DataFrame(data)
# Split data to simulate a real scenario
X_train, X_test = train_test_split(df, test_size=0.33, random_state=42)
print("--- Original Data ---")
print("Train:\n", X_train)
print("\nTest:\n", X_test)
# --- Normalization (MinMaxScaler) ---
min_max_scaler = MinMaxScaler()
# Fit on training data and transform it
X_train_normalized = min_max_scaler.fit_transform(X_train)
# Only transform the test data
X_test_normalized = min_max_scaler.transform(X_test)
print("\n--- After Normalization (MinMaxScaler) ---")
print("Train Scaled:\n", pd.DataFrame(X_train_normalized, columns=df.columns))
print("\nTest Scaled:\n", pd.DataFrame(X_test_normalized, columns=df.columns))
# --- Standardization (StandardScaler) ---
standard_scaler = StandardScaler()
# Fit on training data and transform it
X_train_standardized = standard_scaler.fit_transform(X_train)
# Only transform the test data
X_test_standardized = standard_scaler.transform(X_test)
print("\n\n--- After Standardization (StandardScaler) ---")
print("Train Scaled:\n", pd.DataFrame(X_train_standardized, columns=df.columns))
print("\nTest Scaled:\n", pd.DataFrame(X_test_standardized, columns=df.columns))
# Verify the properties of the standardized training data
print("\n\n--- Properties of Standardized Training Data ---")
print("Mean (close to 0):\n", pd.DataFrame(X_train_standardized, columns=df.columns).mean())
print("\nStandard Deviation (close to 1):\n", pd.DataFrame(X_train_standardized, columns=df.columns).std())
3. When to Use Which? The Decision-Making Process
This is the most important part of the lesson: choosing the right tool for the job. The decision depends on your algorithm and, critically, the distribution of your data.
Standardization Vs Normalization- Feature Scaling
So when should you use one over the other? Let's finish the video from Krish Naik, where he discusses this exact point.
Watch from 09:00 to the end. Pay attention to his rules of thumb for when to use each scaler, which is based on both the algorithm type and his practical experience.
Here's a summary of the key decision factors:
-
Data Distribution and Outliers:
- Standardization is generally preferred if your data follows a Gaussian (bell-shaped) distribution. It is also less sensitive to outliers.
- Normalization is useful when your data does not have a Gaussian distribution and you need your feature values to be in a bounded interval (e.g., [0, 1] for image pixel intensities or certain neural networks). However, it is very sensitive to outliers. A single extreme value can cause all other data points to be "squashed" into a very small range.
-
The Impact of Outliers: A Visual Explanation
The image below perfectly illustrates how different scalers handle outliers. Pay close attention to MinMaxScaler.
This visual shows why Standardization is often a safer default choice. The RobustScaler is even better at handling outliers, which is a technique we'll discuss in our next lesson.
To solidify your decision-making process, the following reading provides an excellent final summary.
How to Use StandardScaler and MinMaxScaler Transforms in Python
This can still be a bit confusing, so let's read a concise FAQ that addresses the common question: 'Should I Normalize or Standardize?'.
Read the 'Common Questions' section, especially the first question 'Should I Normalize or Standardize?'. It gives a great rule of thumb for when to choose which and what to do if you're unsure.
The key takeaway is that there's no single "best" scaler. If you're unsure, standardization is a good starting point. For best results on a serious project, you would experiment to see which scaling method yields the best performance for your specific model.
Conclusion
Great job! You have now added another essential preprocessing technique to your data science toolkit. Understanding how and when to scale your features is crucial for building high-performing models.
Key Takeaways:
- Why we scale: To prevent features with large ranges from dominating algorithms that are sensitive to scale (e.g., distance-based or gradient-based).
- Normalization (
MinMaxScaler): Scales features to a fixed range, usually [0, 1]. It's useful for algorithms that require bounded inputs but is highly sensitive to outliers. - Standardization (
StandardScaler): Scales features to have a mean of 0 and a standard deviation of 1. It's less affected by outliers and is a robust, common default choice. - How to choose: Consider your algorithm and data distribution. Standardization is often a safe bet. When in doubt, you can always test multiple approaches.
Preview of the next lesson:
In this lesson, we saw how outliers can dramatically affect certain scaling techniques. This highlights the importance of dealing with them directly. In our next lesson, we will focus on applying common strategies for handling outliers, such as clipping and removal, building directly on the concepts we've touched on today.