Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Grow big, then cut back
Stopping a tree early is risky: a split that looks useless now might enable great splits below it. CART’s answer is to grow the tree fully, then prune. Every split must justify itself. If a split reduces training errors by less than a price α per extra leaf, it goes. Raising α removes splits in order from weakest to strongest, giving a family of ever-smaller trees. Cross-validation picks the price.
Pruning is how a single tree becomes trustworthy: small enough to read, robust to label noise, and chosen by data rather than by guessing a depth. scikit-learn exposes it as ccp_alpha and cost_complexity_pruning_path. The same “fit big, then penalise complexity” idea underlies the lasso, model selection with AIC and BIC, and regularisation in general.
- 1. CART prunes…
- 2. Raising α makes the pruned tree…
- 3. In the demo, error on fresh data as you prune…
Level 1: Beginner
★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.Weakest-link pruning by hand
Criterion. R_α(T) = R(T) + α·|T|, where R is the training error rate and |T| the number of leaves.
For an internal node t, compare keeping its subtree T_t with collapsing it to a leaf. Collapsing wins once α ≥ g(t) = (R(t) − R(T_t))/(|T_t| − 1).
Example tree (N = 100).
| Node | counts (class 0, class 1) | R(t) if collapsed | subtree errors | leaves | g(t) |
|---|---|---|---|---|---|
| A = (5, 8) | leaves (4, 1), (1, 7) | 0.05 | 0.02 | 2 | 0.03 |
| B = (50, 10) | leaves (45, 2) + A’s two | 0.10 | 0.04 | 3 | 0.03 |
| C = (10, 30) | leaves (8, 4), (2, 26) | 0.10 | 0.06 | 2 | 0.04 |
| root (60, 40) | all 5 leaves | 0.40 | 0.10 | 5 | 0.075 |
- The smallest g is 0.03, a tie between A and B (A is inside B). Collapse B: 5 leaves become 3, and the error becomes 0.16.
- Recompute: C’s g is still 0.04, and the root’s is now (0.40 − 0.16)/2 = 0.12. Collapse C at 0.04, leaving 2 leaves with error 0.20.
- Root: g = (0.40 − 0.20)/1 = 0.20. Collapse to a single leaf.
The sequence is α = 0, 0.03, 0.04, 0.20 with 5 → 3 → 2 → 1 leaves. For any α between 0.03 and 0.04 the 3-leaf tree is optimal.
The steps in plain English:
- Grow the tree until leaves are pure (or tiny).
- Compute g(t) for every internal node, collapse the smallest, recompute, and repeat until only the root remains.
- Record the α at each step. Each subtree in the sequence is optimal on its α interval.
- Choose α by cross-validation and return the corresponding subtree.
- 1. g(t) for R(t) = 0.10, subtree error 0.04 and 3 leaves?
- 2. Which node is pruned first?
- 3. Training error along the pruning sequence…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.The algorithm and choosing α
Nested optimality (Breiman et al., 1984). For each α there is a unique smallest subtree T(α) minimising R_α. As α increases, T(α) shrinks through the nested sequence T₀ ⊃ T₁ ⊃ … ⊃ root found by weakest-link pruning. So the whole path costs only one pass of repeated collapses, not a search over exponentially many subtrees.
Cross-validated choice of α.
- Compute the sequence α₀ = 0 below α₁ below … on the full data.
- Use the geometric midpoints β_k = √(α_k·α_(k+1)) as representative α values.
- For each fold, grow and prune a tree on the training part, and record its error at each β_k on the held-out part.
- Average over folds. Pick β with the minimum CV error, or the one-standard-error rule: the largest β whose CV error is within one SE of the minimum, giving a simpler tree with statistically equivalent error.
Pruning versus early stopping. Early stopping (max depth, min gain) can halt before a useful split hidden behind a weak one (the XOR problem). Pruning sees the full tree first, so it can keep a weak split that enables strong ones below it. Its cost is growing the full tree first.
Other pruning methods. C4.5’s pessimistic error pruning (no validation needed); reduced-error pruning with a held-out set (Quinlan, 1987); minimum description length (MDL) pruning.
from sklearn.tree import DecisionTreeClassifier
path = DecisionTreeClassifier().cost_complexity_pruning_path(X, y) # alphas and impurities
cv = GridSearchCV(DecisionTreeClassifier(), {"ccp_alpha": path.ccp_alphas[:-1]}, cv=5).fit(X, y)- 1. Breiman evaluates CV error at…
- 2. The 1-SE rule picks…
- 3. Pruning can beat early stopping because…
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.Theory
Why the sequence is nested. R_α(T) is linear in α for every fixed subtree. The minimum over subtrees is a concave, piecewise-linear function of α, and its breakpoints are the α_k. Breiman et al. showed the minimisers are nested and found by weakest-link cutting, because removing a link with a larger g never helps until all smaller-g links are gone.
Statistical guarantees. Pruning by penalised empirical risk with penalty ∝ |T| is a form of structural risk minimisation. Oracle inequalities show the selected tree’s risk is close to the best trade-off between approximation and estimation (Nobel, 2002; Scott and Nowak, 2006). For classification, penalties of order √(|T|·log n / n) give adaptive rates.
The 1-SE rule (Breiman et al., 1984) is a heuristic, not a theorem. It trades a small expected loss in accuracy for a much simpler, more stable tree. Exercise 2: about 1 accuracy point lost for 0.3–0.6 fewer leaves on average.
Instability. The chosen subtree can change a lot between CV splits, especially with many near-tied g values. Repeated CV or ensembles stabilise the choice.
Interview questions.
- Why not just pick the depth by CV? Depth applies uniformly across branches, while cost-complexity pruning removes weak splits wherever they are, so trees can be deep where the signal is complex and shallow elsewhere.
- What does α mean? It is the price of a leaf in units of training error: a split survives only if it reduces training errors by more than α per added leaf.
- 1. min over subtrees of R_α(T), as a function of α, is…
- 2. The 1-SE rule is…
- 3. Cost-complexity pruning relates to…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.How much does pruning recover on noisy labels?
History.
- Breiman, Friedman, Olshen and Stone (1984) introduced cost-complexity pruning, cross-validated α and the 1-SE rule.
- Quinlan (1987) compared reduced-error, pessimistic and cost-complexity pruning.
- Mingers (1989) and Esposito, Malerba and Semeraro (1997) ran large empirical comparisons of pruning methods.
- Scott and Nowak (2006) gave minimax-optimal dyadic tree pruning.
- scikit-learn added
ccp_alphain 2019 (version 0.22).
Exercise 1. The sequence is α = 0, 0.03, 0.04, 0.20 with 5 → 3 → 2 → 1 leaves. Every subtree matched the brute-force optimum of R(T) + α|T| on its interval.
Exercise 2. Setup: a rule on 5 features with 0%, 10% or 25% of training labels flipped, n = 300, accuracy on clean test labels, 10 repetitions.
| Noise | unpruned | CV min | CV 1-SE | oracle subtree |
|---|---|---|---|---|
| 0% | 0.990 (4.5 leaves) | 0.989 (4.2) | 0.989 (4.0) | 0.990 (4.4) |
| 10% | 0.858 (42.5) | 0.978 (4.1) | 0.971 (3.8) | 0.982 (4.6) |
| 25% | 0.709 (71.3) | 0.923 (4.1) | 0.912 (3.5) | 0.923 (4.6) |
- Unpruned trees memorised the flipped labels. They grew 40–70 leaves and lost 13–28 accuracy points against the clean-label tree.
- Pruning chosen by CV recovered almost everything. It came within 0.004 of the best subtree in hindsight (the oracle), with about 4 leaves.
- The 1-SE rule gave slightly smaller trees for about 1 point less accuracy. That is a fair trade when interpretability matters.
- On clean labels there was little to prune. The full tree was already small.
Pruning in AI, robotics and aerospace
- Readable fault trees from noisy maintenance labels: prune until the rules are both small and validated.
- Certification-friendly models: small pruned trees can be reviewed line by line.
- The same principle elsewhere: pruning neural networks, lasso paths, and model-order selection for system identification.
References
- L. Breiman, J. H. Friedman, R. A. Olshen, C. J. Stone (1984). Classification and Regression Trees, chapter 3. Wadsworth.
- J. R. Quinlan (1987). “Simplifying decision trees.” International Journal of Man-Machine Studies 27.
- F. Esposito, D. Malerba, G. Semeraro (1997). “A comparative analysis of methods for pruning decision trees.” IEEE TPAMI 19(5).
- C. Scott, R. D. Nowak (2006). “Minimax-optimal classification with dyadic decision trees.” IEEE Transactions on Information Theory 52(4).
- T. Hastie, R. Tibshirani, J. Friedman (2009). The Elements of Statistical Learning, 2nd ed., section 9.2.2. Springer.