Why is target encoding dangerous, and how do you do it safely?
hardAnswer
- Target encoding replaces a category with the mean target for that category, so the target leaks directly into the feature.
- Fit it on the whole training set and the model memorizes rare categories, which look perfectly predictive in training and useless later.
- Do it safely with out-of-fold encoding: for each fold, compute the mapping from the other folds only.
- Add smoothing toward the global mean so a category with three rows is pulled hard to the prior.
- Keep the encoder inside the cross-validation pipeline, never as a preprocessing step applied before splitting.
Check yourself — multiple choice
- It is always safe
- The target leaks into the feature; use out-of-fold encoding with smoothing, fitted inside the CV pipeline
- Only use it on numeric features
- Replace it with one-hot always
Out-of-fold computation plus smoothing, applied inside the pipeline, is what makes it safe.
#encoding#pipelines
Practise Supervised Learning
215 interview questions in this topic.
Related questions
- How do decision trees handle categorical features?
- What makes CatBoost different from XGBoost / LightGBM?
- How do XGBoost, LightGBM, and CatBoost each handle categorical features?
- Ordinal vs nominal encoding — how do you choose?
- What is target encoding and when is it useful?
- How do you prevent leakage in target encoding?