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 ( Card, Bank, Cash, Wire), browser types, or clinic wards. Even when a database exports those labels as integer codes, is not twice and is not halfway between and .
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 entries form the training split and the last two entries ( and ) 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 entries: [20, 10, 20, 30, 10]. Notice that only codes , , and appear in this training prefix; held-out code at index 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 matches indices and (count ). Code matches indices and (count ). Code matches index (count ).
The pinned training count vector is [2, 2, 1], whose entries sum to (every training row accounted for). Counting across all seven rows without respecting the split leaks index (another ) to return [2, 2, 2], while index (code ) 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 , counts are [2, 2, 1] with of rows matched. Drag train_rows down to ([20, 10]) to see [1, 1, 0], or to to see [1, 2, 1]. Drag train_rows up to to see [2, 2, 2], and to to see [2, 2, 2] with only of rows matched because row holds unseen code .
![Three category count bars for pinned vocabulary [10, 20, 30] showing counts [2, 2, 1] over 5 training rows.](../assets/ml/pin-the-category-list--demo.png)
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.](../assets/ml/pin-the-category-list--exercise.png)
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 , the sum of pinned counts () is less than the prefix length (), 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 -element vocabulary counts exact occurrences per slot (
[2, 2, 1]on the 5-row prefix). - Any gap between prefix length and total matched count flags unseen categories outside the pinned vocabulary.