All problems
One-Hot Encoding
Problem
One-hot encode integer labels into rows of length num_classes, setting row[label] = 1.
A label outside [0, num_classes) produces an all-zero row. Empty input returns [].
Hint
Build a zero row, flip one position, and append it.
Test cases
case 1
in: [[0,2,1],3]
expected: [[1,0,0],[0,0,1],[0,1,0]]
case 2
in: [[1,1,0],2]
expected: [[0,1],[0,1],[1,0]]
case 3
in: [[2,0],4]
expected: [[0,0,1,0],[1,0,0,0]]
case 4
in: [[],3]
expected: []
Starter codePython
def one_hot_encode(labels, num_classes):
# Your code here
pass4 test cases · run your code against them in the editor