M2-026M2: Classical Machine LearningDecision TreesHard
⚡Flashcard Drill➔Mastery:
Compare Industrial Strategies for Handling Missing Values and Categorical Features in Tree Models
📐 Mathematical Definition
⚡ One-Sentence Core Answer
Modern tree models (LightGBM, XGBoost) handle missing values natively by routing them to whichever child branch maximizes impurity gain, and handle categoricals natively via histogram integer binning and optimal Fisher splits.
📌 Key Takeaways
- •Missing Values in XGBoost/LightGBM: Treats missingness as a valid signal; during training, tests routing all missing values to left vs right branch, setting the default path to maximum gain.
- •Categorical Handling - One-Hot Encoding: Inefficient for high cardinality (sparse splits, unbalanced deep trees).
- •Categorical Handling - Target Encoding: Encodes categories as mean target values with Bayesian smoothing; risks severe target leakage if not out-of-fold.
- •Native Categorical Splits (LightGBM): Sorts categories by target mean and evaluates optimal subset partition in time via Fisher's exact theorem.
📐 Mathematical Derivations
LightGBM optimal categorical split derivation: For binary classification with categories, finding the optimal subset split among possible partitions is an NP-hard combinatorial problem. Fisher (1958) proved that if categories are sorted by the ratio of positive targets: , the optimal split threshold on this 1D sorted sequence is mathematically guaranteed to be the optimal subset split in the original categorical space! LightGBM sorts the histogram bins in time, turning an exponential search into a simple linear scan over thresholds.
🏭 Production Trade-offs
Missing value strategies: (1) Mean/Median Imputation: Destroys missingness information and distorts feature variance. (2) Missingness Indicator Flag: Adds binary column ; effective for linear models. (3) Native Tree Routing: Superior in XGBoost/LightGBM because it captures informative missingness (e.g. Failure to provide annual income correlates with credit risk).
⚠️ Common Interview Pitfalls
- ✕One-hot encoding high-cardinality features (e.g.
ZIP_Codewith 5,000 levels), which creates extremely sparse, deep, unbalanced trees. - ✕Computing Target Encoding globally without K-fold out-of-fold splitting (causes catastrophic label leakage).
🎯 Interviewer Follow-up Inquiries
- ?How does Fisher's 1958 theorem prove that sorting categories by target mean guarantees the globally optimal binary partition?
- ?How does CatBoost prevent target leakage in categorical encoding using online target statistics ordered by time?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
如何处理缺失值与类别特征?对比常见做法。
缺失值可用代理分裂(CART)、按缺失率分流、或缺失指示变量;类别特征可用 one-hot、目标编码、或原生类别分裂(LightGBM/CatBoost)。
核心要点
- 缺失值处理 :① 代理分裂(CART) ——对每个主分裂,额外学若干'代理分裂'(用与主特征最相关的其他特征),当主特征缺失时用代理分裂决定走向;优点是保留样本且利用特征相关性,缺点是计算复杂。② 默认方向(XGBoost/LightGBM) ——训练时为每个分裂学习'缺失样本走左还是走右'(按哪边损失更小),推理时缺失样本按该默认方向走;实现简单且效果常优于简单填充。③ 填充 + 指示变量 ——用均值/中位数/模型预测填充,并额外加一个'是否缺失'的二值特征(当缺失有信息时有效)。④ 按缺失分流 ——把缺失作为一个独立的类别分支。
- 类别特征处理 :① one-hot ——适合低基数( 目标编码(target/mean encoding) ——用类别对应的目标均值替代,需 交叉拟合 (在 K 折内用其他折的均值编码本折,防止标签泄漏)+ 平滑(向全局均值收缩,尤其对低频类别)。③ 原生类别分裂(LightGBM/CatBoost) ——LightGBM 用'按类别目标均值排序后分裂'(many-vs-many 分裂),CatBoost 用 ordered target statistics (按时间/随机顺序只用'过去'样本统计)从原理上避免泄漏,且用对称树加速。④ 嵌入 ——深度模型中对高基数类别学嵌入向量(如推荐系统中的 item embedding)。⑤ 计数/频率编码 ——用类别出现频次作为特征,简单且不泄漏,但信息量有限。
- 目标编码需交叉拟合防泄漏
- 高基数类别 → 哈希/嵌入/计数编码
常见陷阱
- 对高基数类别用 one-hot(维度爆炸 + 分裂低效)
- 目标编码不做交叉拟合(严重标签泄漏)