Add Missingness Indicators
Pair a train-imputed column with a binary missingness indicator computed before filling so models can distinguish average observations from imputed gaps.
The problem
In Fill the Displayed Table, every -1 gap became a finite training statistic so arithmetic downstream could proceed. That repair introduces a quiet collision: once a missing cell is overwritten with the training mean, a row whose sensor failed looks numerically identical to a row genuinely measured at that average.
Suppose six rows arrive as [role, x]: four training rows ([1, 10], [1, 20], [1, -1], [1, 30]) and two held-out rows ([0, 44], [0, -1]). If sensor outages happen specifically during acute events, erasing -1 without recording where it occurred throws away predictive signal. Pairing the filled feature with a missingness indicator derived from Missing Cells Stay Visible preserves both finite numbers and the fact of absence.
Work through the numbers
First compute the training observed mean on the role = 1 rows (r0 = 10, r1 = 20, r3 = 30, skipping r2 = -1). Their sum is 60 across 3 observed rows, giving tr_mean = 20. Held-out row r4 = 44 stays excluded; leaking it would pull the average up to 104 / 4 = 26.
Before replacing -1, capture miss (1 where raw x < 0, 0 otherwise) giving [0, 0, 1, 0, 0, 1]. Filling raw -1 entries with 20 produces [10, 20, 20, 30, 44, 20]. Compare r1 and r2: in the filled column alone, both equal 20. Stacking [filled, miss] separates measured r1 as [20, 0] from unmeasured r2 and r5 as [20, 1], yielding the 6-by-2 matrix [[10, 0], [20, 0], [20, 1], [30, 0], [44, 0], [20, 1]].
The live model
The three columns show raw x, train-mean-filled x, and is_missing. At default r1_raw = 20, r1 sits at [20, 0] directly above r2 at [20, 1], showing how the indicator breaks the tie between an average measurement and a filled gap. Drag r1_raw left to -1 to make r1 missing too (tr_mean stays 40 / 2 = 20 and r1 flips to [20, 1]), or right to 35 to lift tr_mean to 75 / 3 = 25 so r1 becomes [35, 0] while r2 and r5 update to [25, 1].

let r1_raw = scene.parameter("r1_raw", 20.0, -1.0, 35.0, 3.0);
let table = input.clone();
table.set(3, r1_raw);
let role = table.select(1, 0);
let vals = table.select(1, 1);
let miss = vals.lt(0.0).to(DType.F64);
let tr_obs = role * vals.ge(0.0).to(DType.F64);
let tr_mean = (vals * tr_obs).sum().div(tr_obs.sum());
let filled = torch.where(vals.lt(0.0), vals * 0.0 + tr_mean, vals);
let answer = torch.stack(torch.list().add(filled).add(miss), 1);
out.set("train_mean", tr_mean).set("features", answer);
{
"train_mean": 20,
"features": [
[
10,
0
],
[
20,
0
],
[
20,
1
],
[
30,
0
],
[
44,
0
],
[
20,
1
]
]
}Your turn
Look at the second column of the starter output: every entry is 0, even on rows 2 and 5 where the sensor went offline, so measured row 1 and unmeasured row 2 both appear as [20, 0]. Restore the indicator column so offline rows carry 1 beside their filled value and answer reads [[10, 0], [20, 0], [20, 1], [30, 0], [44, 0], [20, 1]].

let table = input;
let role = table.select(1, 0);
let vals = table.select(1, 1);
let tr_obs = role * vals.ge(0.0).to(DType.F64);
let tr_mean = (vals * tr_obs).sum().div(tr_obs.sum());
let filled = torch.where(vals.lt(0.0), vals * 0.0 + tr_mean, vals);
let miss = filled.lt(0.0).to(DType.F64);
let answer = torch.stack(torch.list().add(filled).add(miss), 1);
out.set("train_mean", tr_mean).set("features", answer);
{
"train_mean": 20,
"features": [
[
10,
0
],
[
20,
0
],
[
20,
0
],
[
30,
0
],
[
44,
0
],
[
20,
0
]
]
}Pair the train-mean-filled feature column with a 0/1 missingness indicator that marks every row whose original reading was unrecorded.
Recording absence before imputation overwrites -1 restores 1 on rows 2 and 5, turning answer into [[10, 0], [20, 0], [20, 1], [30, 0], [44, 0], [20, 1]].
Read the result carefully
With [filled, miss] stacked side by side, a linear scorer w_1 * filled + w_2 * miss + b gains an explicit offset w_2 dedicated to missing entries, letting [20, 0] and [20, 1] receive different predictions whenever absence is informative. Creating miss requires zero fitted parameters—testing x < 0 applies identically to training and held-out rows without leakage. However, if a feature never goes missing during training (miss = 0 on all training rows), no model can learn a useful weight w_2 before test time.
Recap
- Mean imputation alone maps missing rows onto the exact coordinate of an average observed row (
20vs20). - Capture the
0/1missingness indicator from the raw column before imputation overwrites the-1sentinel. - Stack
[filled, is_missing]so downstream models receive finite values plus an explicit signal for missingness.