2.3.4Tree-Based & Instance Methods

Tree pruning techniques

2,139 words10 min readdifficulty · medium

WHAT is pruning?


Cost-Complexity Pruning (the important 20%)

This is the algorithm behind CART / scikit-learn's ccp_alpha. Master this and you've got 80% of exam/interview value.

Deriving the criterion from scratch

WHAT do we want? A tree that is accurate and small. Let's put both wishes in one objective.

Let TT be a tree with terminal nodes (leaves) T~\tilde{T}. Define:

  • R(T)R(T) = total error (or impurity) of the tree = tT~R(t)\sum_{t \in \tilde{T}} R(t), where R(t)R(t) is the error at leaf tt.
  • T~|\tilde{T}| = number of leaves (our complexity measure).

We penalize complexity with a knob α0\alpha \ge 0:

  • α=0\alpha = 0: no penalty → keep the full tree.
  • α\alpha \to \infty: penalty dominates → collapse to the root (single node).

So sweeping α\alpha from 00 \to \infty traces out a sequence of trees from largest to smallest.

When is it worth cutting a subtree? (the key derivation)

Take an internal node tt and the subtree TtT_t rooted at it. Compare two options:

Option A — keep the subtree TtT_t: cost =R(Tt)+αTt~= R(T_t) + \alpha|\tilde{T_t}|. Option B — prune it to a single leaf tt: cost =R(t)+α1= R(t) + \alpha\cdot 1.

We should prune when B is no worse than A: R(t)+αR(Tt)+αTt~R(t) + \alpha \le R(T_t) + \alpha|\tilde{T_t}|

Solve for α\alpha: α(1Tt~)R(Tt)R(t)\alpha\big(1 - |\tilde{T_t}|\big) \le R(T_t) - R(t) Since Tt~1|\tilde{T_t}| \ge 1, dividing flips nothing dangerous when T~t>1|\tilde T_t|>1: αR(t)R(Tt)Tt~1\alpha \ge \frac{R(t) - R(T_t)}{|\tilde{T_t}| - 1}

WHY numerator R(t)R(Tt)R(t)-R(T_t)? It's the error you gain by collapsing (the subtree was more accurate, so R(t)R(Tt)R(t)\ge R(T_t), numerator 0\ge 0). WHY divide by T~t1|\tilde T_t|-1? You're spreading that error-cost over the number of leaves you remove. It's "extra error per leaf saved" — a fair per-unit price.

  1. Grow the full tree T0T_0.
  2. For every internal node compute αeff\alpha_{\text{eff}}.
  3. Prune the node with the smallest αeff\alpha_{\text{eff}} → get a smaller tree; record that α\alpha.
  4. Repeat until only the root remains.
  5. You now have a nested sequence T0T1{root}T_0 \supset T_1 \supset \dots \supset \{\text{root}\} with increasing α1<α2<\alpha_1 < \alpha_2 < \dots
  6. Pick the best α\alpha by cross-validation (the tree with lowest CV error).
Figure — Tree pruning techniques

Reduced-Error Pruning (simpler alternative)


Pre-pruning knobs (scikit-learn)

Knob Meaning Effect
max_depth max levels shallower tree
min_samples_split min samples to split a node fewer splits
min_samples_leaf min samples per leaf smoother leaves
min_impurity_decrease required impurity drop to split blocks weak splits
ccp_alpha cost-complexity α\alpha post-pruning

Worked Examples


Common Mistakes (Steel-manned)


Recall Feynman: explain to a 12-year-old

Imagine you drew a family tree of "yes/no" questions to guess an animal. If you keep asking questions, eventually every animal gets its own tiny box — but some questions are silly ("does its name have 7 letters?") and only work for the animals you already saw. Pruning is snipping off those silly branches so the tree stays smart about new animals it hasn't met. We test each branch: "if I cut you, do I still guess well on animals I hid earlier?" If yes → snip! Simpler tree, still smart.


Connections


Flashcards

