Apply the Train Fit to Test
Transform held-out rows with the frozen training min and span, allowing unseen features to land below 0 or above 1 without refitting.
The problem
After mapping training rows onto [0, 1] in Scale the Train Rows, we face held-out rows at prediction time. Beginners often panic when a held-out measurement lands outside [tr_min, tr_max] and either refit a fresh min-max scaler on the test set or clip the coordinate back into [0, 1].
Both reactions corrupt the coordinate system. A model trained on (x - tr_min) / tr_span expects 0 to mean tr_min and 1 to mean tr_max forever; any held-out reading above tr_max must map above 1, and any reading below tr_min must map below 0.
Work through the numbers
Five [role, x] rows sit in table = [[1, 10], [1, 20], [0, 40], [1, 30], [0, 6]]. Training rows (role = 1) are [10, 20, 30], so the fitted ruler is tr_min = 10 with tr_max = 30, and the span works out to 30 - 10 = 20. Held-out rows (role = 0) sit above and below that band at r2 = 40 and r4 = 6.
Applying the single frozen formula (x - 10) / 20 across all five rows gives:
r0(tr,x = 10):(10 - 10) / 20 = 0r1(tr,x = 20):(20 - 10) / 20 = 0.5r2(te,x = 40):(40 - 10) / 20 = 30 / 20 = 1.5r3(tr,x = 30):(30 - 10) / 20 = 1r4(te,x = 6):(6 - 10) / 20 = -4 / 20 = -0.2
Why are 1.5 and -0.2 essential? Because 40 sits half a training span (10 units) above 30, and 6 sits one-fifth of a training span (4 units) below 10. Refitting [te_min, te_max] = [6, 40] on test rows maps r2 = 40 to 1.0—telling the trained model that 40 equals 30.
The live model
Vertical gray guides mark 0.0 and 1.0. At default r2_x = 40, amber bar r2 extends past the 1.0 guide to 1.5, while r4 (x = 6) extends left of 0.0 in red to -0.2. Slide r2_x down to 20 (0.5) or 30 (1.0), then up to 50 (2.0): r2 crosses the 1.0 guide smoothly while r0 = 0, r1 = 0.5, r3 = 1, and r4 = -0.2 never budge.
![Applying the frozen [10, 30] ruler across all rows maps held-out r2 = 40 to 1.5 and held-out r4 = 6 to -0.2.](../assets/ml/apply-the-train-fit-to-test--demo.png)
let r2_x = scene.parameter("r2_x", 40.0, 20.0, 50.0, 10.0);
let table = input.clone();
table.select(0, 2).set(1, r2_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 tr_span = tr_max - tr_min;
let answer = (x - tr_min).div(tr_span);
out.set("tr_min", tr_min).set("tr_span", tr_span).set("scaled_all", answer);
{
"tr_min": 10,
"tr_span": 20,
"scaled_all": [
0,
0.5,
1.5,
1,
-0.2
]
}Your turn
Compare rows r2 (x = 40) and r3 (x = 30) on the starter canvas: both land on 1.0, while r4 (x = 6) lands on 0.0 alongside r0 (x = 10), because held-out rows are being rescaled against their own [6, 40] bounds. Measure every row against the frozen training ruler instead. Check tests two unseen tables whose held-out rows sit outside the training band.
![Refitting [te_min, te_max] on held-out rows maps r2 = 40 to 1.0 and r4 = 6 to 0.0, making 40 indistinguishable from 30.](../assets/ml/apply-the-train-fit-to-test--exercise.png)
let table = input;
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 is_te = role.eq(0.0);
let tr_min = torch.where(is_tr, x, hi).min();
let tr_max = torch.where(is_tr, x, lo).max();
let te_min = torch.where(is_te, x, hi).min();
let te_max = torch.where(is_te, x, lo).max();
let tr_sc = (x - tr_min).div(tr_max - tr_min);
let te_sc = (x - te_min).div(te_max - te_min);
let answer = torch.where(is_tr, tr_sc, te_sc);
out.set("scaled_all", answer);
{
"scaled_all": [
0,
0.5,
1,
1,
0
]
}Transform every row of input in answer using the single frozen min-max ruler fitted on the training split.
Measuring every row on the training ruler sends r2 past the 1.0 guide to 1.5 and r4 below 0.0 to -0.2, giving [0, 0.5, 1.5, 1, -0.2].
Read the result carefully
Notice how clean the production rule becomes: transform(x) does not even need role—it subtracts the stored scalar tr_min = 10 and divides by the stored scalar tr_span = 20, whether x arrives in a batch of a thousand rows or a single live request where a separate test span would divide by zero.
Recap
- Held-out and production rows must be transformed with the frozen
(x - tr_min) / (tr_max - tr_min)fitted on training rows. - Values below
0(-0.2atx = 6) and above1(1.5atx = 40) are honest coordinates, not bugs to clip away. - Refitting a scaler on test data makes
x = 40masquerade asx = 30(1.0) and breaks on single-row inference.