Skip to content
DeepForgeby svx
All problems
EasyML Fundamentalsml-027

Label Encoding

Problem

Encode labels as integers 0, 1, 2, ... in order of first appearance.

Return the encoded list; empty input returns [].

Hint

Assign the next integer whenever you see an unseen label.

Test cases

case 1
in: [["cat","dog","cat","bird"]]
expected: [0,1,0,2]
case 2
in: [[3,1,3,2,1]]
expected: [0,1,0,2,1]
case 3
in: [[5,5,5]]
expected: [0,0,0]
case 4
in: [[]]
expected: []
Starter codePython
def label_encode(labels):
    # Your code here
    pass

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