Speed

Freeze the Baseline

Broadcast the fitted training mean unchanged across both training and held-out rows instead of refitting a separate summary on held-out targets.

The problem

In Train Mean Uses Train Rows, we isolated the training target average as a single scalar. Turning that scalar into row-by-row predictions exposes a subtle trap: what should the baseline predict on held-out rows? Beginners sometimes compute a fresh mean on the held-out split and plug it into held-out rows, or multiply the prediction vector by tr_mask and leave zeros on held-out rows.

Both break evaluation. Freezing a model means locking every learned parameter once training finishes and applying that identical formula to unseen held-out rows without peeking at their targets.

Work through the numbers

Examine six [role, y] rows: [1, 9], [1, 13], [0, 24], [1, 17], [0, 30], and [1, 21]. Across the four training rows (r0, r1, r3, r5), the target sum is 9 + 13 + 17 + 21 = 60, giving tr_mean = 60 / 4 = 15. Across the two held-out rows (r2, r4), the target sum is 24 + 30 = 54, so a refitted held-out mean would be 54 / 2 = 27, and the leaked six-row mean would be 114 / 6 = 19.

Freezing tr_mean = 15 broadcasts 15 into every slot of the prediction vector: [15, 15, 15, 15, 15, 15]. Held-out rows r2 (y = 24) and r4 (y = 30) receive 15, not 27 and not 0.

The live model

The table pairs each row's split badge (train in blue, held-out in amber) and observed Target y with Frozen Pred. At default r5_y = 21, all six rows show 15 in green because one frozen constant serves the entire dataset. Drag training target r5_y left to 9 and tr_mean drops to 48 / 4 = 12, moving all six predictions together to 12; drag r5_y right to 33 and tr_mean rises to 72 / 4 = 18, moving all six predictions together to 18.

Six rows comparing observed targets y against the frozen baseline vector filled everywhere with the training mean 15.
let r5_y = scene.parameter("r5_y", 21.0, 9.0, 33.0, 4.0);
let table = input.clone();
table.set(11, r5_y);
let role = table.select(1, 0);
let y = table.select(1, 1);
let tr_mask = role.eq(1.0).to(DType.F64);
let tr_mean = (y * tr_mask).sum().div(tr_mask.sum());
let answer = y * 0.0 + tr_mean;
out.set("train_mean", tr_mean).set("baseline_pred", answer);
{
  "train_mean": 15,
  "baseline_pred": [
    15,
    15,
    15,
    15,
    15,
    15
  ]
}

Your turn

Look at rows 2 and 4 of the starter output: instead of carrying forward the training estimate 15, they predict 27—an average computed directly from the held-out answers being tested. Lock a single baseline learned on training rows across all six positions so answer reads [15, 15, 15, 15, 15, 15].

The starter refits a separate mean 27 on held-out rows instead of freezing 15 everywhere.
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 te_mean = (y * te_mask).sum().div(te_mask.sum());
let answer = tr_mask * tr_mean + te_mask * te_mean;
out.set("train_mean", tr_mean).set("baseline_pred", answer);
{
  "train_mean": 15,
  "baseline_pred": [
    15,
    15,
    27,
    15,
    27,
    15
  ]
}

Produce the baseline prediction vector across all rows using only the scalar learned on the training split, without peeking at held-out targets.

Carrying the training estimate across every slot turns all six entries green at [15, 15, 15, 15, 15, 15], invariant to held-out targets.

Read the result carefully

Notice the asymmetry between The Role Column during fitting and during prediction. Fitting uses tr_mask to block held-out rows from entering tr_mean. Prediction removes the mask so the frozen model produces an output on every row—especially held-out rows, where we are about to measure generalization error on the next page. Refitting on held-out data is equivalent to grading an exam after copying the answer key into your guesses: held-out residuals shrink artificially because the baseline cheated.

Recap

  • Fit the baseline parameter (tr_mean = 15) using role = 1 rows only.
  • Freeze that scalar and broadcast it across all N rows ([15, 15, 15, 15, 15, 15]) so held-out rows receive genuine out-of-sample predictions.
  • Never compute a separate te_mean (27) or zero out held-out predictions (0) when scoring a model.