One-Hot Encoding
You are preprocessing categorical labels for a neural network classifier. Neural networks cannot directly use integer class labels like 0, 1, 2 — they need a binary vector representation where only one position is "hot" (set to 1).
Given a 1D array of integer class labels and the total number of classes n_classes, return a 2D one-hot encoded matrix of shape (len(labels), n_classes). Row i should have a 1 at column labels[i] and 0 everywhere else.
Always use n_classes for the number of columns — the input labels may not cover all classes.
Example 1
([0, 1, 2], 3)[[1, 0, 0], [0, 1, 0], [0, 0, 1]]Each label maps to its column. Result is a 3x3 identity matrix when labels are [0,1,2].
Example 2
([0, 1], 5)[[1, 0, 0, 0, 0], [0, 1, 0, 0, 0]]n_classes=5 but only 2 labels. Trailing columns are zeros — always trust n_classes not max(labels)+1.
Example 3
([2, 2, 2], 4)[[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]]All same class — identical rows. Each row is independent, no state carries between rows.
- ›1 <= len(labels) <= 1000
- ›0 <= labels[i] < n_classes
- ›2 <= n_classes <= 100
Reference solution available after you attempt the question.
Ready to solve it?
Start a session on Mockbit #72. Write your code, run it against hidden tests, and get graded with specific critique on each axis.