Speed

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 NN into an N×KN \times K 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-55 column to 5×15 \times 1 and the length-33 vocabulary to 1×31 \times 3 lets outer equality produce the entire 5×35 \times 3 indicator matrix in one step. Swapping those two axes transposes the result into 3×53 \times 5, 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 00 (code 22) becomes [0, 0, 1]. Row 11 (code 00) becomes [1, 0, 0]. Row 22 (code 11) becomes [0, 1, 0]. Row 33 (code 22) becomes [0, 0, 1]. Row 44 (code 00) 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 5×35 \times 3 matrix XX. Summing across columns (∑k=02Xi,k\sum_{k=0}^{2} X_{i,k}) yields [1, 1, 1, 1, 1]: every row belongs to one category.

Summing down rows (∑i=04Xi,k\sum_{i=0}^{4} X_{i,k}) yields [2, 1, 2]: code 00 appears twice, code 11 appears once, and code 22 appears twice, totaling 2+1+2=52 + 1 + 2 = 5 rows. If row 00's category code changes from 22 to 00, row 00 flips to [1, 0, 0] and column sums shift to [3, 1, 1]; if row 00 changes to 11, row 00 flips to [0, 1, 0] and column sums become [2, 2, 1].

The live model

The 5×35 \times 3 binary grid colors active 11 cells blue and inactive 00 cells light gray, printing the column-sum vector below the grid. At default row0_code =2= 2, row 00 lights column 22 and column totals read [2, 1, 2]. Drag row0_code to 00 to move row 00's active cell to column 00 (column totals [3, 1, 1]), or to 11 to move it to column 11 (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].
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 3×53 \times 5 matrix whose rows index categories rather than observations. Set answer to the row-aligned N×3N \times 3 binary one-hot matrix encoding each entry of input against pinned vocabulary [0, 1, 2]; Check evaluates vectors of varying row counts NN and category mixes.

Starter 3 by 5 transposed grid instead of the 5 by 3 row-aligned one-hot matrix.
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 ∑i=1NXi,k\sum_{i=1}^N X_{i,k} immediately give class or category counts [n0,n1,n2][n_0, n_1, n_2] without writing a histogram loop.

Second, multiplying XTX^T (K×NK \times N) by any target vector yy (N×1N \times 1) sums yy separately inside each category: (XTy)k=∑i:ci=kyi(X^T y)_k = \sum_{i : c_i = k} y_i. The closing page divides those per-category sums by per-category counts nkn_k to recover exact group means.

Recap

  • Broadcasting an N×1N \times 1 column of codes against a 1×K1 \times K vocabulary produces the full N×KN \times K one-hot matrix XX 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 K×NK \times N table whose row count no longer matches the NN observations.