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_mean, where the confidence weight is
λ(ni)=ni+mni — the larger the category count
ni, the more the within-category mean is trusted, and the smoothing strength
m 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.