Unknown Category Goes to Other
Route unseen test-time categories into a dedicated fourth catch-all column so every observation preserves unit row sum.
The problem
Real deployments encounter category values that never appeared in training: a newly launched product tier, a regional carrier added in Q4, or a typo code from an upstream form. Suppose training pinned three known categories [10, 20, 30], and four incoming observations arrive with codes [10, 40, 30, 50].
Matching [10, 40, 30, 50] strictly against [10, 20, 30] turns rows (code ) and (code ) into all-zero vectors [0, 0, 0]. Appending a fourth catch-all Other column routes every unseen code — whether , , , or — into a dedicated slot so every row still sums to without resizing the weight matrix.
Work through the numbers
First compute the known indicator block against [10, 20, 30]. Row (code ) gives [1, 0, 0] with known sum . Row (code ) gives [0, 0, 0] with known sum . Row (code ) gives [0, 0, 1] with known sum . Row (code ) gives [0, 0, 0] with known sum .
Next form the fourth column for each row: [0, 1, 0, 1]. Concatenating known slots and Other across columns yields the matrix [[1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]].
Every row once again sums to ([1, 1, 1, 1]), and column sums read [1, 0, 1, 2]: one row in slot , zero in slot , one in slot , and two unseen arrivals pooled in Other.
The live model
The grid colors known active slots blue and active Other slots purple, printing how many rows landed in Other. At default row1_code , rows and both light column (Other count ). Drag row1_code to , , or to watch row move into known columns , , or (Other count ), then drag row1_code to to see another unseen code route back into column (Other count ).
![4 by 4 indicator grid for [10, 40, 30, 50] with known slots [10, 20, 30] in blue and unseen rows 1 and 3 routed to Other in purple.](../assets/ml/unknown-category-goes-to-other--demo.png)
let row1_code = scene.parameter("row1_code", 40.0, 10.0, 50.0, 10.0);
let r0 = input.slice(0, 0, 1);
let r1 = torch.full(1, row1_code).to(DType.F64);
let rest = input.slice(0, 2, input.size(0));
let col = torch.cat(torch.list().add(r0).add(r1).add(rest), 0).unsqueeze(1);
let vocab = torch.tensor("[10.0, 20.0, 30.0]", DType.F64).unsqueeze(0);
let known = col.eq(vocab).to(DType.F64);
let other = (1.0 - known.sum(1)).unsqueeze(1);
let answer = torch.cat(torch.list().add(known).add(other), 1);
out.set("encoded_with_other", answer);
{
"encoded_with_other": [
[
1,
0,
0,
0
],
[
0,
0,
0,
1
],
[
0,
0,
1,
0
],
[
0,
0,
0,
1
]
]
}Watch the parameter change
Your turn
The starter program leaves unseen categories as all-zero rows ([0, 0, 0, 0]) instead of routing them to column . Set answer to the binary indicator matrix over pinned categories [10, 20, 30] followed by Other for any code outside {10, 20, 30}; Check tests unseen codes below , between known slots (such as ), and above .

let col = input.unsqueeze(1);
let vocab = torch.tensor("[10.0, 20.0, 30.0]", DType.F64).unsqueeze(0);
let known = col.eq(vocab).to(DType.F64);
let other = torch.zeros(input.size(0)).to(DType.F64).unsqueeze(1);
let answer = torch.cat(torch.list().add(known).add(other), 1);
out.set("encoded_with_other", answer);
{
"encoded_with_other": [
[
1,
0,
0,
0
],
[
0,
0,
0,
0
],
[
0,
0,
1,
0
],
[
0,
0,
0,
0
]
]
}Return the N-by-4 indicator matrix over pinned categories [10, 20, 30] plus a fourth catch-all Other column for every unseen code.
Read the result carefully
Why is a dedicated Other column safer than leaving unknown categories as all-zero rows? In a full indicator table, [0, 0, 0] forces to output the bare intercept , which no training row ever occupied. Worse, if one baseline column is dropped to remove collinearity with the intercept (as the closing lesson discusses), the all-zero row represents that baseline category — so Other must stay an explicit column and must not be the dropped baseline.
Also consider how is learned: if zero training rows ever hit Other during fit, stays untrained (or shrinks to under regularization). Teams expecting new categories in production often pool rare training categories into Other so learns an honest rare-category estimate.
Recap
- Comparing against pinned
[10, 20, 30]gives known rows and unseen rows . - Setting captures every out-of-vocabulary code — below, between, or above known codes — in column .
- When dropping a reference column for an unregularized intercept, keep
Otheractive so unseen categories never masquerade as the baseline category.