Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-336

One Hot Encoding Matrix

Problem

One-hot encoding maps each token to a unit vector over the vocabulary in the given order.

Given the vocabulary and a token list, return the matrix of one-hot rows.

Hint

Build an index map from the vocabulary.

Test cases

case 1
in: [["a","b"],["a","b","a"]]
expected: [[1,0],[0,1],[1,0]]
case 2
in: [["x"],["x"]]
expected: [[1]]
case 3
in: [["p","q"],[]]
expected: []
Starter codePython
def one_hot_encoding_matrix(vocab, tokens):
    # Your code here
    pass

3 test cases · run your code against them in the editor