Score Held-Out Rows
Measure out-of-sample error row by row by taking |y - tr_mean| and masking with te_mask so training residuals stay excluded.
The problem
Once Freeze the Baseline broadcasts tr_mean across the table, every row has a prediction and therefore a residual y - tr_mean. Which rows belong in the evaluation score? Including training rows mixes in-sample fit with out-of-sample performance—a complex model that memorizes training rows perfectly would dilute its held-out mistakes with zeros.
Honest evaluation flips masks between the two stages: fit with tr_mask, score with te_mask, and take absolute values |y - tr_mean| so undershoots and overshoots never cancel one another out.
Work through the numbers
Consider six [role, y] observations: [1, 11], [1, 13], [0, 9], [1, 17], [0, 27], and [1, 19]. Summing the four training rows (11 + 13 + 17 + 19 = 60) and dividing by 4 yields tr_mean = 15.
Subtracting 15 from all six targets gives signed residuals [-4, -2, -6, 2, 12, 4]. Notice held-out row r2 sits 6 units below 15 (9 - 15 = -6) while held-out row r4 sits 12 units above 15 (27 - 15 = 12). Taking magnitudes produces [4, 2, 6, 2, 12, 4], and multiplying by te_mask = [0, 0, 1, 0, 1, 0] zeroes out all four training slots, leaving held_out_err = [0, 0, 6, 0, 12, 0].
The live model
The table displays each row's split badge, Target y, and Scored |err|. At default r2_y = 9, training rows stay masked at 0 in grey while held-out rows r2 and r4 record 6 and 12 in green. Drag r2_y left to 3 to widen r2's undershoot (|3 - 15| = 12), move to the middle stop 15 to watch r2's error hit 0 right at the frozen baseline, or drag right to 27 to see symmetric overshoot (|27 - 15| = 12) while tr_mean stays 15.

let r2_y = scene.parameter("r2_y", 9.0, 3.0, 27.0, 6.0);
let table = input.clone();
table.set(5, r2_y);
let role = table.select(1, 0);
let y = table.select(1, 1);
let tr_mask = role.eq(1.0).to(DType.F64);
let te_mask = role.eq(0.0).to(DType.F64);
let tr_mean = (y * tr_mask).sum().div(tr_mask.sum());
let answer = te_mask * (y - tr_mean).abs();
out.set("train_mean", tr_mean).set("held_out_err", answer);
{
"train_mean": 15,
"held_out_err": [
0,
0,
6,
0,
12,
0
]
}Your turn
Right now held_out_err reads [4, 2, 0, 2, 0, 4]: the four training rows are graded in red while held-out rows r2 and r4 sit ungraded at 0. Flip evaluation onto the held-out split so training slots remain 0 and held-out slots record their absolute distance from tr_mean.
![The starter scores training residuals [4, 2, 0, 2, 0, 4] instead of held-out residuals [0, 0, 6, 0, 12, 0].](../assets/ml/score-held-out-rows--exercise.png)
let table = input;
let role = table.select(1, 0);
let y = table.select(1, 1);
let tr_mask = role.eq(1.0).to(DType.F64);
let te_mask = role.eq(0.0).to(DType.F64);
let tr_mean = (y * tr_mask).sum().div(tr_mask.sum());
let answer = tr_mask * (y - tr_mean).abs();
out.set("train_mean", tr_mean).set("held_out_err", answer);
{
"train_mean": 15,
"held_out_err": [
4,
2,
0,
2,
0,
4
]
}Fit tr_mean on training rows, then output a per-row error vector that holds |y - tr_mean| on held-out rows and 0 on training rows.
Evaluating only the held-out split zeroes the four training slots and records [0, 0, 6, 0, 12, 0] on rows 2 and 4.
Read the result carefully
Look at both ingredients of te_mask * (y - tr_mean).abs(). First, .abs() turns -6 on r2 into +6; without .abs(), summing -6 and +12 on the next page would cancel half of r4's error and pretend a wild swing of -6 and +12 was an average miss of +3. Second, te_mask enforces the firewall built in The Role Column: tr_mask touches tr_mean, te_mask touches held_out_err, and no row ever wears both hats.
Recap
- Fit
tr_mean(15) usingtr_mask, then switch tote_maskwhen evaluating residuals. - Take absolute differences
|y - tr_mean|before aggregating so negative (-6) and positive (+12) errors both count positively. - Zeroing training slots in
held_out_err([0, 0, 6, 0, 12, 0]) prepares the exact numerator for held-out MAE.