Fit Min and Max on Train
Fit a min-max scaler's lower bound, upper bound, and span strictly on role == 1 rows so held-out spikes cannot widen the denominator.
The problem
In Roles Come First, we separated training rows from held-out rows before inspecting feature values. A min-max scaler stores two numbers from its fitting pass—the lower bound tr_min and upper bound tr_max—along with their width tr_span = tr_max - tr_min, which becomes the denominator of every scaled coordinate.
If a held-out row leaks into that fitting step, both the zero point min and the denominator span shift in response to data the model is not supposed to have seen yet.
Work through the numbers
Consider six [role, x] rows in table = [[1, 12], [1, 20], [0, 52], [1, 32], [1, 24], [0, 4]]. Four training rows (role = 1) take values [12, 20, 32, 24], while two held-out rows (role = 0) sit outside that band at r2 = 52 and r5 = 4.
Fitting the min-max scaler strictly on role == 1 rows produces:
tr_min = min(12, 20, 32, 24) = 12tr_max = max(12, 20, 32, 24) = 32tr_span = 32 - 12 = 20
Packing those three numbers gives train_fit = [12, 32, 20]. Compare what happens when a pipeline fits across all six rows without masking role == 1: f_min drops to 4 (from held-out r5), f_max jumps to 52 (from held-out r2), and the leaked denominator widens to 52 - 4 = 48—more than double the honest training span of 20.
The live model
The green card shows train_fit = [tr_min, tr_max, tr_span]; the red card shows leaked_fit across all six rows. At default r2_x = 52, train_fit reads [12, 32, 20] while leaked_fit reads [4, 52, 48]. Drag r2_x down to 20 to watch leaked_fit shrink to [4, 32, 28], or up to 68 to watch leaked_fit balloon to [4, 68, 64], while train_fit never moves from [12, 32, 20].
![Train-only scaler fit [12, 32, 20] stays invariant while full-table leaked fit [4, 52, 48] stretches with held-out r2.](../assets/ml/fit-min-and-max-on-train--demo.png)
let r2_x = scene.parameter("r2_x", 52.0, 20.0, 68.0, 16.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().reshape(1);
let tr_max = torch.where(is_tr, x, lo).max().reshape(1);
let tr_span = tr_max - tr_min;
let answer = torch.cat(torch.list().add(tr_min).add(tr_max).add(tr_span), 0);
let f_min = x.min().reshape(1);
let f_max = x.max().reshape(1);
let f_sp = f_max - f_min;
let leaked = torch.cat(torch.list().add(f_min).add(f_max).add(f_sp), 0);
out.set("train_fit", answer).set("leaked_fit", leaked);
{
"train_fit": [
12,
32,
20
],
"leaked_fit": [
4,
52,
48
]
}Your turn
Right now the candidate green card matches the leaked red card at [4, 52, 48]: held-out rows r2 = 52 and r5 = 4 have widened the fitted denominator from 20 to 48. Fit tr_min, tr_max, and tr_span strictly from the training rows so answer stores the honest scaler state. Check tests three unseen tables, including one with all-negative training readings ([-18, -22, -20]).
![Fitting min, max, and span across the full table gives [4, 52, 48] instead of the train-only fit [12, 32, 20].](../assets/ml/fit-min-and-max-on-train--exercise.png)
let table = input;
let role = table.select(1, 0);
let x = table.select(1, 1);
let tr_min = x.min().reshape(1);
let tr_max = x.max().reshape(1);
let tr_span = tr_max - tr_min;
let answer = torch.cat(torch.list().add(tr_min).add(tr_max).add(tr_span), 0);
out.set("train_fit", answer);
{
"train_fit": [
4,
52,
48
]
}Fit the scaler state [tr_min, tr_max, tr_span] in answer strictly from the training split so held-out rows cannot move the bounds or denominator.
Once held-out rows are excluded from fitting, the top card settles at [12, 32, 20] while the full-table leak stays [4, 52, 48] below it.
Read the result carefully
Notice that tr_span equals the difference tr_max - tr_min = 20, never their sum (44), because min-max scaling measures how wide the training interval is from its lowest training point to its highest training point. On negative readings [-22, -20, -18], that width is still positive: -18 - (-22) = 4.
Recap
- A min-max scaler's fitted state is
[tr_min, tr_max]and its denominatortr_span = tr_max - tr_min. - Fitting strictly on
role == 1rows keeps[12, 32, 20]invariant no matter where held-out features land (20,36,52, or68). - Full-table fitting leaks held-out extremes (
4and52) into[4, 52, 48], inflating the scaling denominator from20to48.