Fill the Displayed Table
Apply numeric mean imputation and categorical mode imputation column by column from training rows to assemble a complete feature matrix.
The problem
Real tables rarely hold a single column type or put all missing entries in the same row. Suppose seven building-sensor logs arrive as [role, temp, zone]: four training rows ([1, 12, 1], [1, -1, 1], [1, 18, -1], [1, 24, 0]) and three held-out rows ([0, -1, 0], [0, 26, -1], [0, 30, 0]).
Temperature is continuous in degrees Celsius inside a climate-controlled building that never drops below zero (see the sentinel caveat in Missing Cells Stay Visible). Zone is a discrete floor code (0, 1, or 2), and -1 marks an unrecorded cell in either column.
Dropping any row with a -1 would discard two of four training rows and two of three held-out rows. Instead, per-column imputation fits the training mean on temperature and the training mode on zone, then fills only the negative sentinel slots while leaving every observed measurement untouched.
Work through the numbers
Fit each column's statistic separately on the four training rows (role = 1). For temperature, row 1 is missing, leaving observed training readings 12, 18, and 24 on rows 0, 2, and 3. Their sum is 54 across 3 observed rows, giving temp_mean = 18. Including held-out temperatures 26 and 30 would leak held-out data and raise that average to 110 / 5 = 22.
For zone, row 2 is missing, leaving observed training codes 1, 1, and 0 on rows 0, 1, and 3. The training counts across [0, 1, 2] are [1, 2, 0], giving zone_mode = 1. Including held-out zones 0 and 0 would shift the counts to [3, 2, 0] and flip the mode to 0, whereas averaging observed training codes would yield 2 / 3 (0.67).
Replacing -1 in column 0 with 18 (rows 1 and 4) and -1 in column 1 with 1 (rows 2 and 5) assembles the 7-by-2 matrix [[12, 1], [18, 1], [18, 1], [24, 0], [18, 0], [26, 1], [30, 0]].
The live model
The left grid displays raw [temp, zone] with -1 gaps in red; the right grid shows the filled 7-by-2 matrix with imputed slots in green. At default row3_temp = 24, temp_mean is 18 and zone_mode is 1. Lower row3_temp to 12 to move temp_mean and filled cells (1, 0) and (4, 0) down to 14, or raise row3_temp to 36 to move them up to 22 while zone_mode stays 1.

let row3_temp = scene.parameter("row3_temp", 24.0, 12.0, 36.0, 6.0);
let table = input.clone();
table.set(10, row3_temp);
let role = table.select(1, 0);
let temp = table.select(1, 1);
let zone = table.select(1, 2);
let t_obs = role * temp.ge(0.0).to(DType.F64);
let t_mean = (temp * t_obs).sum().div(t_obs.sum());
let z0 = (role * zone.eq(0.0).to(DType.F64)).sum().reshape(1);
let z1 = (role * zone.eq(1.0).to(DType.F64)).sum().reshape(1);
let z2 = (role * zone.eq(2.0).to(DType.F64)).sum().reshape(1);
let z_counts = torch.cat(torch.list().add(z0).add(z1).add(z2), 0);
let z_mode = float(z_counts.argmax(0).item_int());
let f_temp = torch.where(temp.lt(0.0), temp * 0.0 + t_mean, temp).unsqueeze(1);
let f_zone = torch.where(zone.lt(0.0), zone * 0.0 + z_mode, zone).unsqueeze(1);
let answer = torch.cat(torch.list().add(f_temp).add(f_zone), 1);
out.set("temp_mean", t_mean).set("zone_mode", z_mode).set("filled", answer);
{
"temp_mean": 18,
"zone_mode": 1,
"filled": [
[
12,
1
],
[
18,
1
],
[
18,
1
],
[
24,
0
],
[
18,
0
],
[
26,
1
],
[
30,
0
]
]
}Your turn
Column 0 already fills missing temperatures with 18, yet column 1 writes the fractional value 0.67 into rows 2 and 5 where a valid floor code belongs. Repair the zone fill rule so both columns use their proper training summary and answer matches [[12, 1], [18, 1], [18, 1], [24, 0], [18, 0], [26, 1], [30, 0]].

let table = input.clone();
let role = table.select(1, 0);
let temp = table.select(1, 1);
let zone = table.select(1, 2);
let t_obs = role * temp.ge(0.0).to(DType.F64);
let t_mean = (temp * t_obs).sum().div(t_obs.sum());
let z_obs = role * zone.ge(0.0).to(DType.F64);
let z_fill = (zone * z_obs).sum().div(z_obs.sum());
let f_temp = torch.where(temp.lt(0.0), temp * 0.0 + t_mean, temp).unsqueeze(1);
let f_zone = torch.where(zone.lt(0.0), zone * 0.0 + z_fill, zone).unsqueeze(1);
let answer = torch.cat(torch.list().add(f_temp).add(f_zone), 1);
out.set("temp_mean", t_mean).set("zone_fill", z_fill).set("filled", answer);
{
"temp_mean": 18,
"zone_fill": 0.6666666666666666,
"filled": [
[
12,
1
],
[
18,
1
],
[
18,
0.6666666666666666
],
[
24,
0
],
[
18,
0
],
[
26,
0.6666666666666666
],
[
30,
0
]
]
}Assemble the complete two-column feature matrix by filling missing temperatures with the training mean and missing floor zones with the most frequent training zone (codes 0, 1, and 2), keeping every observed reading intact.
Once z_fill uses the most common training zone 1 instead of 0.67, rows 2 and 5 become [18, 1] and [26, 1], completing the 7-by-2 matrix.
Read the result carefully
Column-by-column imputation needs its own mask per column: row 1 had a missing temperature alongside a valid zone 1, and row 2 had a valid temperature 18 alongside a missing zone. Using a single row-level mask or overwriting observed slots destroys real readings. Notice also that both replacement constants (18 and 1) are frozen once from training rows and reused verbatim on held-out rows 4 and 5.
Recap
- Fit each column's replacement rule according to its data type: training observed mean for continuous features and training observed mode for discrete codes.
- Build a separate missingness mask per column so an unrecorded cell in one feature never overwrites a valid reading in another.
- Concatenate the filled columns in fixed order to produce a complete numeric matrix across both splits.