One-Hot Encoding

easynumpy_reductions

Implement one_hot(labels, k).

Given a 1D integer array labels with values in [0, k), return a (len(labels), k) array of 0/1 where row i has a 1.0 in column labels[i] and 0.0 elsewhere. Vectorize it — no for/while loops.

Constraints
  • no scipy, sklearn
  • no for/while loops

Your solution

Edit one_hot and run the real pytest suite in your browser — no install required. Your code is saved locally.

⌘/Ctrl + ↵

Loading the Python runtime (first run only)…

Hints

Hint 1

Index the rows with np.arange and the columns with the labels in a single assignment.