Train Mean Uses Train Rows
Fit a baseline target average strictly on role == 1 rows by dividing the masked training sum by the training count rather than total table rows.
The problem
Once The Role Column separates observations into training (role = 1) and held-out (role = 0) masks, the simplest predictive baseline estimates a constant target value from the training split. Computing that average carelessly fails in two opposite ways: calling .mean() on the entire column leaks held-out outcomes into the baseline, while zeroing held-out entries in the numerator without shrinking the denominator drags the estimate toward zero.
Honest fitting requires complete invariance to held-out targets: changing a held-out outcome by fifty points must change the fitted training mean by zero.
Work through the numbers
Take six [role, y] observations: [1, 10], [1, 14], [0, 36], [1, 18], [0, 44], and [1, 22]. Multiply y elementwise by tr_mask = [1, 1, 0, 1, 0, 1] to obtain [10, 14, 0, 18, 0, 22], whose masked numerator sum is 10 + 14 + 18 + 22 = 64.
Now check the denominator. Summing tr_mask counts the four training rows (n_train = 4), producing tr_mean = 64 / 4 = 16. Dividing 64 by all 6 table rows instead treats the two masked zeros as real zero-valued training targets and outputs 64 / 6 = 10.67. Conversely, averaging all six unmasked targets leaks 36 and 44 to produce 144 / 6 = 24.
The live model
Blue bars mark the four training targets (10, 14, 18, 22) and amber bars mark the two held-out targets (36, 44). At default r4_y = 44, the solid blue training-mean line sits at 16 while the red leaked six-row mean sits at 24. Drag r4_y down to 20 or up to 68: the amber bar on r4 follows the slider and the red leaked line moves from 20 to 28, yet the blue training-mean line stays locked at 16.

let r4_y = scene.parameter("r4_y", 44.0, 20.0, 68.0, 12.0);
let table = input.clone();
table.set(9, r4_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 = tr_mean.reshape(1);
out.set("train_mean", answer).set("leaked_mean", y.mean().reshape(1));
{
"train_mean": [
16
],
"leaked_mean": [
24
]
}Your turn
Why does the starter's blue line sit down at 10.67 when every training bar (10, 14, 18, 22) is at least 10? Zeroing the held-out targets kept the sum at 64, yet those excluded rows are still counted in the average. Correct the training-mean calculation so answer reports [16] across splits of any ratio.

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 tr_mean = (y * tr_mask).sum().div(float(table.size(0)));
let answer = tr_mean.reshape(1);
out.set("train_mean", answer);
{
"train_mean": [
10.666666666666666
]
}Compute the 1-element training target mean across role 1 rows alone so excluded held-out slots neither leak into the sum nor drag the average down.
Counting only the four training rows lifts tr_mean from 10.67 to 16, matching the true average of [10, 14, 18, 22].
Read the result carefully
Masked tensor arithmetic appears throughout machine learning whenever batches contain padded tokens, censored events, or held-out splits. Zeroing excluded entries in a product y * mask removes their values from .sum(), yet leaves their slots inside the tensor's shape. Pairing (y * mask).sum() with mask.sum() guarantees both numerator and denominator see the exact same subset of rows.
Recap
- Exclude held-out targets from the numerator via
(y * tr_mask).sum()(64). - Divide by
tr_mask.sum()(4), never bytable.size(0)(6) and never viay.mean()(24). - Verify invariance: perturbing any
role = 0target leaves a properly fitted training statistic completely unchanged.