0 votes
1 view
in Data Science by (16.3k points)

When label encoding numbers

[1, 1, 2, 6]

LabelEncoder return [0,0,1,2] because it sorts the classes

What's the best possible way to get [1,1,0,2] by preserving the original order

Tried - CategoricalIndex, which works the same way

from sklearn import preprocessing

le = preprocessing.LabelEncoder()

le.fit([2, 1, 2, 6])

# le.classes_ [1,2,6]

le.transform([1, 1, 2, 6]) 

1 Answer

0 votes
by (25.9k points)

Use factorize:

pd.factorize([2, 1, 2, 6])[0]

array([0, 1, 0, 2])

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...