Scaling numeric features
Standardize, min–max or robust-scale a column, know which models care, and learn scaling values from training rows only.
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
Same data, new ruler
ReadScaling changes a column's units, not its shape or the order of its values.
| Method | Formula | Good for |
|---|---|---|
| Standardize | most scale-sensitive models | |
| Min–max | values that must sit between 0 and 1 | |
| Robust | columns with outliers | |
| Max-abs | sparse columns where zero must stay zero |
The Scale step in Prepare offers all four.
Who needs it?
PredictPredict 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?
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.
The rule of thumb
ReadModels 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.
See it on real numbers
ExploreWatch 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?
Penalty
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 to see the result.
Where scaling is learned
In the appScaling 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.
Check your understanding
CheckpointCheck 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.
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