Speed

What Train-Only Preprocessing Does and Does Not Say

Separate what train-only scaling guarantees—leak-free feature coordinates—from extrapolation error, saturating physics, and upstream target leakage.

The problem

Across this book we fixed split roles first, fitted tr_min and tr_max on role == 1 rows, mapped training rows onto [0, 1], applied the frozen ruler to held-out rows, and audited how Full-Data Fit Changes the Numbers. Once a pipeline passes every train-only check, what does that discipline guarantee—and what failure modes remain untouched?

Train-only preprocessing guarantees that held-out rows never alter the feature coordinates fed into training. It cannot guarantee that a linear rule stays accurate far outside [tr_min, tr_max] or rescue features that leak future outcomes in raw form.

Work through the numbers

Five [role, x, y] measurements sit in table = [[1, 10, 10], [1, 20, 20], [1, 30, 30], [0, 25, 25], [0, 50, 38]]. On training rows [10, 20, 30], tr_min = 10, tr_max = 30, and tr_span = 20, so x_sc = (x - 10) / 20 maps training inputs to [0, 0.5, 1] and the fitted linear model pred = 10 + 20 * x_sc matches [10, 20, 30] exactly.

Now score the two held-out rows (role = 0) with the frozen scaler and model:

  • r3 (x = 25, y = 25, inside [10, 30]): x_sc = 15 / 20 = 0.75, pred = 10 + 20 * 0.75 = 25, |25 - 25| = 0.
  • r4 (x = 50, y = 38, two spans above tr_min): x_sc = 40 / 20 = 2.0, pred = 10 + 20 * 2.0 = 50, |50 - 38| = 12.

Zero leakage occurred on r4, yet error jumped to 12 because physical yield began saturating above x = 30 while the linear formula kept climbing along 10 + 20 * x_sc.

The live model

Each row pairs pred = 10 + 20 * x_sc with |pred - y|. At default r4_y = 38, rows r0 through r3 turn green at 0 error while extrapolated r4 (x = 50, x_sc = 2) predicts 50 and misses 38 by 12. Slide r4_y down to 34 to see saturation widen the extrapolation miss to 16, or slide r4_y up to 50 to see the miss vanish to 0 only when reality stays strictly linear all the way to x = 50.

Leak-free scaling gives zero error on interpolated held-out r3 = 25, while extrapolated r4 = 50 (x_sc = 2.0) misses saturating y = 38 by 12.
let r4_y = scene.parameter("r4_y", 38.0, 34.0, 50.0, 4.0);
let table = input.clone();
table.select(0, 4).set(2, r4_y);
let role = table.select(1, 0);
let x = table.select(1, 1);
let y = table.select(1, 2);
let hi = x * 0.0 + x.max() + 1.0;
let lo = x * 0.0 + x.min() - 1.0;
let is_tr = role.eq(1.0);
let tr_min = torch.where(is_tr, x, hi).min();
let tr_max = torch.where(is_tr, x, lo).max();
let x_sc = (x - tr_min).div(tr_max - tr_min);
let pred = x_sc * 20.0 + 10.0;
let err = (pred - y).abs();
let answer = torch.stack(torch.list().add(pred).add(err), 1);
out.set("x_sc", x_sc).set("pred_and_err", answer);
{
  "x_sc": [
    0,
    0.5,
    1,
    0.75,
    2
  ],
  "pred_and_err": [
    [
      10,
      0
    ],
    [
      20,
      0
    ],
    [
      30,
      0
    ],
    [
      25,
      0
    ],
    [
      50,
      12
    ]
  ]
}

Your turn

Even the in-range rows r1, r2, and r3 miss their targets by 5, 10, and 7.5 in the starter (pred = [10, 15, 20, 17.5, 30]), because x_sc was scaled across the whole table including r4 = 50. Rescale x from the training split alone so answer pairs [pred, abs_err] cleanly across all five rows. Check grades on a four-row table and a five-row table.

Full-table scaling distorts every row's coordinate and breaks even in-range training and held-out predictions.
let table = input;
let role = table.select(1, 0);
let x = table.select(1, 1);
let y = table.select(1, 2);
let f_min = x.min();
let f_max = x.max();
let x_sc = (x - f_min).div(f_max - f_min);
let pred = x_sc * 20.0 + 10.0;
let err = (pred - y).abs();
let answer = torch.stack(torch.list().add(pred).add(err), 1);
out.set("x_sc", x_sc).set("pred_and_err", answer);
{
  "x_sc": [
    0,
    0.25,
    0.5,
    0.375,
    1
  ],
  "pred_and_err": [
    [
      10,
      0
    ],
    [
      15,
      5
    ],
    [
      20,
      10
    ],
    [
      17.5,
      7.5
    ],
    [
      30,
      8
    ]
  ]
}

Pair [pred, abs_err] in answer after rescaling x with bounds fitted strictly on the training rows before evaluating pred = 10 + 20 * x_sc.

Once x_sc uses the training bounds, rows r0 through r3 return to 0 error and only extrapolated r4 carries the honest 12-unit saturation miss in [[10, 0], [20, 0], [30, 0], [25, 0], [50, 12]].

Read the result carefully

Standardization—subtracting the training mean and dividing by the training standard deviation—follows the exact same rule as min-max scaling: fit both numbers on role == 1 rows and apply them unchanged to held-out rows.

Keep two remaining non-guarantees in mind. First, if a raw column already encodes post-outcome information (such as refund_issued when predicting churn), train-only scaling scales that leaked column faithfully; second, trying ten different scalers and picking whichever minimizes held-out error leaks test information through human selection.

Recap

  • Train-only preprocessing guarantees zero leakage of held-out feature bounds into training coordinates and weights.
  • Interpolated held-out points (r3 = 25, x_sc = 0.75) stay inside the training regime, whereas extrapolated points (r4 = 50, x_sc = 2.0) fail whenever true physics bends or saturates (|50 - 38| = 12).
  • Scaling on train alone cannot sanitize a column that already leaks future outcomes or prevent human peeking across repeated test-set evaluations.