M2-023M2: Classical Machine LearningDecision TreesEasy
⚡Flashcard Drill➔Mastery:
Detail Pre-Pruning and Post-Pruning Strategies to Prevent Overfitting in Decision Trees
📐 Mathematical Definition
⚡ One-Sentence Core Answer
Decision trees prevent overfitting via Pre-Pruning (stopping growth early via depth and sample limits) and Post-Pruning (growing a full tree and pruning subtrees using Cost-Complexity Pruning).
📌 Key Takeaways
- •Pre-Pruning parameters:
max_depth(restricts tree levels),min_samples_split(minimum samples to allow split),min_samples_leaf(minimum samples per terminal leaf),min_impurity_decrease. - •Post-Pruning (Cost-Complexity Pruning): Minimizes , balancing empirical misclassification against tree leaf count .
- •Why Post-Pruning is superior: Pre-pruning suffers from 'myopia' (stops early before reaching split combinations that together provide high gain); post-pruning evaluates full context.
📐 Mathematical Derivations
Minimal Cost-Complexity Pruning (Breiman et al., 1984): For any subtree rooted at node , the cost of collapsing into a single leaf node is . The cost of keeping the subtree is . The subtree is preferred as long as . Setting these equal defines the critical threshold . As complexity penalty increases from 0 to , it generates a nested sequence of pruned subtrees . Cross-validation is used to select optimal (parameter
ccp_alpha in scikit-learn).🏭 Production Trade-offs
In industrial pipelines: (1) Standalone decision trees require aggressive pre-pruning or post-pruning to prevent memorizing training noise. (2) In Random Forests, individual trees are deliberately grown deep and unpruned (maximizing individual tree capacity and diversity) because variance is subsequently eliminated by ensemble averaging. (3) In GBDT, trees are shallow (depth 3 to 6) weak learners.
⚠️ Common Interview Pitfalls
- ✕Setting
min_samples_leaftoo small in noisy datasets (leads to leaves fitting individual outlier observations). - ✕Relying solely on
max_depthwithout tuningmin_samples_split.
🎯 Interviewer Follow-up Inquiries
- ?How does scikit-learn
CostComplexityPruningcompute the sequence of effective alphas? - ?Why are shallow trees (depth 3-6) preferred in Gradient Boosting while deep trees are preferred in Random Forests?
⚡
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.
中文速览与面试要点
决策树如何防止过拟合?列出主要手段。
预剪枝(最大深度/最小样本/最小增益)+ 后剪枝(代价复杂度)+ 集成。
核心要点
- 两类剪枝策略:① 预剪枝(pre-pruning) ——在生长过程中提前停止:限制最大深度(max_depth)、最小叶样本数(min_samples_leaf)、最小分裂样本数(min_samples_split)、最小不纯度下降(min_impurity_decrease)、最大叶节点数。优点是快,缺点是 贪心停止可能错过后续更有用的分裂 (因为当前分裂看起来增益小但为后续铺路),导致欠拟合。② 后剪枝(post-pruning) ——先长成完整树再自底向上剪: 代价复杂度剪枝 (CCP)最小化 R_α(T)=R(T)+α|T|,其中 R(T) 是训练误差、|T| 是叶节点数、α 是惩罚系数;α=0 不剪、α→∞ 剪成单节点,通过 CV 选 α。其他方法包括 错误率降低剪枝 (REP)、 悲观剪枝 (PEP,C4.5 用,无需额外验证集)。
- 实践要点:① 后剪枝通常优于预剪枝 ——因为它基于完整树的全局信息做决策,而非贪心提前停止;但计算成本更高。② 单棵树的根本问题 ——即使剪枝,单棵树仍 方差大 (训练数据微小扰动会导致结构大变),这是 Bagging/随机森林的直接动机。③ 实践中的选择 ——现代实践中很少直接用单棵树:若需要可解释性,用 浅树 (深度 3–5)+ 预剪枝;若追求性能,用随机森林(深树 + 平均降方差)或 GBDT(浅树 + 串行降偏差)。④ sklearn 的默认 —— DecisionTreeClassifier 默认不剪枝(完全生长),需手动设置; ccp_alpha 参数控制代价复杂度剪枝强度,可用 cost_complexity_pruning_path 得到候选 α 序列。
- 预剪枝快但可能欠拟合
- 后剪枝通常效果更好
- 实际多直接用随机森林/GBDT
常见陷阱
- 认为预剪枝总是更好(可能欠拟合)
- 让单棵树完全生长而不剪枝(严重过拟合)