Speed

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 y^=b+wu\hat{y} = b + w u on min-max scaled features uu instead of y^=braw+wrawx\hat{y} = b_{\mathrm{raw}} + w_{\mathrm{raw}} x on raw features xx?

Test the four-row dataset [[10, 3], [20, 4], [40, 9], [50, 10]]. Fit ordinary least squares on scaled u∈[0,1]u \in [0, 1], compare its coefficients and row predictions against raw ordinary least squares, and then watch what happens the moment an L2L_2 ridge penalty enters the denominator.

Work through the numbers

First scale x=[10,20,40,50]x = [10, 20, 40, 50] with lo=10\mathrm{lo} = 10 and span=40\mathrm{span} = 40 to obtain u=[0,0.25,0.75,1.0]u = [0, 0.25, 0.75, 1.0], whose mean is μu=0.5\mu_u = 0.5. Targets y=[3,4,9,10]y = [3, 4, 9, 10] have mean μy=6.5\mu_y = 6.5. Centered vectors uc=[−0.5,−0.25,0.25,0.5]u_c = [-0.5, -0.25, 0.25, 0.5] and yc=[−3.5,−2.5,2.5,3.5]y_c = [-3.5, -2.5, 2.5, 3.5] have cross-product sum 1.75+0.625+0.625+1.75=4.751.75 + 0.625 + 0.625 + 1.75 = 4.75 and sum of squares 0.25+0.0625+0.0625+0.25=0.6250.25 + 0.0625 + 0.0625 + 0.25 = 0.625.

Dividing 4.75/0.625=7.64.75 / 0.625 = 7.6 gives scaled slope w=7.6w = 7.6 and intercept b=6.5−7.6(0.5)=2.7b = 6.5 - 7.6(0.5) = 2.7, returning [2.7, 7.6]. Evaluating 2.7+7.6u2.7 + 7.6 u across u=[0,0.25,0.75,1.0]u = [0, 0.25, 0.75, 1.0] predicts [2.7, 4.6, 8.4, 10.3].

Compare raw least squares on x=[10,20,40,50]x = [10, 20, 40, 50] (mean 3030): cross-product sum is 190190, sum of squares is 1,0001{,}000, raw slope is 0.19=7.6/400.19 = 7.6 / 40, raw intercept is 0.8=2.7−7.6⋅10/400.8 = 2.7 - 7.6 \cdot 10 / 40, and raw predictions 0.8+0.19x0.8 + 0.19 x 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 u∈[0,1]u \in [0, 1], the green line draws the scaled fit, and crimson squares show predictions from a raw-coordinate fit under the same L2L_2 penalty. At default penalty =0.0= 0.0, coefficients are [2.7, 7.6] and both fits overlap completely at [2.7, 4.6, 8.4, 10.3]. Drag penalty to 0.3750.375: scaled coefficients become [4.125, 4.75] (predicting [4.125, 5.3125, 7.6875, 8.875]), while raw ridge barely moves because 0.3750.375 is tiny next to 1,0001{,}000. At penalty =1.375= 1.375, 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.
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 u∈[0,1]u \in [0, 1] ([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].
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 xx by span 4040 multiplies ww by 4040 (0.19→7.60.19 \to 7.6) and shifts bb (0.8→2.70.8 \to 2.7) so every prediction stays fixed.

By contrast, Euclidean kk-NN distances, L1L_1/L2L_2 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 [0,1][0, 1], standardization — subtracting the training mean μ\mu and dividing by the training standard deviation σ\sigma via z=x−μσz = \frac{x - \mu}{\sigma} — is the usual alternative and is less squeezed by a single extreme value. Its μ\mu and σ\sigma are pinned on training rows exactly like lo\mathrm{lo} and hi\mathrm{hi}; libraries differ in dividing by nn or n−1n - 1, 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 L2L_2 penalty breaks invariance because the penalty taxes scaled slopes (7.67.6) far more heavily than wide-unit slopes (0.190.19).
  • Standardization (z=x−μσz = \frac{x - \mu}{\sigma}, with μ\mu and σ\sigma pinned on training rows and divisor nn or n−1n - 1 stated explicitly) is the standard outlier-resistant alternative to min-max scaling.