← Back to Classic ML Mind Map
📊 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.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Target Encoding.
Use Cases
In production engineering, what are the primary use cases for Target Encoding and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
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
🎯
Verify Mastery: Practice Exam Questions for "Target Encoding"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardHuber Loss & Focal LossNext Card →Data Leakage

🔗 More Classic ML Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/ML/card/target-encoding
中文速览与面试要点

目标编码 Target Encoding (Target Encoding)

定义: 目标编码(Target Encoding)用“该类别下目标均值”编码高基数类别特征(如 user_id、城市、商品 ID)。平滑公式: TE = (n_i) mean(y_i) + (1 - (n_i)) global\_mean,其中置信度权重 (n_i) = n_in_i + m——样本量 n_i 越大越相信组内均值, 平滑参数 m 控制收缩强度, 防止小样本组的编码被噪声带偏。泄漏风险: 若直接对全量数据求组均值, 训练与验证特征都携带了目标信息(CV 分数虚高、线上崩溃), 这是 TE 最大的坑。解法: OOF (out-of-fold) 编码——对某样本用“不包含它的那一折之外”的数据算均值; CatBoost 更进一步用 Ordered Target Statistics——按时间/顺序只用该样本之前的历史样本计算统计量, 从机制上切断泄漏。 使用场景: 高基数类别特征 + 树模型(GBDT/LightGBM)与 Kaggle 竞赛的标配; 面试常问“为什么高基数不用 one-hot”“目标编码怎么防泄漏”“m 怎么选”。 解决的问题: One-hot 对高基数特征维度爆炸(100 万 user_id 变成 100 万列)、内存与稀疏性灾难; 组均值编码泄漏目标信息、小样本组过拟合; 平滑收缩 + OOF/Ordered 编码在保留类别-目标关联信息的同时, 把泄漏和过拟合都控制住——CatBoost 正是靠 Ordered 编码取代 one-hot, 才让类别特征直接参与训练。

核心要点

  • 写出平滑目标编码公式 TE = mean(y_i) + (1- ) global, (n_i) = n_in_i+m; 直观解释 m 与 n_i 的作用?
  • 为什么直接用全量组均值编码会泄漏? 描述具体泄漏路径与 CV 虚高的机理?
  • OOF (out-of-fold) 编码的具体步骤? 与 CatBoost Ordered Target Statistics 的区别?
  • 为什么高基数类别特征不用 one-hot 而用 target encoding? 维度、稀疏性、信息量三方面对比?
  • m 平滑参数怎么选? 小样本组不收缩会发生什么(编码方差大、过拟合)?