Feature Scaling (Normalization & Standardization)
What you’ll learn
- why features on very different scales hurt most ML algorithms
- min-max scaling (normalization) with
MinMaxScalerMinMaxScaler - standardization with
StandardScalerStandardScaler - how outliers affect each method differently
- why you fit scalers on the training set only, then transform everything
Why scaling matters
Look at two columns from the housing data: total_roomstotal_rooms ranges roughly from 6 to
39,320, while median_incomemedian_income ranges from about 0 to 15. With few exceptions, ML
algorithms don’t perform well when input features are on such different scales — one
column can dominate a distance calculation or a gradient step purely because its
numbers are bigger, not because it’s more important.
Note that you generally do not need to scale the target value (median_house_valuemedian_house_value
here) — only the input features.
There are two common approaches: min-max scaling and standardization.
Min-max scaling (normalization)
MinMaxScalerMinMaxScaler shifts and rescales values so they land between 0 and 1 (or a custom
range via feature_rangefeature_range):
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(housing_num_train) # fit on the TRAINING set only
X_train_scaled = scaler.transform(housing_num_train)
X_test_scaled = scaler.transform(housing_num_test)from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(housing_num_train) # fit on the TRAINING set only
X_train_scaled = scaler.transform(housing_num_train)
X_test_scaled = scaler.transform(housing_num_test)The math is simple: subtract the minimum, divide by (max - min)(max - min). That simplicity is
also its weakness — a single extreme outlier stretches the range and crushes every
other value toward 0.
Standardization
StandardScalerStandardScaler subtracts the mean (so the result always has zero mean) and divides
by the standard deviation (so the result has unit variance). It doesn’t bound values
to a fixed range, which can be an issue for algorithms that expect inputs in [0, 1][0, 1]
(e.g., some neural networks) — but it’s much less sensitive to outliers.
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(housing_num_train)
X_train_scaled = scaler.transform(housing_num_train)
X_test_scaled = scaler.transform(housing_num_test)from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(housing_num_train)
X_train_scaled = scaler.transform(housing_num_train)
X_test_scaled = scaler.transform(housing_num_test)Consider a district with a median income mistakenly recorded as 100100 (instead of a
normal range of roughly 0–15). Min-max scaling would compress every legitimate value
from 0–150–15 down into a tiny sliver of the 0–10–1 range near zero — one bad data point
distorts everything else. Standardization barely notices it, since a single outlier
barely moves the mean or standard deviation of a large dataset.
flowchart TD
A["Numeric feature, wide scale"] --> B{"Outliers present?"}
B -->|"Few / none"| C["MinMaxScaler
bounds to [0, 1]"]
B -->|"Yes, some extreme values"| D["StandardScaler
zero mean, unit variance"]
C --> E["fit() on TRAIN only,
transform() everywhere"]
D --> E
Fit on the training set, always
Just like the imputer, scalers must be fit on the training data only. Fitting on
the full dataset (including the test set) leaks test-set statistics — its min, max,
mean, or standard deviation — into the transformation, which is a subtle form of data
snooping. Once fit, use the same fitted scaler to transform()transform() the training set,
the test set, and any future data.
Visualize it
🧪 Try It Yourself
Exercise 1 – Min-max scale a feature
Exercise 2 – Standardize a feature
Exercise 3 – Fit on train, transform train and test
Next
Continue to Transformation Pipelines & Custom Transformers — imputing, encoding,
and scaling are each simple on their own; a PipelinePipeline chains them into one reusable
step.
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
