Column Order Is Part of the Data
Align one-hot column order strictly to the trained weight vector so category predictions never scramble at inference time.
The problem
A trained linear model stores its parameters by position, not by category name. Suppose a model was fit on the pinned vocabulary order [10, 20, 30] and learned category weights [2, -1, 5] plus intercept . Weight belongs to column (code ), weight belongs to column (code ), and weight belongs to column (code ).
Now four inference rows arrive with category codes [10, 20, 30, 10]. If the serving pipeline encodes those rows in the pinned order [10, 20, 30], every row hits its trained weight in . What happens if someone re-sorts or reverses the encoding columns to [30, 20, 10] without permuting ?
Work through the numbers
First encode [10, 20, 30, 10] against the pinned order [10, 20, 30]. Row (code ) becomes [1, 0, 0]. Row (code ) becomes [0, 1, 0]. Row (code ) becomes [0, 0, 1]. Row (code ) becomes [1, 0, 0].
Evaluate with [2, -1, 5]. Row predicts . Row predicts . Row predicts . Row predicts . The exact prediction vector is [12, 9, 15, 12].
Now reverse the vocabulary at inference time to [30, 20, 10] while keeping [2, -1, 5]. Row (code ) lands in column with weight , predicting instead of . Row (code ) stays in middle column and predicts . Row (code ) lands in column with weight , predicting instead of . Predictions scramble to [15, 9, 12, 15] with zero runtime errors.
The live model
Four horizontal bars compare each row's predicted score against the trained reference [12, 9, 15, 12]. At default swap_order , vocabulary order [10, 20, 30] aligns with [2, -1, 5] and all four bars match [12, 9, 15, 12] in blue. Drag swap_order to to flip encoding order to [30, 20, 10]: rows , , and jump to , , and in amber while middle row stays at .
![Four horizontal prediction bars showing [12, 9, 15, 12] in blue when [10, 20, 30, 10] is encoded in pinned order [10, 20, 30].](../assets/ml/column-order-is-part-of-the-data--demo.png)
let swap_order = scene.parameter("swap_order", 0.0, 0.0, 1.0, 1.0);
let vocab = torch.tensor("[10.0, 20.0, 30.0]", DType.F64);
if (swap_order > 0.5) {
vocab = torch.tensor("[30.0, 20.0, 10.0]", DType.F64);
}
let enc = input.unsqueeze(1).eq(vocab.unsqueeze(0)).to(DType.F64);
let w = torch.tensor("[2.0, -1.0, 5.0]", DType.F64).reshape(3, 1);
let answer = enc.matmul(w).reshape(input.size(0)) + 10.0;
out.set("predictions", answer);
{
"predictions": [
12,
9,
15,
12
]
}Watch the parameter change
Your turn
The starter pipeline routes codes and to the wrong weights, predicting [15, 9, 12, 15] instead of [12, 9, 15, 12]. Set answer to the linear model predictions for any 1D batch input of codes in when [2, -1, 5] was trained on pinned column order [10, 20, 30]; Check tests unseen sequences of varying lengths.
![Starter prediction bars showing scrambled [15, 9, 12, 15] because categories were encoded in reversed order [30, 20, 10].](../assets/ml/column-order-is-part-of-the-data--exercise.png)
let vocab = torch.tensor("[30.0, 20.0, 10.0]", DType.F64);
let enc = input.unsqueeze(1).eq(vocab.unsqueeze(0)).to(DType.F64);
let w = torch.tensor("[2.0, -1.0, 5.0]", DType.F64).reshape(3, 1);
let answer = enc.matmul(w).reshape(input.size(0)) + 10.0;
out.set("predictions", answer);
{
"predictions": [
15,
9,
12,
15
]
}Encode 1D vector input against pinned vocabulary [10, 20, 30] and return predictions X * [2, -1, 5] + 10.
Read the result carefully
Notice why row (code ) still predicted even when swap_order reversed [10, 20, 30] into [30, 20, 10]. Reversing a three-element list leaves its center element fixed! On a spot check of row alone, a broken deployment pipeline would look completely healthy while mispricing every code- and code- row by points.
This silent failure explains why production feature stores serialize the exact ordered category array alongside the weight tensor. Column order is not cosmetic metadata; it is the coordinate system that binds each column of to its corresponding entry of .
Recap
- Weight vectors index columns strictly by integer position, so
[10, 20, 30]and[30, 20, 10]define distinct feature spaces. - Encoding
[10, 20, 30, 10]under[10, 20, 30]with[2, -1, 5]and yields[12, 9, 15, 12]. - Reversing the vocabulary to
[30, 20, 10]silently swaps weights and , turning predictions into[15, 9, 12, 15]without raising a shape error.