One-Hot Encode Categorical Labels

Easy
~15 min
code completion

One-Hot Encode Categorical Labels

One-hot encoding converts integer class labels into sparse binary vectors. For a label , the one-hot vector has length , with a 1 at position and 0s everywhere else.

This prevents the model from inferring a false ordinal relationship between categories (e.g., treating class 2 as "twice" class 1).

Example: labels = [0, 1, 2], num_classes = 3

Efficient NumPy approach:

1. Allocate a zero matrix of shape (n, num_classes)

2. Use advanced indexing: result[np.arange(n), labels] = 1.0

This sets exactly one element per row without any Python-level loop.

Your task:

Implement one_hot_encode(labels, num_classes) that returns a float matrix of shape (n, num_classes).

Example Tests

3-class identity case: each sample gets one distinct 1

Input: {"labels":[0,1,2],"num_classes":3}

Expected: [[1,0,0],[0,1,0],[0,0,1]]

Binary encoding with repeated labels

Input: {"labels":[1,1,0],"num_classes":2}

Expected: [[0,1],[0,1],[1,0]]

Output shape check: 4 samples, 4 classes → (4, 4)

Input: {"labels":[0,1,2,0],"num_classes":4}

Expected: [4,4]

Sign in to solve this problem

You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.