← chapter

Normalization and standardization

Put every feature on a common scale before the model ever runs.

Distance doesn't care about meaning — only magnitude. Fix the ruler, not the data.

The problem

The raw spread

Eight features, one shared axis. Insulin and glucose stretch across; the rest are crushed near zero.

Three transforms, one line each

Min-max to a fixed [0, 1] box:

x=xxminxmaxxminx' = \dfrac{x - x_{\min}}{x_{\max} - x_{\min}}

Standardization — the z-score, mean 0 and std 1:

z=xμσz = \dfrac{x - \mu}{\sigma}

Robust scaling — median and IQR, outlier-proof:

x=xmedian(x)IQR(x)x' = \dfrac{x - \operatorname{median}(x)}{\operatorname{IQR}(x)}

The one rule you can get wrong

Watch scaling flip a prediction

Same query, same data. Only the ruler changes: raw space → standardized space.

Raw: 15 neighbors vote 4 diabetes / 11 no → no diabetes. Standardized: 8 / 7 → diabetes.

What scaling does to the model

Fixed k-NN, same split, four runs. Test accuracy — fraction of held-out patients called right (unitless, 0–1).

Raw 0.745 → standardized 0.771. No new information, just a change of units.

Takeaways