Numeric Missing Uses the Mean
Fill missing numeric entries with the arithmetic mean of observed training rows, leaving held-out rows and missing slots out of the average.
The problem
Once missing cells are marked, a linear model still needs a finite number in every numeric slot before multiplying by a weight. For a continuous feature such as weekly study hours, standard mean imputation replaces each missing entry with the arithmetic mean of the observed training values.
Consider six student records stored as [role, hours], where role = 1 marks a training row, role = 0 marks a held-out test row, and -1 marks an unrecorded hour log: [1, 10], [1, 20], [1, -1], [1, 30], [0, 50], and [0, -1]. Two traps sit inside this small table: counting row 2 in the training denominator dilutes the sum, and peeking at test row 4 (50 hours) leaks held-out data into training preprocessing.
Work through the numbers
Build two zero-one masks first. The observed mask hours >= 0 equals [1, 1, 0, 1, 1, 0]. Multiplying by role ([1, 1, 1, 1, 0, 0]) isolates observed training rows: tr_obs = [1, 1, 0, 1, 0, 0].
Only rows 0, 1, and 3 survive tr_obs. Their hours are 10, 20, and 30, giving numerator 10 + 20 + 30 = 60 and denominator 1 + 1 + 0 + 1 + 0 + 0 = 3. Dividing 60 by 3 produces the training observed mean 20.
Two common mistakes give visibly different numbers on the same table. Dividing 60 by all 4 training rows yields 15, pulling imputed cells downward whenever training data has gaps. Including observed test row 4 (50) gives (10 + 20 + 30 + 50) / 4 = 27.5, letting held-out data shift the fill value. Applying the genuine training mean 20 to both missing rows (row 2 and row 5) produces [10, 20, 20, 30, 50, 20].
The live model
Each bar shows a student's final hour value after imputation: blue for observed training rows, green for observed held-out rows, and amber for imputed slots. At the default row3_hours = 30, the observed training mean is 20, so both row 2 and row 5 equal 20 while held-out row 4 stays 50. Drag row3_hours down to 15 to lower the training mean to 15, or up to 45 to raise both imputed bars to 25.

let row3_hours = scene.parameter("row3_hours", 30.0, 15.0, 45.0, 15.0);
let table = input.clone();
table.set(7, row3_hours);
let role = table.select(1, 0);
let hours = table.select(1, 1);
let obs = hours.ge(0.0).to(DType.F64);
let tr_obs = role * obs;
let tr_mean = (hours * tr_obs).sum().div(tr_obs.sum());
let fill = hours * 0.0 + tr_mean;
let answer = torch.where(hours.lt(0.0), fill, hours);
out.set("train_mean", tr_mean).set("filled_hours", answer);
{
"train_mean": 20,
"filled_hours": [
10,
20,
20,
30,
50,
20
]
}Your turn
In the starter run, missing rows 2 and 5 are filled with 15 hours even though the three recorded training students logged 10, 20, and 30 hours (mean 20). Unrecorded training rows are still diluting the average; repair tr_mean so answer becomes [10, 20, 20, 30, 50, 20].

let table = input.clone();
let role = table.select(1, 0);
let hours = table.select(1, 1);
let obs = hours.ge(0.0).to(DType.F64);
let tr_obs = role * obs;
let tr_mean = (hours * tr_obs).sum().div(role.sum());
let fill = hours * 0.0 + tr_mean;
let answer = torch.where(hours.lt(0.0), fill, hours);
out.set("train_mean", tr_mean).set("filled_hours", answer);
{
"train_mean": 15,
"filled_hours": [
10,
20,
15,
30,
50,
15
]
}Fill every missing hour slot with the mean of the observed training rows. Right now the starter fills missing slots with 15 instead of 20 because unrecorded training rows are still counted when averaging.
Once only observed training rows are counted, tr_mean rises from 15 to 20, and filled_hours becomes [10, 20, 20, 30, 50, 20].
Read the result carefully
Replacing missing training entries with 20 preserves the sample mean of the training column ((10 + 20 + 20 + 30) / 4 = 20), so centered linear algebra stays aligned around the same origin. However, repeating 20 shrinks sample spread and erases person-to-person variation on imputed rows.
When a numeric column has outliers that would pull the arithmetic mean away from typical observations, the training median is the usual choice instead, and when few rows are missing, dropping those rows is the alternative. Notice also that held-out row 5 receives the training mean 20, never an average computed on test rows: at prediction time a single incoming record has no test batch of its own.
Recap
- Compute a numeric fill value from rows that are both in the training split and observed.
- Divide the observed training sum by
tr_obs.sum()rather than the total training row count. - Apply that frozen training mean identically to missing training slots and missing held-out slots.