devnotes.

Min-Max Scaling (Normalization)

Min-Max scaling rescales values to a fixed range, usually 0 to 1. The minimum value becomes 0, the maximum becomes 1, and the shape of the distribution is preserved.

Formula

X_scaled = (X - X_min) / (X_max - X_min)

For a custom range [a, b]: X_scaled = a + (X - X_min) x (b - a) / (X_max - X_min).

Worked example

Ages [20, 30, 40, 60] with min 20 and max 60:

AgeCalculationScaled
20(20 - 20) / 400.00
30(30 - 20) / 400.25
40(40 - 20) / 400.50
60(60 - 20) / 401.00

In scikit-learn

from sklearn.preprocessing import MinMaxScaler
import pandas as pd

scaler = MinMaxScaler(feature_range=(0, 1))

num_cols = ["Age", "SibSp", "Parch", "Fare"]
X_train_mm = X_train.copy()
X_test_mm = X_test.copy()

X_train_mm[num_cols] = scaler.fit_transform(X_train[num_cols])   # learn min/max from train
X_test_mm[num_cols] = scaler.transform(X_test[num_cols])         # apply to test

print(X_train_mm[num_cols].describe().loc[["min", "max"]])

fit learns the min and max; transform applies them. fit_transform does both.

Custom range

scaler = MinMaxScaler(feature_range=(-1, 1))
scaled = scaler.fit_transform(X_train[["Age"]])

Manual implementation with pandas

col = X_train["Fare"]
manual = (col - col.min()) / (col.max() - col.min())

Effect of an outlier

One extreme value stretches the range and squeezes all normal values into a tiny interval.

import numpy as np

values = np.array([[10], [12], [11], [13], [12], [1000]])   # 1000 is an outlier
print(MinMaxScaler().fit_transform(values).round(3).ravel())
# [0.    0.002 0.001 0.003 0.002 1.   ]  -> normal values are squeezed near 0

Pros and cons

ProsCons
Bounded output rangeVery sensitive to outliers
Preserves the original distribution shapeNew data outside the training min/max falls outside [0, 1]
Good for neural networks and image pixelsNot centered around zero

When to use

  • Neural networks and algorithms that expect inputs in a bounded range.
  • Image data (pixels 0-255 divided to 0-1).
  • When you know there are no extreme outliers.
Min-Max Scaling (Normalization) - Feature Scaling | Data Preparation | DevNotes