Cost-Complexity Pruning
A grown-out decision tree almost always overfits. Cost-complexity pruning trims it back systematically by penalizing tree size directly, producing a single tuning parameter that trades training fit against tree complexity.
Prerequisites: Decision Trees and CART
Grow a decision tree without stopping and it keeps splitting until every leaf holds one or two observations — a perfect training fit and a badly overfit model. You could stop early with a fixed rule, but that requires guessing a threshold in advance and applies it uniformly, even to branches that would have benefited from growing further. Cost-complexity pruning grows the tree all the way out, then trims it back afterward, guided by an explicit penalty on size.
The analogy: growing a hedge, then trimming it back
Rather than guessing in advance how tall each part of a hedge should be, let it grow out fully and unevenly. Then trim it back using one rule: cut any branch whose extra height isn't earning its keep relative to how much taller it makes the hedge overall. Different parts get cut back by different amounts, proportional to what they actually contribute.
The mechanics: penalizing tree size directly
Define a cost over tree with leaves:
In plain English: the first term is training error; the second charges per leaf, so a bigger tree pays a bigger tax regardless of fit. At the fully-grown tree wins outright; as rises, small fit gains from extra leaves stop being worth their cost, and the optimal subtree shrinks. Pruning computes, as increases from 0, the nested sequence of optimal subtrees, then picks the (and subtree) that does best on held-out data.
Worked example: pruning a small tree
A fully-grown tree for predicting default has 6 leaves, training error 4. Collapsing two sibling leaves into one gives 5 leaves, error 6. At : 6-leaf tree wins (leaves free). At : 6-leaf costs , pruned costs — full tree still wins. At : 6-leaf costs , pruned costs — now pruning wins, since the extra leaf's marginal gain no longer justifies its cost. Sweeping this way and cross-validating picks the value that generalizes best.
Drag model complexity above and watch training error keep falling while test error eventually turns back up — exactly the trade-off controls: a large tree (low ) chases training error down but overfits, while enough pruning (higher ) finds the complexity that minimizes error on new data.
What this means in practice
Cost-complexity pruning turns "grow a tree" into a properly regularized procedure: grow fully (cheap), then use held-out data to pick how far to trim, rather than guessing stopping rules while growing. It's why "depth" and "leaf count" are treated as tunable regularization knobs in every tree-based method, not arbitrary settings.
Cost-complexity pruning grows a tree fully, then trims using — a single penalty on size, with chosen by cross-validation rather than fixed in advance.
Don't confuse pruning with early stopping (capping depth while growing). Early stopping applies uniformly and can block a genuinely useful split simply because it comes late; pruning after full growth trims different branches by different amounts based on what they actually contribute.
Related concepts
Practice in interviews
Further reading
- Breiman, Friedman, Olshen & Stone, Classification and Regression Trees (1984), ch. 3
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning, ch. 9.2