← Math
Math

Normalization

Why did high-mileage cars get average offers?

Keep the meaning of scaled features consistent between training and serving.

Outcome

Scale a value with min-max and z-scores, keep the training scaler in serving, and send inputs the model never saw to a person.

Terminology (5)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Min-max scaling
(x − min) / (max − min): maps the training range onto 0 to 1.
Z-score Also standard score
(x − mean) / σ: how many standard deviations x is from the training mean.
Population standard deviation
√(Σ(x − mean)² / n), as scikit-learn’s StandardScaler uses; pandas’ std divides by n − 1 by default.
Training/serving skew
Inputs prepared differently in production than in training, so the model sees unfamiliar values.
Extrapolation
Predicting for inputs outside the range the model was trained on.

Why this matters

Models learn from scaled inputs, so the scaling is part of the model. Serving code that scales a little differently, with a different standard deviation formula or fresh statistics from each request, feeds the model numbers it never learned from. The predictions stay plausible, which is why these bugs survive for weeks.

This lesson assumes means, standard deviation and percentages.

What feature scaling changes

Feature scaling expresses a quantity relative to a reference dataset. Min-max scaling uses (x − min)/(max − min) to express position within its range. A z-score uses (x − mean)/σ to express distance from its mean in standard deviations.

Both ratios are dimensionless when the feature and its reference statistics use the same units. A zero range or zero standard deviation needs an explicit policy because the corresponding ratio is undefined. Scaling preserves useful comparisons only when the reference statistics and conventions match the model’s inputs.

01Min-max and z-scores

Min-max and z-scores

A used-car marketplace gives sellers an instant offer from a model that takes the odometer, among other features, as a z-score. After a rewrite of the serving code, a weekend of high-mileage trade-ins received offers as if they were average cars. Nothing crashed, and every number looked plausible.

The training cars’ odometers were 12, 35, 48, 60, 75, 90, 110 and 140 thousand km. Their range is 140 − 12 = 128 thousand km. Min-max scaling maps x to (x − 12)/128, so a car with 81 becomes 69/128 = 0.5391. The z-score measures distance from the mean, 71.25, in standard deviations, 38.87: the same car is (81 − 71.25)/38.87 ≈ 0.2509. Units cancel in both rescalings.

02Which σ?

Which σ?

Training divided the squared deviations by n, as scikit-learn’s StandardScaler does. The rewrite used pandas’ std, which divides by n − 1: σ becomes 41.55 and the 81 car’s z becomes 0.2347. Neither formula is wrong in general; serving must match training.

There is no standard scaler in Node.js; serving code usually loads the mean and σ that training saved as JSON next to the model.

03The saved scaler is part of the model

The saved scaler is part of the model

The small n-versus-n−1 mismatch does not explain an entire high-mileage batch looking average. Compare the saved training scaler with a scaler fitted only on the three weekend cars. Predict both lines.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const train = [12, 35, 48, 60, 75, 90, 110, 140]; // odometers the model was trained on, thousand km
const batch = [150, 160, 170]; // this weekend's cars

const fit = (v: number[]) => {
  const mean = v.reduce((s, x) => s + x, 0) / v.length;

  return [
    mean,
    Math.sqrt(v.reduce((s, x) => s + (x - mean) ** 2, 0) / v.length)
  ] as const;
};
const z = ([mean, sd]: readonly [number, number]) =>
  batch.map((x) => ((x - mean) / sd).toFixed(2)).join(" ");

console.log(z(fit(train))); // the saved scaler
console.log(z(fit(batch))); // refit on the batch

The rewrite standardised each incoming batch with its own mean and spread. Any batch then comes out centred on 0: the 160 car is exactly average among this weekend’s cars, so the model prices it as an average car. With the saved scaler it is 2.28 standard deviations above the training mean.

04Guard what the model never saw

Guard what the model never saw

Five cars against the training scaler, limit 2
OdometerzDecision
800.23instant offer
1351.64instant offer
1502.03by hand: above 140 and beyond 2
8−1.63by hand: below 12
60−0.29instant offer
The 8 car’s z is only −1.63. Why does it go to a person?

05What the guard does not say

What the guard does not say

06Fix the instant offer

Fix the instant offer

First reproduce the training scale for one odometer. Then preserve those fitted statistics across a serving batch, including a batch of one. The offer guard adds two distinct checks: training range and distance from the mean; constant training features require their own outcome.

In the frontend The offer page’s guard in Next.js and Svelte Optional

The seller’s offer page can apply the same guard before showing a price, and promise a manual quote instead of an extrapolated one.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// The seller's offer page: an automatic offer only inside the range the model was trained on.
type Scaler = { mean: number; sd: number; min: number; max: number; limit: number };

export function OfferStatus({
  odometer,
  scaler,
  offer
}: {
  odometer: number;
  scaler: Scaler;
  offer: (z: number) => number;
}) {
  const z = (odometer - scaler.mean) / scaler.sd;
  const outside =
    scaler.sd === 0 ||
    odometer < scaler.min ||
    odometer > scaler.max ||
    Math.abs(z) > scaler.limit;

  if (outside)
    return <p>We’ll check this one by hand and send an offer within a day.</p>;

  return <p>Your instant offer: €{offer(z).toLocaleString()}</p>;
}

Check this idea later
A request contains a single car. What z-score does refitting on the request give it?
Where should serving get the mean and σ?
Sources

Values above were run in Node 22, Go 1.27, Rust 1.99 and Apple clang 17. The marketplace and odometers are a worked model.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.