What is label encoding and when is it appropriate?
Label encoding converts categorical labels into integer values.
For example:
Cat = 0
Dog = 1
Rabbit = 2Each category receives a unique number.
When label encoding is appropriate
Label encoding is commonly appropriate for target labels in classification problems.
For example, a target such as:
Spam
Not Spamcan be converted to:
Spam = 1
Not Spam = 0The numbers simply represent class identities.
It can also work with ordinal input features
If an input feature has a real order, numerical codes can preserve that ranking.
For example:
Low = 0
Medium = 1
High = 2In this case, the ordering is meaningful.
Be careful with nominal input features
For unordered categories such as:
Delhi
Mumbai
Bengalurumapping them to:
Delhi = 0
Mumbai = 1
Bengaluru = 2creates numbers that may look ordered even though the cities have no natural ranking.
Some models may interpret those values as if 2 is greater than 1 in a meaningful way.
For nominal input features, one-hot encoding or another categorical encoding method is often safer.
Model choice matters
Some tree-based models can work reasonably with integer-coded categories in certain implementations, while other models are more likely to treat those codes as numerical quantities.
The encoding strategy should therefore match the model and the meaning of the feature.
Keep the mapping consistent
The same category-to-number mapping must be used for training, validation, test, and production data.
Unknown categories should also have a defined handling strategy.
In short, label encoding assigns a unique integer to each category. It is commonly appropriate for classification targets and ordered categories, but arbitrary integer codes for nominal input features can introduce a misleading sense of order.
Learning Sequence
Machine Learning Curriculum
Explore Related Questions
Deepen your understanding across related Machine Learning topics.