Speed

Pin the Category List

Freeze the ordered vocabulary of known categories on the training prefix and audit per-category counts without leaking held-out rows.

The problem

Categorical columns store discrete labels rather than magnitudes: payment methods (10=10 = Card, 20=20 = Bank, 30=30 = Cash, 40=40 = Wire), browser types, or clinic wards. Even when a database exports those labels as integer codes, 2020 is not twice 1010 and 3030 is not halfway between 2020 and 4040.

Before building indicator columns, the pipeline must pin an ordered vocabulary list strictly from the training split. Consider the seven-row sequence [20, 10, 20, 30, 10, 30, 40], where the first train_rows =5= 5 entries form the training split and the last two entries (3030 and 4040) are held out. Against the pinned training vocabulary [10, 20, 30], how many times does each category appear in training?

Work through the numbers

Inspect the first train_rows =5= 5 entries: [20, 10, 20, 30, 10]. Notice that only codes 1010, 2020, and 3030 appear in this training prefix; held-out code 4040 at index 66 has not arrived yet and therefore cannot belong to the pinned vocabulary [10, 20, 30].

Count exact matches inside [20, 10, 20, 30, 10] for each vocabulary slot in order. Code 1010 matches indices 11 and 44 (count 22). Code 2020 matches indices 00 and 22 (count 22). Code 3030 matches index 33 (count 11).

The pinned training count vector is [2, 2, 1], whose entries sum to 2+2+1=52 + 2 + 1 = 5 (every training row accounted for). Counting across all seven rows without respecting the split leaks index 55 (another 3030) to return [2, 2, 2], while index 66 (code 4040) matches none of the three pinned slots.

The live model

Three bars plot occurrences of vocabulary slots [10, 20, 30] inside the active prefix of length train_rows. At default train_rows =5= 5, counts are [2, 2, 1] with 55 of 55 rows matched. Drag train_rows down to 22 ([20, 10]) to see [1, 1, 0], or to 44 to see [1, 2, 1]. Drag train_rows up to 66 to see [2, 2, 2], and to 77 to see [2, 2, 2] with only 66 of 77 rows matched because row 66 holds unseen code 4040.

Three category count bars for pinned vocabulary [10, 20, 30] showing counts [2, 2, 1] over 5 training rows.
let train_rows = scene.parameter("train_rows", 5.0, 2.0, 7.0, 1.0);
let tr = input.slice(0, 0, int(train_rows)).unsqueeze(1);
let vocab = torch.tensor("[10.0, 20.0, 30.0]", DType.F64).unsqueeze(0);
let answer = tr.eq(vocab).to(DType.F64).sum(0);
out.set("pinned_counts", answer);
{
  "pinned_counts": [
    2,
    2,
    1
  ]
}
Watch the parameter change

Your turn

The starter program leaks held-out entries into the histogram, returning [2, 2, 2] instead of [2, 2, 1] at train_rows = 5. Set answer to the three-element occurrence count vector for pinned categories [10, 20, 30] in order across only the first train_rows entries of input; Check varies both the category sequence and train_rows.

Starter bars counting all 7 rows to produce [2, 2, 2] instead of [2, 2, 1] on the 5-row training prefix.
let train_rows = scene.parameter("train_rows", 5.0, 2.0, 7.0, 1.0);
let col = input.unsqueeze(1);
let vocab = torch.tensor("[10.0, 20.0, 30.0]", DType.F64).unsqueeze(0);
let answer = col.eq(vocab).to(DType.F64).sum(0);
out.set("pinned_counts", answer);
{
  "pinned_counts": [
    2,
    2,
    2
  ]
}

Return exact occurrence counts for pinned categories [10, 20, 30] in order across the first int(train_rows) elements of input.

Read the result carefully

Pinning the category list solves two silent bugs before a single weight is trained. First, if you run a unique-values scan separately on training data and test data, a category missing from a small test batch disappears and shifts every downstream column index, scrambling matrix multiplication.

Second, pinning the list on training rows makes unseen categories explicit: at train_rows =7= 7, the sum of pinned counts (∑kck=6\sum_k c_k = 6) is 11 less than the prefix length (77), immediately revealing one out-of-vocabulary arrival without crashing or inventing a new weight column at test time.

Recap

  • Categorical encoding begins by freezing an ordered list of known category codes (here [10, 20, 30]) learned strictly from training rows.
  • Broadcasting equality between the training prefix and the KK-element vocabulary counts exact occurrences per slot ([2, 2, 1] on the 5-row prefix).
  • Any gap N−∑k=1KckN - \sum_{k=1}^K c_k between prefix length and total matched count flags unseen categories outside the pinned vocabulary.