Skip to content

Scaling numeric features

Standardize, min–max or robust-scale a column, know which models care, and learn scaling values from training rows only.

Applied ~9 min7 steps

You will be able to

  • Compute a standardized value by hand
  • Decide which models need scaling
  • Choose a scaler when the column has outliers

Read first

01

Same data, new ruler

Read

Scaling changes a column's units, not its shape or the order of its values.

MethodFormulaGood for
Standardize(x−xˉ)/s(x - \bar x) / smost scale-sensitive models
Min–max(x−xmin⁡)/(xmax⁡−xmin⁡)(x - x_{\min}) / (x_{\max} - x_{\min})values that must sit between 0 and 1
Robust(x−median)/IQR(x - \text{median}) / \text{IQR}columns with outliers
Max-absx/max⁡∣x∣x / \max\lvert x\rvertsparse columns where zero must stay zero

The Scale step in Prepare offers all four.

02

Who needs it?

Predict

Predict first

A column of incomes runs to six figures, and a column of ages to two. You don't scale. Which model is thrown off?

k-nearest neighborsA decision treeBoth equally
Reveal

The answer: k-nearest neighbors

k-nearest neighbors: it measures distance, so income swamps age. A decision tree splits on one column at a time and only uses the order of values, so a change of units leaves it unmoved.

03

The rule of thumb

Read

Models that use distances, gradients or penalties care about scale: k-nearest neighbors, support vector machines, neural networks, k-means, PCA, ridge and lasso. Tree-based models don't.

04

See it on real numbers

Explore

Watch what a penalty does to coefficients as it grows. Scaling is what makes this comparison fair across features.

DExplorable · Regularization paths

What happens to each coefficient as the penalty gets stronger?

Three of the eight predictors really matter. How many coefficients will be exactly zero at this penalty?

4
How sure?

Penalty

−0.50
0.30
60
2.0
this penaltybest

Blue paths are the three predictors that matter; gray are the five that don't. Below, training error (orange) and new-data error (blue).

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

05

Where scaling is learned

In the app

Scaling learns a mean and an SD from data, so where it learns matters.

  • In Predict, the option Scale numbers standardizes with the training rows' mean and SD, inside each fold. Use this when you're building a model.
  • In Prepare (⌘3), the Scale step learns once from the whole input. Use it for clustering, charts or description.

Scaling on all rows before you split gives the model a small preview of its test data.

06

Check your understanding

Checkpoint

Check yourself

A column has mean 50 and standard deviation 10. What is the standardized value of 70?

Show the answer

Answer: 2

(70 − 50) / 10. The value is two standard deviations above the mean.

Check yourself

A column has a few huge values, and you want to keep them. Which scaler stops them from squeezing everything else together?

Show the answer

Answer: b

Min–max sets the extremes to the edges, squashing the rest. Standardizing keeps the outliers and inflates the SD. Neither removes anything.

Check yourself

You standardize a right-skewed column. What is its skewness afterwards?

Show the answer

Answer: a

Standardizing shifts and stretches. To change the shape, you need a transformation such as Box–Cox, which has its own lesson.

07

Recap

Recap
  • Scaling changes units, not shape.
  • Distances, gradients and penalties care about scale; trees don't.
  • When modeling, learn scaling values from training rows only.

Concepts in this lesson