Speed

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 11 (code 4040) and 33 (code 5050) into all-zero vectors [0, 0, 0]. Appending a fourth catch-all Other column routes every unseen code — whether 55, 2525, 4040, or 9999 — into a dedicated slot so every row still sums to 11 without resizing the weight matrix.

Work through the numbers

First compute the 4×34 \times 3 known indicator block KK against [10, 20, 30]. Row 00 (code 1010) gives [1, 0, 0] with known sum 11. Row 11 (code 4040) gives [0, 0, 0] with known sum 00. Row 22 (code 3030) gives [0, 0, 1] with known sum 11. Row 33 (code 5050) gives [0, 0, 0] with known sum 00.

Next form the fourth column oi=1−∑j=02Ki,jo_i = 1 - \sum_{j=0}^{2} K_{i,j} for each row: [1−1,1−0,1−1,1−0]=[1 - 1, 1 - 0, 1 - 1, 1 - 0] = [0, 1, 0, 1]. Concatenating known slots and Other across columns yields the 4×44 \times 4 matrix [[1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]].

Every row once again sums to 11 ([1, 1, 1, 1]), and column sums read [1, 0, 1, 2]: one row in slot 1010, zero in slot 2020, one in slot 3030, and two unseen arrivals pooled in Other.

The live model

The 4×44 \times 4 grid colors known active slots blue and active Other slots purple, printing how many rows landed in Other. At default row1_code =40= 40, rows 11 and 33 both light column 33 (Other count 22). Drag row1_code to 1010, 2020, or 3030 to watch row 11 move into known columns 00, 11, or 22 (Other count 11), then drag row1_code to 5050 to see another unseen code route back into column 33 (Other count 22).

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.
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 33. Set answer to the N×4N \times 4 binary indicator matrix over pinned categories [10, 20, 30] followed by Other for any code outside {10, 20, 30}; Check tests unseen codes below 1010, between known slots (such as 2525), and above 3030.

Starter 4 by 4 grid leaving rows 1 and 3 all zeros because the Other column was filled with zeros.
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 Xw+bX w + b to output the bare intercept bb, 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 wotherw_{\mathrm{other}} is learned: if zero training rows ever hit Other during fit, wotherw_{\mathrm{other}} stays untrained (or shrinks to 00 under L2L_2 regularization). Teams expecting new categories in production often pool rare training categories into Other so wotherw_{\mathrm{other}} learns an honest rare-category estimate.

Recap

  • Comparing against pinned [10, 20, 30] gives known rows ∑j=02Ki,j=1\sum_{j=0}^{2} K_{i,j} = 1 and unseen rows ∑j=02Ki,j=0\sum_{j=0}^{2} K_{i,j} = 0.
  • Setting oi=1−∑j=02Ki,jo_i = 1 - \sum_{j=0}^{2} K_{i,j} captures every out-of-vocabulary code — below, between, or above known codes — in column 33.
  • When dropping a reference column for an unregularized intercept, keep Other active so unseen categories never masquerade as the baseline category.