What Scaling Is and Is Not
Verify that unregularized ordinary least squares gives identical predictions before and after min-max scaling while regularized fits diverge.
The problem
After seeing min-max scaling flip nearest neighbors, it is tempting to assume feature scaling improves every model automatically. That assumption is half true and half myth. What actually changes when you fit a linear regression on min-max scaled features instead of on raw features ?
Test the four-row dataset [[10, 3], [20, 4], [40, 9], [50, 10]]. Fit ordinary least squares on scaled , compare its coefficients and row predictions against raw ordinary least squares, and then watch what happens the moment an ridge penalty enters the denominator.
Work through the numbers
First scale with and to obtain , whose mean is . Targets have mean . Centered vectors and have cross-product sum and sum of squares .
Dividing gives scaled slope and intercept , returning [2.7, 7.6]. Evaluating across predicts [2.7, 4.6, 8.4, 10.3].
Compare raw least squares on (mean ): cross-product sum is , sum of squares is , raw slope is , raw intercept is , and raw predictions are [2.7, 4.6, 8.4, 10.3] — identical to every decimal place!
The live model
Blue circles mark the four observations on scaled coordinate , the green line draws the scaled fit, and crimson squares show predictions from a raw-coordinate fit under the same penalty. At default penalty , coefficients are [2.7, 7.6] and both fits overlap completely at [2.7, 4.6, 8.4, 10.3]. Drag penalty to : scaled coefficients become [4.125, 4.75] (predicting [4.125, 5.3125, 7.6875, 8.875]), while raw ridge barely moves because is tiny next to . At penalty , scaled coefficients flatten to [5.3125, 2.375].
![Data points on [0, 1] with scaled OLS line [2.7, 7.6] matching raw OLS predictions [2.7, 4.6, 8.4, 10.3] at penalty 0.](../assets/ml/what-scaling-is-and-is-not--demo.png)
let penalty = scene.parameter("penalty", 0.0, 0.0, 1.375, 0.125);
let x = input.select(1, 0);
let y = input.select(1, 1);
let u = (x - x.min()).div(x.max() - x.min());
let uc = u - u.mean();
let yc = y - y.mean();
let w = (uc * yc).sum().div(uc.square().sum() + penalty);
let b = y.mean() - w * u.mean();
let answer = torch.cat(torch.list().add(b.reshape(1)).add(w.reshape(1)), 0);
out.set("scaled_ols_coeffs", answer);
{
"scaled_ols_coeffs": [
2.7,
7.6
]
}Watch the parameter change
Your turn
The starter program returns raw-unit ordinary least-squares coefficients ([0.8, 0.19]) instead of the [intercept, slope] fitted on min-max scaled coordinates ([2.7, 7.6]). Set answer to [intercept, slope] in min-max scaled feature units for any two-column [x, y] dataset; Check tests both this four-row table and unseen feature-target pairs.
![Starter line using raw-unit coefficients [0.8, 0.19] on a [0, 1] axis instead of scaled OLS [2.7, 7.6].](../assets/ml/what-scaling-is-and-is-not--exercise.png)
let x = input.select(1, 0);
let y = input.select(1, 1);
let xc = x - x.mean();
let yc = y - y.mean();
let w = (xc * yc).sum().div(xc.square().sum());
let b = y.mean() - w * x.mean();
let answer = torch.cat(torch.list().add(b.reshape(1)).add(w.reshape(1)), 0);
out.set("scaled_ols_coeffs", answer);
{
"scaled_ols_coeffs": [
0.7999999999999998,
0.19
]
}Min-max scale feature column 0 of input onto [0, 1] and return the ordinary least-squares [intercept, slope] fitted on that scaled feature and target column 1.
Read the result carefully
This experiment draws a sharp line across machine learning. Unregularized linear regression and axis-aligned decision trees are invariant to positive affine feature scaling: rescaling by span multiplies by () and shifts () so every prediction stays fixed.
By contrast, Euclidean -NN distances, / regularized models (ridge, lasso, weight decay), fixed-scale weight initializations paired with unscaled inputs, and shared-step gradient descent depend directly on coordinate scale. Scaling standardizes geometry, penalties, and optimization — never magically improving scale-invariant estimators.
While this book traced min-max scaling onto , standardization — subtracting the training mean and dividing by the training standard deviation via — is the usual alternative and is less squeezed by a single extreme value. Its and are pinned on training rows exactly like and ; libraries differ in dividing by or , so state which you use.
Recap
- On
[[10, 3], [20, 4], [40, 9], [50, 10]], unregularized OLS changes coefficients from[0.8, 0.19]to[2.7, 7.6]after min-max scaling while keeping predictions identical at[2.7, 4.6, 8.4, 10.3]. - Adding an penalty breaks invariance because the penalty taxes scaled slopes () far more heavily than wide-unit slopes ().
- Standardization (, with and pinned on training rows and divisor or stated explicitly) is the standard outlier-resistant alternative to min-max scaling.