Speed

Full-Data Fit Changes the Numbers

Compare train-only scaling and full-data scaling side by side to see how a single held-out outlier rewrites every training coordinate.

The problem

People sometimes shrug off preprocessing leakage as a technicality, guessing that fitting min and max on the whole dataset changes the numbers by a tiny rounding error. Sometimes that guess is right: on a large random split the held-out minimum and maximum usually sit close to the training ones, and the two fits nearly agree. But nothing guarantees it, and one extreme held-out row is enough to break it. Putting the two matrices side by side for such a row shows how large the change can be.

Because min-max scaling divides every row by max - min, a single extreme value in the held-out set inflates the denominator across the board and compresses every training row's coordinate—before gradient descent or linear regression even starts.

Work through the numbers

Four rows carry [role, x] pairs in table = [[1, 10], [1, 20], [1, 30], [0, 50]]: three training rows [10, 20, 30] and one held-out row r3 = 50.

Compare the two scaling pipelines row for row:

  • Honest train-only fit (tr_min = 10, tr_max = 30, tr_span = 20): divides (x - 10) by 20, yielding [0, 0.5, 1, 2].
  • Leaked full-data fit (f_min = 10, f_max = 50, f_span = 40): divides (x - 10) by 40, yielding [0, 0.25, 0.5, 1].

Look at training rows r1 (20) and r2 (30): leaking r3 = 50 cuts both training coordinates in half (0.5 -> 0.25 and 1.0 -> 0.5). Any regularized model (such as ridge or lasso) penalizes weight size directly, so shrinking training features in half doubles the slope required to make the same prediction and changes which weights survive regularization.

The live model

Green cells show column 0 (honest); red cells show column 1 (leaked). At default r3_x = 50, answer reads [[0, 0], [0.5, 0.25], [1, 0.5], [2, 1]]. Drag r3_x down to 30 to see both columns agree at [[0, 0], [0.5, 0.5], [1, 1], [1, 1]] when the held-out point stays inside [10, 30]. Drag r3_x up to 70 ([0, 0.17, 0.33, 1]) or 90 ([0, 0.13, 0.25, 1]): changing one held-out reading silently rewrites every training row in the red column while the green training rows never move.

Side-by-side honest vs leaked scaling: one held-out outlier r3 = 50 halves every positive training coordinate from [0, 0.5, 1] to [0, 0.25, 0.5].
let r3_x = scene.parameter("r3_x", 50.0, 30.0, 90.0, 20.0);
let table = input.clone();
table.select(0, 3).set(1, r3_x);
let role = table.select(1, 0);
let x = table.select(1, 1);
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 honest = (x - tr_min).div(tr_max - tr_min);
let f_min = x.min();
let f_max = x.max();
let leaked = (x - f_min).div(f_max - f_min);
let answer = torch.stack(torch.list().add(honest).add(leaked), 1);
out.set("honest_vs_leaked", answer);
{
  "honest_vs_leaked": [
    [
      0,
      0
    ],
    [
      0.5,
      0.25
    ],
    [
      1,
      0.5
    ],
    [
      2,
      1
    ]
  ]
}

Your turn

Both columns of the starter's audit table show [0, 0.25, 0.5, 1], hiding the 2x compression on r1 and r2 because honest currently copies the full-table scale. Compute genuine train-only coordinates for honest in column 0 alongside leaked in column 1. Check verifies the side-by-side (n, 2) matrix on two unseen tables with held-out spikes at both ends.

Duplicating leaked in both columns hides how full-data fitting halves training coordinates [0.5, 1.0] into [0.25, 0.5].
let table = input;
let role = table.select(1, 0);
let x = table.select(1, 1);
let f_min = x.min();
let f_max = x.max();
let leaked = (x - f_min).div(f_max - f_min);
let honest = leaked;
let answer = torch.stack(torch.list().add(honest).add(leaked), 1);
out.set("honest_vs_leaked", answer);
{
  "honest_vs_leaked": [
    [
      0,
      0
    ],
    [
      0.25,
      0.25
    ],
    [
      0.5,
      0.5
    ],
    [
      1,
      1
    ]
  ]
}

Build the (n, 2) comparison matrix answer pairing honest train-only min-max coordinates in column 0 with full-table leaked coordinates in column 1.

Putting train-only coordinates in column 0 reveals [[0, 0], [0.5, 0.25], [1, 0.5], [2, 1]] side by side.

Read the result carefully

That (n, 2) audit matrix gives a concrete sanity check for any preprocessing pipeline: perturb a held-out row (r3_x) and assert that zero training coordinates change. If a single training entry moves, held-out information has leaked into fit().

Recap

  • Full-data min-max scaling lets one held-out outlier (50) double the denominator from 20 to 40, halving training coordinates [0.5, 1] into [0.25, 0.5].
  • Compressing training coordinates alters feature variance and changes regularized model weights before training begins.
  • Perturbing a held-out row must leave every training row's preprocessed coordinate bit-for-bit identical.