What are the two families of tree pruning?
Pre-pruning (early stopping while growing) and post-pruning (grow full, then cut back).
Write the cost-complexity objective.
Rα(T)=R(T)+αT~R_\alpha(T) = R(T) + \alpha|\tilde T|, error plus a penalty α\alpha times number of leaves.
What tree do you get at α=0\alpha=0 vs α\alpha\to\infty?
α=0\alpha=0 → full unpruned tree; α\alpha\to\infty → just the root node.
Formula for a node's effective alpha.
αeff(t)=R(t)R(Tt)T~t1\alpha_{\text{eff}}(t) = \dfrac{R(t)-R(T_t)}{|\tilde T_t|-1}.
In weakest-link pruning, which node is pruned first?
The internal node with the smallest αeff\alpha_{\text{eff}} (cheapest error-per-leaf-saved).
Why is post-pruning generally better than pre-pruning?
Pre-pruning is greedy/short-sighted (horizon effect); post-pruning sees the whole subtree's value before cutting (handles XOR-like splits).
How do you choose the best α\alpha?
By cross-validation (or a validation set) — pick the α\alpha minimizing CV error, not training error.
What is Reduced-Error Pruning?
Bottom-up, replace each subtree with a majority-class leaf; keep the replacement if validation accuracy doesn't drop.
Why can't you use training error to decide pruning?
Training error monotonically decreases with more splits, so it never signals to prune — you need held-out data.
Name three pre-pruning hyperparameters.
max_depth, min_samples_leaf, min_impurity_decrease (also min_samples_split, ccp_alpha).
Effect of increasing α\alpha on tree size?
Larger α\alpha → smaller tree (heavier penalty on leaves).
Danger of over-pruning?
Underfitting — bias rises, both training and test error can worsen.

Concept Map

memorizes noise

motivates

reduces

improves

family 1

family 2

greedy, misses XOR

sees full subtree

objective

alpha=0

alpha to inf

cut when

formula

Fully grown tree

Overfitting

Pruning

Tree complexity

Generalization

Pre-pruning early stopping

Post-pruning

Short-sighted

Cost-complexity pruning

R_alpha = R T + alpha leaves

Root only

Effective alpha of node

alpha = R t - R Tt over leaves - 1

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, decision tree ka ek problem hai: agar usko roko mat, toh woh training data ko poora ratta maar leta hai — har chhota noise ke liye alag branch bana deta hai. Isse training pe accuracy 100% lagti hai, par naye (test) data pe woh flop ho jaata hai. Isko overfitting kehte hain. Pruning ka matlab hai — is over-grown tree ki extra branches ko kaat dena, taaki tree simple rahe aur naye data pe achha generalize kare.

Do tarike hain. Pre-pruning (early stopping): tree banate waqt hi rok do — jaise max_depth set kar do ya bolo "kam se kam itne samples ho tab hi split karo". Post-pruning: pehle poora bada tree banao, phir baad mein kaat-chhaant karo. Post-pruning zyada acha hai kyunki kabhi-kabhi ek split abhi bekaar lagta hai par uske neeche ke splits bahut valuable hote hain (XOR wala case) — pre-pruning yeh miss kar deta hai.

Sabse important formula: Rα(T)=R(T)+αT~R_\alpha(T) = R(T) + \alpha|\tilde T|. Yaha R(T)R(T) error hai aur T~|\tilde T| leaves ki ginti (size). α\alpha ek "tax" hai jo bade tree pe lagta hai. Yaad rakho: bada α\alpha → chhota tree. Har node ka ek αeff=R(t)R(Tt)T~t1\alpha_{\text{eff}} = \frac{R(t)-R(T_t)}{|\tilde T_t|-1} nikaalte hain — jiska yeh sabse chhota, wahi "weakest link", woh pehle katega. Aur sabse important baat: sahi α\alpha cross-validation se choose karo, training error se kabhi nahi — kyunki training error toh hamesha ghatta hi rehta hai jitne zyada split karo.

Go deeper — visual, from zero

Test yourself — Tree-Based & Instance Methods

Connections