Encode the Shown Table
Expand an N-element categorical column into an N-by-K binary indicator matrix using outer equality broadcasting.
The problem
Encoding rows one at a time in Python loops obscures how tensor libraries turn a categorical column of length into an design matrix. Suppose five observations carry category codes [2, 0, 1, 2, 0] over the pinned three-category list [0, 1, 2].
Reshaping the length- column to and the length- vocabulary to lets outer equality produce the entire indicator matrix in one step. Swapping those two axes transposes the result into , breaking row alignment with the rest of the dataset.
Work through the numbers
Match each entry of [2, 0, 1, 2, 0] against vocabulary columns [0, 1, 2]. Row (code ) becomes [0, 0, 1]. Row (code ) becomes [1, 0, 0]. Row (code ) becomes [0, 1, 0]. Row (code ) becomes [0, 0, 1]. Row (code ) becomes [1, 0, 0].
Stacking all five rows gives [[0, 0, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0]]. Check both marginal sums of this matrix . Summing across columns () yields [1, 1, 1, 1, 1]: every row belongs to one category.
Summing down rows () yields [2, 1, 2]: code appears twice, code appears once, and code appears twice, totaling rows. If row 's category code changes from to , row flips to [1, 0, 0] and column sums shift to [3, 1, 1]; if row changes to , row flips to [0, 1, 0] and column sums become [2, 2, 1].
The live model
The binary grid colors active cells blue and inactive cells light gray, printing the column-sum vector below the grid. At default row0_code , row lights column and column totals read [2, 1, 2]. Drag row0_code to to move row 's active cell to column (column totals [3, 1, 1]), or to to move it to column (column totals [2, 2, 1]).
![5 by 3 binary one-hot grid for [2, 0, 1, 2, 0] showing active cells at columns 2, 0, 1, 2, 0 and column sums [2, 1, 2].](../assets/ml/encode-the-shown-table--demo.png)
let row0_code = scene.parameter("row0_code", 2.0, 0.0, 2.0, 1.0);
let head = torch.full(1, row0_code).to(DType.F64);
let tail = input.slice(0, 1, input.size(0));
let col = torch.cat(torch.list().add(head).add(tail), 0).unsqueeze(1);
let vocab = torch.tensor("[0.0, 1.0, 2.0]", DType.F64).unsqueeze(0);
let answer = col.eq(vocab).to(DType.F64);
out.set("encoded_table", answer);
{
"encoded_table": [
[
0,
0,
1
],
[
1,
0,
0
],
[
0,
1,
0
],
[
0,
0,
1
],
[
1,
0,
0
]
]
}Watch the parameter change
Your turn
The starter program returns a matrix whose rows index categories rather than observations. Set answer to the row-aligned binary one-hot matrix encoding each entry of input against pinned vocabulary [0, 1, 2]; Check evaluates vectors of varying row counts and category mixes.

let row = input.unsqueeze(0);
let vocab = torch.tensor("[0.0, 1.0, 2.0]", DType.F64).unsqueeze(1);
let answer = vocab.eq(row).to(DType.F64);
out.set("encoded_table", answer);
{
"encoded_table": [
[
0,
1,
0,
0,
1
],
[
0,
0,
1,
0,
0
],
[
1,
0,
0,
1,
0
]
]
}Return the N-by-3 one-hot matrix encoding every element of 1D vector input against pinned vocabulary [0, 1, 2].
Read the result carefully
Two matrix identities make full-table one-hot encoding a workhorse of linear algebra. First, the column sums immediately give class or category counts without writing a histogram loop.
Second, multiplying () by any target vector () sums separately inside each category: . The closing page divides those per-category sums by per-category counts to recover exact group means.
Recap
- Broadcasting an column of codes against a vocabulary produces the full one-hot matrix in one operation.
- On
[2, 0, 1, 2, 0]over[0, 1, 2], row sums are[1, 1, 1, 1, 1]and column sums are[2, 1, 2]. - Reversing the broadcast axes produces a transposed table whose row count no longer matches the observations.