Back to Classic ML Mind Map
中文·English
📊 Classic MLID: target-encoding

Target Encoding

目标编码 Target Encoding
🎯Core Definition
Target encoding encodes high-cardinality categorical features (user_id, city, item ID) with the mean target per category. Smoothing formula: TE=λ(ni)mean(yi)+(1λ(ni))global_meanTE = \lambda(n_i) \cdot \text{mean}(y_i) + (1 - \lambda(n_i)) \cdot \text{global\_mean}, where the confidence weight is λ(ni)=nini+m\lambda(n_i) = \frac{n_i}{n_i + m} — the larger the category count nin_i, the more the within-category mean is trusted, and the smoothing strength mm controls how hard the estimate shrinks toward the global mean, protecting small groups from noisy means. Leakage risk: computing group means on the full dataset makes both train and validation features carry target information (inflated CV, production collapse) — the biggest trap of TE. Fixes: out-of-fold (OOF) encoding — for each sample, compute the mean from the folds that did not contain it; CatBoost goes further with Ordered Target Statistics — compute statistics using only history samples preceding the current one, structurally eliminating leakage.
💡Use Cases
high-cardinality categorical features with tree models (GBDT/LightGBM) and Kaggle competitions; interviews ask “why not one-hot”, “how to prevent TE leakage”, “how to choose m”.
Key Problems Solved
one-hot explodes dimensionality for high cardinality (1M user_ids become 1M columns, memory and sparsity disaster); raw group-mean encoding leaks the target and overfits small groups; smoothing shrinkage plus OOF/Ordered encoding keeps the category-target association while controlling leakage and overfitting — CatBoost replaces one-hot with Ordered encoding precisely so categorical features can join training directly.
🎯5 High-Frequency Exam Points
1
Write the smoothed TE formula TE=λmean(yi)+(1λ)globalTE = \lambda \cdot \text{mean}(y_i) + (1-\lambda) \cdot \text{global} with λ(ni)=nini+m\lambda(n_i) = \frac{n_i}{n_i+m}; intuitively, what do m and nin_i do?
2
Why does full-data group-mean encoding leak? Trace the leakage path and the mechanism of inflated CV scores?
3
Concrete steps of OOF (out-of-fold) encoding? How does it differ from CatBoost's Ordered Target Statistics?
4
Why target encoding instead of one-hot for high-cardinality features? Compare dimensionality, sparsity and information content?
5
How to choose the smoothing m? What happens to small groups without shrinkage (high encoding variance, overfitting)?
Updated 2026-08-12
🎯
Test Your Knowledge: Practice Questions for "Target Encoding"
Single choice pitfall questions with instant feedback and mistake tracking.
🚀 Start Card Practice
Previous CardHuber Loss & Focal LossNext CardData Leakage

🔗 More Classic ML Knowledge Cards

AdaBoost DerivationBagging & Random ForestBaum-Welch (HMM EM)GBDT Negative Gradient