One-Hot Encoder

Start Timer

0:00:00

Upvote
3
Downvote
Save question
Mark as completed
View comments (5)
Next question

Given a list, write a function encode which one-hot encodes the items in the list. When one-hot encoding, the most significant bit (index zero) should be represented by the first unique element in the list.

Example 1:

Input:

elements = [1, 3, 4, 3, 2, 1]

Output:

def encode(elements) -> [[1, 0, 0, 0], [0, 1, 0, 0],
 [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0]]

Example 2:

Input:

elements = ['hi', 'hello', 'amazing', 'amazing']

Output:

def encode(elements) -> [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 1]]
.
.
.
.
.


Comments

Loading comments