Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k 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 (41.4k points)

Use factorize:

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

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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...