A tree that gets 100% of its training data right sounds perfect. Is it?
The top row is training data — five of those points are quietly mislabeled, noise a real dataset would have. Drag depth up: watch the tree stop finding the boundary and start carving out tiny one-point regions instead, each one built to rescue a single mislabeled point.
Nothing stops a tree from growing until every training point is classified perfectly — split enough times and you can isolate any single point into its own leaf. The question isn't whether it can memorize noise, it's whether doing so helps on data it hasn't seen:
- gap — the difference between training and validation accuracy; a growing gap signals overfitting.
- — the fraction of training points the tree classifies correctly.
- — the fraction of held-out (never-trained-on) points the tree classifies correctly.
The gap tells you that a tree is overfitting, but it needs a validation set to compute. Cost-complexity pruning finds the right size using only training data, by charging a fixed toll for every leaf a tree keeps:
- — tree 's cost-complexity score at penalty strength ; smaller is better.
- — 's training misclassification rate, .
- — the price charged per leaf; charges nothing, larger charges more.
- — 's leaf count, i.e. how many regions it carved feature space into.
- alpha=0 reduces to pure training error
With no penalty, is just — whichever tree fits training data best wins, no matter how many leaves it took to get there. This is exactly the overfitting problem, restated as an objective.
- Growing alpha makes extra leaves cost more than they're worth
Each split only pays for itself if the training-error improvement it buys is bigger than . As rises, splits that were only rescuing one or two noisy points stop being worth their toll first.
- This finds the right complexity blind — no validation set needed
Real pruning algorithms (e.g. scikit-learn's
ccp_alpha) minimize exactly this objective. It's not magic: it works because acts as a stand-in for "how much do I trust that this split reflects real structure, not noise" — the same judgment a validation set makes empirically.
At α = 0, cost-complexity pruning picks depth 5 (10 leaves, R_α = 0.04) — using only training data.
The bottom row is validation data the tree never trained on — clean, no mislabeled points. Depth 1 or 2 already gets every single one right. Push the depth slider past that and watch validation accuracy start dropping while training accuracy keeps climbing. The α buttons show a different way to catch this: cost-complexity pruning picks a depth using only the training-side numbers above, no validation peek required — watch its pick shrink as α grows.
- Depth 1, the shallow tree
One split at :
- Training accuracy: (the 5 mislabeled points are the only misses)
- Validation accuracy:
- Leaves: , so
- Depth 5, the overgrown tree
Splitting all the way to depth 5, rescuing 4 of the 5 noisy points:
- Training accuracy:
- Validation accuracy: — it falls even as training accuracy climbed
- Leaves: , so
(Depth 6 adds one more leaf for zero extra training accuracy, so it never wins at any — pruning drops that redundant split for free.)
- alpha=0: raw training error wins, and the overgrown tree looks unbeatable
, while . Depth 5 wins by this metric alone — exactly the overfitting trap, since it's also the worse tree on validation data.
- alpha=0.02: an exact tie, and ties favor the smaller tree
and — dead even. Real weakest-link pruning (and the code here) breaks such ties in favor of the smaller tree, so is already enough to recover depth 1 — and depth 1 is also the validation-optimal tree.
- alpha=0.03: no longer even close
, while . Depth 1 wins outright now — cost-complexity pruning found the right size using only training-side arithmetic, no validation set required.
Pick an α large enough that cost-complexity pruning — reasoning from training data alone — lands on the same depth that reaches the best possible validation accuracy, 100%. A small α still lets the overgrown tree win; you need one where the leaf penalty actually bites.
Training accuracy alone can't tell memorization apart from generalization — a tree can always reach 100% on data it's already seen, and inherits that blind spot exactly. But charge a per-leaf toll and raise it: at some , the leaves that only existed to rescue noise stop paying for themselves, and the objective's own minimum shifts from the overgrown tree to the right-sized one — the same tree a validation set would have picked, found without ever looking at one.