Foundations to Frontier
Tests
D. Classical Machine Learning › D6 Decision Trees

Cost-Complexity Pruning

A tree grown until its leaves are pure fits the noise. Cost-complexity pruning (Breiman et al., 1984) grows the full tree first and then trims it back. Each subtree is scored by training error plus α times its number of leaves, and as α rises the optimal subtrees form a nested sequence obtained by repeatedly removing the weakest link, the split that buys the least error reduction per extra leaf. Cross-validation then picks α, often with the one-standard-error rule. This page covers the penalised criterion, the weakest-link algorithm and why it yields optimal nested subtrees, Breiman's CV recipe and the 1-SE rule, and how pruning compares with early stopping. In the exercises you implement the pruning sequence and check it against brute force; then you measure how much CV pruning recovers on noisy data.

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.

Cost-complexity pruning: cut the weakest links first
leaves (log scale) →error
α = 0.00 errors per leaf · leaves 43 · training error 0.0% · error on fresh data 19.8% (best: 7.1% with 6 leaves, ringed)
Step 0 is the full tree: zero training error, carved around every flipped label. Each step removes the split(s) with the smallest error reduction per leaf. Training error only rises, but the error on fresh data first falls as the noise-chasing splits go, then rises when real structure is cut. Here α is measured in training errors per leaf.
Why engineers care

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.

📝 Quick check (Level 0)
0/3 answered
  1. 1. CART prunes…
  2. 2. Raising α makes the pruned tree…
  3. 3. In the demo, error on fresh data as you prune…

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

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
  1. 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.
  2. 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.
  3. 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:

  1. Grow the tree until leaves are pure (or tiny).
  2. Compute g(t) for every internal node, collapse the smallest, recompute, and repeat until only the root remains.
  3. Record the α at each step. Each subtree in the sequence is optimal on its α interval.
  4. Choose α by cross-validation and return the corresponding subtree.
📝 Quick check (Level 1)
0/3 answered
  1. 1. g(t) for R(t) = 0.10, subtree error 0.04 and 3 leaves?
  2. 2. Which node is pruned first?
  3. 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 α.

  1. Compute the sequence α₀ = 0 below α₁ below … on the full data.
  2. Use the geometric midpoints β_k = √(α_k·α_(k+1)) as representative α values.
  3. For each fold, grow and prune a tree on the training part, and record its error at each β_k on the held-out part.
  4. 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)
🐍 Exercise 1: the weakest-link pruning sequence
Implement leaf error, subtree error and leaf count, the weakest-link pruning sequence (collapsing all ties), the penalised cost and a brute-force optimal subtree for a given α; verify the sequence on a hand-sized tree and that every subtree is optimal on its α interval.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Breiman evaluates CV error at…
  2. 2. The 1-SE rule picks…
  3. 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.
📝 Quick check (Level 3)
0/3 answered
  1. 1. min over subtrees of R_α(T), as a function of α, is…
  2. 2. The 1-SE rule is…
  3. 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_alpha in 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.
🐍 Exercise 2 (research): choosing the pruning level
Grow full CART trees on data with 0%, 10% or 25% flipped labels; pick α by 5-fold CV (minimum rule and 1-SE rule) and compare test accuracy and leaves with the unpruned tree and the best subtree in hindsight (10 repetitions).

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

  1. L. Breiman, J. H. Friedman, R. A. Olshen, C. J. Stone (1984). Classification and Regression Trees, chapter 3. Wadsworth.
  2. J. R. Quinlan (1987). “Simplifying decision trees.” International Journal of Man-Machine Studies 27.
  3. F. Esposito, D. Malerba, G. Semeraro (1997). “A comparative analysis of methods for pruning decision trees.” IEEE TPAMI 19(5).
  4. C. Scott, R. D. Nowak (2006). “Minimax-optimal classification with dyadic decision trees.” IEEE Transactions on Information Theory 52(4).
  5. T. Hastie, R. Tibshirani, J. Friedman (2009). The Elements of Statistical Learning, 2nd ed., section 9.2.2. Springer.