What is label encoding? Application of label encoder in machine learning and deep learning models.

Sunny Kumar
3 min readJan 12, 2024

--

In the process of creating ML models we deal with datasets having multiple type of datatypes. There is wide range from numerical to categorical data. However, it is easy to deal with numerical datatypes , categorical datatypes are always a headache to handle. I would suggest you to go through this article to understand types of categorical data types.

Label encoding is a process in machine learning where categorical data, represented as labels or strings, is converted into numerical format. In this encoding technique, each unique category is assigned a unique integer, effectively converting categorical data into numerical values. The main purpose is to prepare the data for machine learning algorithms that require numerical input.

Where label encoding is suitable?

Label encoding is suitable for categorical data where there is an inherent order or ranking among the categories. Specifically, it is appropriate for ordinal categorical data. Ordinal data is categorical data that has a clear order or ranking, meaning that one category is greater or smaller than another. It’s important to be cautious and considerate of the context when using label encoding. It is not suitable for all types of categorical data, especially when there is no ordinal relationship among the categories.

In cases where the categorical variables are nominal or lack a meaningful order, techniques like one-hot encoding or ordinal encoding may be more appropriate.

Example of label encoding.

Assume that a dataset contains a column called Height with the following elements: tall, medium, and short. We will use label encoding to transform this column from a category to a numerical format. Following the application of label encoding, the Height column is transformed into a numerical column with the elements 0, 1, and 2, where 0 represents the label for long height, 1 represents medium height, and 2 represents short height.

example of label encoding.

Other examples of ordinal categorical data that can be label encoded include:

  1. Education Levels: “High School” < “Some College” < “Bachelor’s Degree” < “Master’s Degree” < “Ph.D.”
  2. Income Levels: “Low” < “Medium” < “High”
  3. Rating Scales: “Poor” < “Average” < “Good” < “Excellent”

There is a clear order among the categories, and label encoding can represent this order by assigning numerical values accordingly.

Code sample to do label encoding.

from sklearn.preprocessing import LabelEncoder
categories = ['red', 'blue', 'green', 'orange']
encoded_categories = label_encoder.fit_transform(categories)
print("Original categories:", categories)
print("Encoded categories:", encoded_categories)
output of label encoder

Benefits of label encoding.

Apart from making our life easier to handle categorial data label encoding also helps in efficient memory management, interpretability, dimension reduction and prevention of algorithm bias.

Some machine learning algorithms may interpret numerical input more effectively than categorical input. Label encoding can help prevent potential bias in the algorithm caused by the representation of categorical features.

Memory efficiency: Numerical representations generally consume less memory compared to storing categorical labels as strings. This can be significant when dealing with large datasets, contributing to more efficient memory usage and faster computation

Conclusion

In this article we looked at basics of label encoding and how to implement it. We also looked at benefits of applying label encoding technique to our dataset. I hope you like this article. Happy Reading 🙂.

--

--

Sunny Kumar

Loves technology . Combining AI and AR to create meaningful solutions.