Feature Scaling

Two columns measured in different units, and a model that can only see the larger one. Switch the scaler and watch the accuracy move.

Overview

The problem is the units

The data here has two columns: age, spanning roughly 25 to 55, and income, spanning roughly 20,000 to 70,000. They describe a person about equally well.

Set the scaler to None and look at the readout. The income range is around a thousand times the age range. Now consider what k-NN does — it finds the nearest points by Euclidean distance:

distance^2 = (age1 - age2)^2 + (income1 - income2)^2

A ten-year age difference contributes 100 to that sum. A ten-pound income difference contributes 100 as well. A five-thousand-pound difference contributes twenty-five million.

The age term is not merely less important. It is numerically irrelevant: any plausible age difference is lost in the rounding of the income term. The model is effectively one-dimensional, and nobody asked for that.

Feature Scaling

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

Any model that measures distance is sensitive to units. k-NN, SVM with an RBF kernel, k-means and PCA all are.
StandardScaler subtracts the mean and divides by the standard deviation. Outliers survive it.
MinMaxScaler maps to [0, 1]. One extreme value squashes everything else into a corner.
Trees do not care. A split asks whether a value exceeds a threshold, and that answer is unchanged by rescaling.

Feature Scaling

Why a column measured in pounds drowns out a column measured in years, and which models care.

What the scalers do

Switch to StandardScaler. Each column has its mean subtracted and is divided by its standard deviation, so both end up centred on 0 with a spread of about 1. The scatter becomes roughly circular, both columns contribute comparably, and the accuracy in the readout moves.

z = (x - mean) / std

Switch to MinMaxScaler. Each column is mapped onto [0, 1] using its minimum and maximum:

x' = (x - min) / (max - min)

Both fix the units problem. They differ in how they handle extremes, and that is the basis for choosing.

StandardScalerMinMaxScaler
Output rangeunbounded, roughly −3 to 3exactly [0, 1]
Usesmean, standard deviationminimum, maximum
One extreme valueshifts the mean a littlecompresses everything else
Good formost things, especially with outliersbounded inputs, image pixels, neural nets

MinMax's weakness is worth stating plainly. Both statistics it uses are the most outlier-sensitive numbers available. One salary of ten million maps to 1.0 and crushes every real salary into the bottom half of a per cent of the range.

Which models care

Sensitive, because they measure distance or magnitude:

  • k-NN and k-means — distance is the entire algorithm
  • SVM with an RBF or polynomial kernel — the kernel is a distance
  • PCA — it finds directions of maximum variance, and unscaled variance is dominated by whichever column has the biggest units
  • Neural networks — not for correctness, but gradients on wildly different scales make optimisation slow and unstable
  • Ridge and Lasso — the penalty is on coefficient size, and coefficient size depends on the units of its column

Not sensitive:

  • Decision trees, random forests, gradient boosting

That last group surprises people, and the reason is worth having. A tree split asks is income > 45000. Rescale the column and the question becomes is income' > 0.53, which partitions the rows into exactly the same two groups. Any monotonic transformation leaves every possible split unchanged, so the tree is identical. Scaling before a random forest is not wrong; it is simply a no-op.

The rule that matters more than the choice

Fit the scaler on the training data only, then apply it to the test data without refitting.

The mean and standard deviation are learned parameters. Computing them from the whole dataset lets information about the test set reach the model before it is evaluated, and the reported score stops being an estimate of anything. That is [data leakage](data_leakage.html), it is the most common serious mistake in applied machine learning, and it is why scalers belong inside a [pipeline](ml_pipelines.html) rather than being applied by hand.

Where it goes wrong

Fitting the scaler on everything. The number you report goes up and the model does not get better.

Scaling the target variable and forgetting to invert it. Predictions come back in scaled units and the error metric is meaningless.

MinMax on data with outliers. The real data ends up in a sliver of the range.

Scaling one-hot columns. They are already 0 and 1. StandardScaler turns them into two arbitrary values and makes the model harder to read for no gain.

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why is k-NN affected by feature scaling but a decision tree is not?

  2. What is MinMaxScaler's main weakness compared with StandardScaler?

  3. Where must a scaler's mean and standard deviation come from?

Cheat sheet

Feature Scaling

The data here has two columns: age, spanning roughly 25 to 55, and income, spanning roughly 20,000 to 70,000. They describe a person about equally well.

MACHINE LEARNING · vizlearn.in/machine_learning/feature_scaling.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.