M2-029M2: Classical Machine Learning集成方法 (Bagging/RF)Medium
⚡Flashcard Drill➔Mastery:
Detail Mean Decrease Impurity (MDI) vs. Permutation Importance (MDA) in Random Forests and Their Known Biases
📐 Mathematical Definition
⚡ One-Sentence Core Answer
Feature importance is measured via Mean Decrease Impurity (MDI, summing tree split gains) and Permutation Feature Importance (MDA, measuring score drops after shuffling a column); MDI is severely biased toward high-cardinality features, while MDA is robust.
📌 Key Takeaways
- •Mean Decrease Impurity (MDI / Gini Importance): Sums impurity reductions achieved by feature across all split nodes in all trees.
- •Permutation Feature Importance (MDA / Mean Decrease Accuracy): Shuffles feature values on out-of-bag/test data and measures validation accuracy drop.
- •Flaws of MDI: Severely inflated for continuous or high-cardinality categorical variables; evaluates training fit rather than generalization.
📐 Mathematical Derivations
Mathematical formulation: (1) MDI: , where is node sample fraction. Because high-cardinality features provide many candidate thresholds, they have vastly more opportunities to reduce training impurity by chance, even if pure noise. (2) Permutation Importance (MDA): Let baseline OOB score be . Randomly shuffle column in the OOB set to form , breaking its relationship with target . Evaluate shuffled score . The importance is: . If is critical, shuffling drops score massively; if noise, score drop is .
🏭 Production Trade-offs
Handling Collinear Features: If two features and are strongly collinear (e.g. ), when is shuffled, the model can still access the identical information via unshuffled . Thus, Permutation Importance shows both features as having low importance! In production attribution, SHAP (Shapley Additive Explanations) based on cooperative game theory or grouped permutation importance resolves this collinear masking.
⚠️ Common Interview Pitfalls
- ✕Relying on default
feature_importances_(MDI) in scikit-learn for high-cardinality features (e.g. Random numerical IDs appear at the top of importance rankings). - ✕Computing Permutation Feature Importance on training data instead of test/OOB data.
🎯 Interviewer Follow-up Inquiries
- ?Why does Strobl et al. (2007) prove that MDI is systematically biased toward variables with many categories?
- ?How do TreeSHAP algorithms compute exact Shapley values in polynomial 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.
中文速览与面试要点
如何得到随机森林的特征重要度?它有什么缺陷。
基于不纯度下降(MDI)或置换重要度(permutation);MDI 偏向高基数特征且受相关特征稀释。
核心要点
- 三种重要度:① MDI(Mean Decrease in Impurity) ——累加该特征在所有树中作为分裂特征带来的不纯度下降(按样本数加权)。优点是免费(训练时顺便算);缺点是:(a) 偏向高基数/多取值特征 (更多候选阈值 → 更可能被选中且增益看似更大);(b) 在训练集上计算 故对过拟合特征给高分;(c) 相关特征互相稀释 ——若两个特征强相关,树会在它们之间随机选,导致两者重要度都被低估。② 置换重要度(Permutation Importance) ——在 验证集 上随机打乱某特征的值,看性能下降多少;优点是不偏向高基数、反映真实预测贡献;缺点是 (a) 计算需 O(p) 次预测;(b) 相关特征仍被稀释 (打乱一个,另一个仍提供信息故性能不降);(c) 若特征分布与真实推理分布不同会失真。③ SHAP ——基于博弈论的 Shapley 值,理论上满足一致性、局部准确性等公理,能给出 逐样本 的贡献分解,且能处理相关特征(需指定特征依赖);代价是计算成本高(TreeSHAP 对树模型有高效精确算法)。
- 实践建议:① 优先用置换重要度 + SHAP ——MDI 只适合快速粗筛;报告重要度时应在 测试集 上算置换重要度并多次重复取平均(减少随机性)。② 处理相关特征 ——用 分组置换 (把相关特征组一起打乱)、或先做聚类再把同组特征合并为一个特征;SHAP 的 feature_perturbation 参数取 interventional 与 tree_path_dependent 时,在相关特征下给出不同答案,需明确选择。③ 重要度 ≠ 因果 ——重要度只反映模型内的预测贡献,不能解释为因果效应;若需因果推断应用专门的因果方法(如 DML、因果森林)。④ 重要度稳定性 ——用多个随机种子重复训练并比较重要度排序,若不稳定说明特征间存在强相关或数据不足。
- 相关特征会互相分走重要度
- 置换重要度更可靠但计算贵
- 更严谨可用 SHAP
常见陷阱
- 用 MDI 的重要度做特征选择(偏向高基数 + 训练集上算)
- 把重要度当作因果效应解读