ML Grind

One-Hot Encoding - Data Preprocessing

Implement One-Hot Encoding in Python with NumPy. medium exercise with examples, hints, and instant feedback. No setup or signup required.

One-Hot Encoding

What you'll build

A function that turns a vector of integer class labels into a one-hot matrix: one row per label, with a single 1 in the column of that label.

Background

Feeding category ids such as 0, 1, 2 straight into a linear model implies an order and a distance (2 is "twice" 1) that don't exist. One-hot vectors put every category at the same distance from every other. The same matrix is the target format for softmax cross-entropy (see Neural Networks).

Formula

out[i, c] = 1 if labels[i] == c else 0        i = 0..n-1,  c = 0..num_classes-1

Inputs

  • labels: 1-D integer array of shape (n,), each value in [0, num_classes - 1] (tests may pass a Python list)
  • num_classes: int, 1 <= num_classes <= 100; it can be larger than max(labels) + 1

Expected return format

An integer array of shape (n, num_classes) containing only 0s and 1s; every row sums to 1.

Edge cases and conventions

  • Classes that never appear still get a (zero) column, so the width is always num_classes.
  • Labels can be in any order and can repeat.

Constraints

  • Use NumPy; vectorise (fancy indexing, no Python loop over samples).

Follow-up

  • How would you encode a feature with a million distinct values (hashing, embeddings)?
  • How do you invert the encoding? (argmax along axis=1.)

Examples

Input: one_hot_encode(np.array([0, 1, 2, 1]), 3)
Output: [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0]]

Row i has its 1 in column labels[i].

Input: one_hot_encode(np.array([0, 2]), 4)
Output: [[1, 0, 0, 0], [0, 0, 1, 0]]

num_classes = 4 gives 4 columns even though classes 1 and 3 never appear.

Practice in Python

import numpy as np

def one_hot_encode(labels: np.ndarray, num_classes: int) -> np.ndarray:
    """
    Convert integer labels to a one-hot matrix.

    Args:
        labels: 1-D int array of shape (n,), values in [0, num_classes - 1]
        num_classes: Number of columns in the output

    Returns:
        Int array of shape (n, num_classes); row i has a 1 in column labels[i]
    """
    # Your code here
    pass

The interactive editor loads with Python and NumPy. Run your implementation against 5 tests, or use the progressive hints.

More exercises in this section →