8 — Label Encoding & One-hot Encoding

Aysel Aydin
3 min readJun 29, 2024

--

In this article, we will talk about label encoding and one hot encoding, their usage areas and differences.

If you want to read more articles about NLP, don’t forget to stay tuned :) click here.

Label Encoding

Label Encoding is a technique used in machine learning and data processing to convert categorical data (such as text-based or symbolic data) into numerical values.

For example, if you have categories like “Apple”, “Chicken” and “Broccoli” you assign them numerical labels such as 1 for “Apple”, 2 for “Chicken” and 3 for “Broccoli”.

This helps machine learning algorithms, which prefer numerical inputs, to work effectively with categorical data.

Label encoding is commonly used in tasks like NLP, classification problems. It’s important to be cautious because Label Encoding assumes an ordinal relationship between the categories. Depending on your dataset and the algorithms you’re using, you might need to consider alternative encoding methods like One-hot Encoding.

Let’s talk about One-hot Encoding now.

One Hot Encoding

One-hot Encoding is another technique used in machine learning to convert categorical data into a format that can be provided to machine learning algorithms more easily.

One-hot Encoding takes each categorical value and turns it into a binary vector. Each category is represented as a binary vector.

For “Apple” (which was label encoded as 1):

  • Original category: “Apple”
  • One-hot encoded vector: [1, 0, 0] (1 at index 0, indicating “Apple”, and 0s elsewhere)

For “Chicken” (which was label encoded as 2):

  • Original category: “Chicken”
  • One-hot encoded vector: [0, 1, 0] (1 at index 1, indicating “Chicken”, and 0s elsewhere)

For “Broccoli” (which was label encoded as 3):

  • Original category: “Broccoli”
  • One-hot encoded vector: [0, 0, 1] (1 at index 2, indicating “Broccoli”, and 0s elsewhere)

Choosing the Right Encoding Method

This question generally depends on your dataset and the model which you wish to apply.

Why Use One-Hot Encoding Instead of Label Encoding?

1 — Converting Categorical Data to Numerical Data: Machine learning models usually work with numerical data. Categorical data (like “Apple”, “Chicken”, “Broccoli”) can’t be used directly. One-Hot Encoding changes these categories into numbers. It makes the training process faster and more effective.

2 — Avoiding Category Ranking Problems: Label Encoding gives categories numerical values (e.g., Apple=1, Chicken=2, Broccoli=3). This can make the model think there is a ranking between the categories. One-Hot Encoding makes each category a separate column, avoiding this problem. In this way, the model can see each category as a separate column and understand the relationships between them correctly. This increases the model’s accuracy and generalization ability.

3 — Better Performance and Accuracy: One-Hot Encoding helps the model learn each category independently, which can make the model more accurate.

Conclusion

Label Encoding and One-Hot Encoding are two powerful techniques to convert categorical data into numerical values. Remember, the choice between these techniques depends on your data and what you want your model to understand.

Follow for more upcoming Articles about NLP, ML & DL❤️
Contact Accounts: Twitter, LinkedIn

--

--