Skip to content

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):

MinMaxScaler
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)
MinMaxScaler
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.

StandardScaler
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)
StandardScaler
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 015015 down into a tiny sliver of the 0101 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.

diagram Choosing a scaler mermaid
MinMaxScaler bounds to a fixed range; StandardScaler is robust to outliers.

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

sketch Before and after StandardScaler p5.js
Same 8 values, on a raw scale up top - watch each one slide down to its standardized position (zero mean, unit variance), then slide back so you can see the round trip.

🧪 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 coffee

Was this page helpful?

Let us know how we did