Categorical Missing Uses the Mode
Count observed training categories and fill missing categorical slots with the training mode rather than a fractional average of codes.
The problem
Not every column stored as integers represents a physical ruler. Suppose customer subscription tiers are coded as 0 for Basic, 1 for Plus, 2 for Pro, and -1 when unrecorded. Averaging those codes as in numeric mean imputation produces fractions like 1.4, which do not name any subscription tier.
Consider ten records [role, plan]: six training rows ([1, 0], [1, 1], [1, 2], [1, 2], [1, 2], [1, -1]) followed by four held-out test rows ([0, 0], [0, 0], [0, 0], [0, -1]). For discrete labels, mode imputation fills missing cells with the single category that appears most often among observed training rows.
Work through the numbers
Count each valid tier (0, 1, and 2) inside the training split (role = 1). Row 5 is -1, leaving five observed training rows: one 0, one 1, and three 2s. The training frequency vector is [1, 1, 3], whose largest count sits at index 2 (Pro).
Compare that against two plausible mistakes. Averaging the five observed training codes gives (0 + 1 + 2 + 2 + 2) / 5 = 1.4, inventing an invalid fractional tier. Counting across all ten rows mixes in the three held-out 0s from rows 6, 7, and 8, shifting frequencies to [4, 1, 3] and flipping the winning category from 2 to 0.
Freezing the genuine training mode 2 fills both missing row 5 (training) and missing row 9 (held-out) with 2, producing [0, 1, 2, 2, 2, 2, 0, 0, 0, 2].
The live model
The left panel plots the training counts [1, 1, 3] and highlights the winning mode in dark blue; the right panel shows all ten filled rows with imputed slots tinted amber. At the default chosen_plan = 2, training counts are [1, 1, 3] and missing rows 5 and 9 become 2. Slide chosen_plan to 1 to make training counts [1, 3, 1] and mode 1, or down to 0 to make training counts [3, 1, 1] and mode 0.
![Training category counts [1, 1, 3] for Basic, Plus, and Pro select mode 2 (Pro), filling missing row 5 and held-out row 9 with 2 despite three held-out Basic rows.](../assets/ml/categorical-missing-uses-the-mode--demo.png)
let chosen_plan = scene.parameter("chosen_plan", 2.0, 0.0, 2.0, 1.0);
let table = input.clone();
table.set(7, chosen_plan);
table.set(9, chosen_plan);
let role = table.select(1, 0);
let plan = table.select(1, 1);
let c0 = (role * plan.eq(0.0).to(DType.F64)).sum().reshape(1);
let c1 = (role * plan.eq(1.0).to(DType.F64)).sum().reshape(1);
let c2 = (role * plan.eq(2.0).to(DType.F64)).sum().reshape(1);
let counts = torch.cat(torch.list().add(c0).add(c1).add(c2), 0);
let mode = float(counts.argmax(0).item_int());
let answer = torch.where(plan.lt(0.0), plan * 0.0 + mode, plan);
out.set("train_counts", counts).set("mode", mode).set("filled_plan", answer);
{
"train_counts": [
1,
1,
3
],
"mode": 2,
"filled_plan": [
0,
1,
2,
2,
2,
2,
0,
0,
0,
2
]
}Your turn
Unrecorded rows 5 and 9 currently receive 1.4—a fraction between Plus (1) and Pro (2) that names no real subscription tier. Fill missing entries with the most common observed training tier instead (taking the lowest code on a tie) so answer equals [0, 1, 2, 2, 2, 2, 0, 0, 0, 2].

let table = input.clone();
let role = table.select(1, 0);
let plan = table.select(1, 1);
let c0 = (role * plan.eq(0.0).to(DType.F64)).sum().reshape(1);
let c1 = (role * plan.eq(1.0).to(DType.F64)).sum().reshape(1);
let c2 = (role * plan.eq(2.0).to(DType.F64)).sum().reshape(1);
let counts = torch.cat(torch.list().add(c0).add(c1).add(c2), 0);
let tr_obs = role * plan.ge(0.0).to(DType.F64);
let avg_code = (plan * tr_obs).sum().div(tr_obs.sum());
let answer = torch.where(plan.lt(0.0), plan * 0.0 + avg_code, plan);
out.set("train_counts", counts).set("filled_plan", answer);
{
"train_counts": [
1,
1,
3
],
"filled_plan": [
0,
1,
2,
2,
2,
1.4,
0,
0,
0,
1.4
]
}Fill missing subscription tiers with the most frequent observed training code (on a tie take the lowest code), leaving held-out rows out of the tally.
Using the winning training category replaces 1.4 with integer tier 2, yielding [0, 1, 2, 2, 2, 2, 0, 0, 0, 2].
Read the result carefully
Mode imputation guarantees every filled slot holds a valid category ID seen during training, so subsequent one-hot or embedding lookups never encounter an unknown code. Still, assigning every unrecorded customer to the majority tier exaggerates how common that tier appears and hides whether missingness belonged to a distinct subgroup such as trial users. Notice also that ties between two equally frequent categories require a deterministic tie-breaking convention (argmax chooses the lowest index).
Recap
- Never average integer category codes; fractional values such as
1.4do not correspond to any discrete category. - Tally category frequencies strictly on observed training rows so held-out rows cannot flip the winning mode.
- Fill missing categorical slots in both training and held-out rows with that frozen training mode.