Part VII — Model Evaluation, Validation & Feature Engineering · Chapter 5

Feature scaling & transformation

Hook

The last chapter tuned one learning rate for one weight. Real models have many weights, one per feature — and if two features sit on wildly different scales, there may be no single learning rate that treats both of them fairly.

Intuition
bold: distance from true w1 (tiny-scale feature) — faint: distance from true w2 (huge-scale feature)

Two features, two weights, one shared learning rate — exactly like every gradient descent so far in this course, except now x1x_1 ranges across ±1\pm 1 and x2x_2 ranges across ±100\pm 100. Step through learning rates and watch the faint curve (w2w_2, the huge-scale feature) and the bold curve (w1w_1, the tiny-scale feature) refuse to cooperate.

Formalize

For linear regression, the curvature of the loss along each weight's axis scales with that feature's variance:

2Lwj2ixij2\frac{\partial^2 \mathcal{L}}{\partial w_j^2} \propto \sum_i x_{ij}^2
  • L\mathcal{L} — the loss function being minimized.
  • wjw_j — the weight (parameter) for feature jj.
  • xijx_{ij} — the value of feature jj for training example ii.
  • jj — index running over features.
  • ii — index running over training examples.
  1. Curvature scales with the square of a feature's range

    A feature on a 100x larger scale produces roughly 1002=10,000×100^2 = 10{,}000\times more curvature along its own weight axis — small differences in scale become huge differences in curvature.

  2. One shared rate must satisfy the worst axis

    A single learning rate must stay below 2/curvature2/\text{curvature} everywhere to avoid diverging, so the large-scale feature forces the whole learning rate down — including for every feature that didn't need it to be that small.

Play

Train on the raw features for 20 steps at the rate that's safe for the huge-scale feature, and w1w_1 has barely moved — its error is still nearly its full starting distance. Rescale x2x_2 down to x1x_1's range first, and the same learning rate that solved the tiny-scale weight now solves both weights exactly, in two steps instead of twenty.

Worked example

y=w1x1+w2x2y = w_1 x_1 + w_2 x_2, true weights w1=3w_1{=}3, w2=0.02w_2{=}0.02, with x1{±1}x_1 \in \{\pm 1\} and x2{±100}x_2 \in \{\pm 100\}:

  1. The same starting point, wildly different gradients

    With four points (x1,x2,y)(x_1,x_2,y): (1,100,5)(1,100,5), (1,100,1)(1,-100,1), (1,100,1)(-1,100,-1), (1,100,5)(-1,-100,-5), all predictions are 00 at w1=w2=0w_1{=}w_2{=}0, so each residual is 0y=y0-y=-y:

    • L/w1=24(residual×x1)=24[(5)(1)+(1)(1)+(1)(1)+(5)(1)]=24(12)=6\partial\mathcal{L}/\partial w_1 = \frac24\sum(\text{residual}\times x_1) = \frac24\big[(-5)(1)+(-1)(1)+(1)(-1)+(5)(-1)\big] = \frac24(-12) = -6
    • L/w2=24(residual×x2)=24[(5)(100)+(1)(100)+(1)(100)+(5)(100)]=24(800)=400\partial\mathcal{L}/\partial w_2 = \frac24\sum(\text{residual}\times x_2) = \frac24\big[(-5)(100)+(-1)(-100)+(1)(100)+(5)(-100)\big] = \frac24(-800) = -400

    Over 66x steeper for w2w_2, purely because x2x_2's values are 100x larger. The loss surface is a long, narrow valley: gentle along w1w_1's axis, brutally steep along w2w_2's.

  2. Safe for the steep axis, frozen on the gentle one
    • η=0.00005\eta=0.00005 is exactly the rate that lands w2w_2 on its true value in one step.
    • Using that same rate for w1w_1 over 20 steps leaves it at just 0.0060.006 — still 2.992.99 away from its true value of 33.
    • Double the rate to η=0.0001\eta=0.0001 and w2w_2 stops converging altogether, bouncing between 00 and 0.040.04 forever.
  3. Scaling turns two curvatures into one

    Dividing x2x_2 by 100100 shrinks its range to match x1x_1's, and its true weight becomes 0.02×100=20.02\times100=2. Both features now produce identical curvature. η=0.5\eta=0.5 — the same "magic" rate from the last chapter — lands both w1=3w_1{=}3 and w2=2w_2{=}2 exactly, in a single step.

Checkpoint

Find the learning rate that's safe for w2 but leaves w1 more than 2 away from its true value after the same 20 steps.

Pick a learning rate to try it
Summary
curvaturejixij2one learning rate can’t serve every feature unless every feature is on the same scale\text{curvature}_j \propto \sum_i x_{ij}^2 \quad\Rightarrow\quad \text{one learning rate can't serve every feature unless every feature is on the same scale}

Grid search from the last chapter can find the best single learning rate, but it can't fix a loss surface shaped like a canyon — it can only pick the least-bad compromise. Standardizing every feature to a comparable scale before training removes the problem instead of compromising around it. This closes Model Evaluation & the Practical Workflow. The capstone puts every metric from this part — accuracy, F1, AUC, cross-validation, tuning, scaling — against one real pipeline at once.