What Categorical Encoding Is
Categorical encoding involves converting non-numeric categories into a format that can be provided to machine learning algorithms. This is necessary because most ML models require numerical input.
The process of encoding transforms categorical data, such as colors or types of animals, into numbers, allowing the model to understand and learn from these features.
Types of Categorical Encoding
One-hot encoding creates a binary vector for each category, where only one bit is '1' (representing the presence of that category) and all others are '0'. This method avoids introducing ordinal relationships between categories.
Label encoding assigns a unique integer to each category. Frequency encoding uses the frequency of occurrence as the encoded value, which can be useful for some types of data.
Target Encoding
Target encoding is a more complex method where the category values are replaced with the mean target value of that category. This approach can capture relationships between categories and outcomes but may lead to overfitting if not handled carefully.
Overfitting occurs when the model learns too much from the training data, including noise or outliers, leading to poor generalization on unseen data.
Implications of Different Encodings
The choice of encoding method can significantly affect a model's performance. One-hot encoding is safe but increases dimensionality, while label and frequency encodings are simpler but may introduce ordinality or bias.
Target encoding can be very powerful but requires careful handling to avoid overfitting, especially when category sample sizes are small.
Frequently asked questions
What is the difference between one-hot encoding and label encoding?
One-hot encoding creates a binary vector for each category, avoiding ordinal relationships. Label encoding assigns a unique integer to each category, which can introduce ordinality.
Why might target encoding lead to overfitting?
Target encoding uses the mean target value of categories as their encoded values. If not handled carefully, especially with small sample sizes, it can capture noise or outliers, leading to poor generalization on new data.
How does frequency encoding work?
Frequency encoding replaces category values with the frequency of occurrence of each category in the dataset, which can be useful for some types of categorical features.
When should I use one-hot encoding over target encoding?
Use one-hot encoding when you want to avoid introducing ordinal relationships between categories and are concerned about overfitting. It is a safer choice but increases dimensionality.
Try it live
Everything above runs in your browser — open Categorical Encoding Lab and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open Categorical Encoding Lab simulation