Scaling and normalization
Put numeric features on comparable ranges so distance- and gradient-based models are not dominated by large units.
Lesson 13 of 6610 minBeginner
You will learn to
- Apply standard, min-max, robust and max-abs scaling
- Know which algorithms care about scale
- Fit scalers on training data only
Before this lesson
Intuition
Suppose you describe houses with two numbers: the area in square metres (around 100) and the price in euros (around 300,000). Ask a program which of two houses is "closer" to a third one. The price differs by thousands, the area by tens, so price decides everything and area is ignored. Not because price matters more, but because its unit produces bigger numbers.
Scaling rewrites numeric columns onto comparable ranges so that no feature dominates just because of its unit. It matters for:
- Distance-based models: k-nearest neighbours, clustering, support vector machines.
- Models trained by gradient descent, such as neural networks and, with regularization, linear and logistic regression: scale affects both the speed of training and how strongly each feature is penalized.
- Dimensionality reduction such as PCA, which looks for directions of large variance.
It does not matter for decision trees, random forests and gradient boosting, which compare a feature to a threshold, one feature at a time. Scaling them is harmless but unnecessary.
Picture it
Standard and min-max scaling are both pulled by the single value 100. The robust scaler anchors on the middle of the data instead.
The math
For a column with values , four common scalers (each is learned from the training rows):
Standard (z-score): subtract the mean , divide by the standard deviation . The result has mean 0 and standard deviation 1.
Min-max: map the smallest value to 0 and the largest to 1.
Robust: subtract the median, divide by the interquartile range .
Max-abs: divide by the largest absolute value, so the result lies in and zeros stay zeros (useful for sparse data).
Scaling is a linear map: it never changes the order of the values or the shape of the distribution. If a variable is skewed, scaling will not fix that; see Transformations.
Worked example
Incomes in thousands: 20, 30, 40, 50, 100. (Computed with scikit-learn's scalers.)
- Standard: mean 48, population standard deviation 27.86. The values become -1.01, -0.65, -0.29, 0.07, 1.87.
- Min-max: minimum 20, maximum 100. The values become 0, 0.125, 0.25, 0.375, 1.
- Robust: median 40, quartiles 30 and 50 so IQR 20. The values become -1, -0.5, 0, 0.5, 3.
Notice how the outlier affects each. With min-max, the four ordinary incomes are all below 0.4. With the robust scaler they are spread from -1 to 0.5, and the outlier is visibly far away, at 3.
Now the effect on distance. Take two people: (age 30, income 50,000) and (age 40, income 52,000). Unscaled, their distance is : the 10-year age gap is invisible. Scale each column to 0-1 using plausible ranges (age 20 to 60, income 20,000 to 100,000): the age difference becomes 10/40 = 0.25 and the income difference 2000/80000 = 0.025, so the distance is about 0.25. Now age drives the result, as a person would expect.
How mlflow does it
In Pipeline Studio, the Scale numbers group of the Choose... list includes Standardise, Scale from 0 to 1, Scale ignoring extremes and Scale by the maximum; Scale each row (each row to unit norm) is under Across several columns; Spread values evenly, Make it bell-shaped and Logarithm are in Change the shape of the values (see the next lesson).
- Recommendations. The rule-based recommender suggests Scale ignoring extremes for numeric columns where more than 5% of the rows are outliers, and Standardise for columns that look roughly normal (skewness within 0.5 and excess kurtosis within 1). Each recommendation names the numbers behind it.
- Fitted inside each fold. The scaler learns its statistics from the training rows of each fold, so the contamination described in Data leakage cannot happen.
- Quick train wizard. The Normalize toggle applies a z-score scaling to the numeric features.
- Time series. Scalers can also transform the target series of a forecasting pipeline; the change is undone on the forecasts.
Common mistakes
- Scaling before splitting. The mean and standard deviation then include the test rows. Use a pipeline step.
- Scaling tree models and expecting a gain. It costs nothing but changes almost nothing. Spend the effort elsewhere.
- Scaling one-hot columns. They are already 0 or 1. Scaling them rarely helps and can make coefficients harder to read.
- Using min-max with outliers. One extreme value squeezes everything else into a narrow band. Use the robust scaler, or deal with the outlier first (see Outliers).
- Applying a different scaler to new data. Predictions must use the numbers learned at training time. Never refit the scaler on the data you are predicting; mlflow keeps the fitted step inside the model so this is automatic.
Try it in mlflow
Add a scaler step
- Open Pipelines and pick a pipeline.
- In the Columns part, select the numeric columns, press Choose for N columns and, under Scale numbers, pick Standardise. Open a chip to see Before and after, then press Save version.
- Train a distance-based algorithm such as k-nearest neighbours with and without the step.
- Compare the two runs on the leaderboard.