What risks does target encoding introduce?
Target encoding can be powerful, but it introduces several risks because the encoded feature is calculated using the target variable itself.
1. Target leakage
The biggest risk is leakage.
If the target encoding is calculated using the full dataset before splitting or validation, information from validation or test rows can influence the encoded feature.
This can make model performance look much better than it really is.
2. Overfitting on rare categories
A category with only a few observations can produce an extreme target average.
For example:
Category A appears 2 times
Both targets = 1
Target encoding = 1.0That value may look highly predictive even though it is based on almost no data.
This can cause the model to overfit rare categories.
3. Unstable estimates
Target statistics can change significantly when category counts are small.
A category's encoded value may therefore vary a lot between different training samples.
Smoothing or minimum-count rules are often used to make these estimates more stable.
4. Leakage inside cross-validation
Even when cross-validation is used, target encoding can still leak information if each training row is encoded using a statistic that includes its own target.
A safer approach is out-of-fold target encoding:
- split the training data into folds
- calculate category statistics from the other folds
- use those statistics to encode the held-out fold
This prevents each row's target from directly influencing its own encoded value.
5. Unseen categories
A category that appears in validation, test, or production data may not exist in the training data.
A fallback strategy is needed, such as using the overall target mean.
6. Distribution changes
If the relationship between a category and the target changes over time, previously learned target encodings may become misleading.
This is especially important in production systems where customer behavior, products, or business conditions can change.
How to reduce the risks
Common safeguards include:
- calculate encodings from training data only
- use out-of-fold encoding for training rows
- smooth estimates for rare categories
- define a fallback for unseen categories
- keep the encoding process inside the model-validation pipeline
In short, target encoding can introduce leakage, overfitting, and unstable estimates because it uses information from the target. Careful train-only, out-of-fold computation and smoothing are essential for reliable use.
Learning Sequence
Machine Learning Curriculum
How do you handle high-cardinality categorical features?
Explore Related Questions
Deepen your understanding across related Machine Learning topics.