2.3.4 · D2Tree-Based & Instance Methods

Visual walkthrough — Tree pruning techniques

2,667 words12 min readBack to topic

We answer one question: out of an over-grown tree, which branch do we cut first, and when?


Step 1 — What an over-grown tree even looks like

Figure — Tree pruning techniques

Two names we will reuse constantly, anchored to that picture:

The tilde () just means "the leaves of", so = "the set of leaves of tree ", and the bars mean "how many are in this set" — the same bars you'd use to count marbles in a bag.


Step 2 — Measuring how wrong the tree is

Figure — Tree pruning techniques

Step 3 — Two wishes fighting: one knob to referee them

Figure — Tree pruning techniques

Why this tool — why addition and a multiplier, not something fancier? Because we want a straight-line exchange rate: one extra leaf always costs exactly more, no matter how many you already have. That linear "one price per leaf" is the simplest fair penalty — the same idea behind regularization, where we add a penalty term to stop a model from getting too complex.

The two extremes (memorize these — they are the whole story):

rent per leaf what wins result
free accuracy only keep the full tree
infinite simplicity only collapse to the root

So sliding from up to walks us from the giant tree down to a single node — a whole family of trees, one per .


Step 4 — The real question: cut this branch, or keep it?

Figure — Tree pruning techniques

Two more names, both read straight off s04:


Step 5 — Writing both options' costs and letting them race

Figure — Tree pruning techniques

Cost of each option, term by term:

We should cut when cutting costs no more than keeping:


Step 6 — Solving for the tipping-point

Figure — Tree pruning techniques

Move all -terms to one side, error-terms to the other:

Now divide by . Careful: since a real subtree has more than one leaf, , so is negative — and dividing an inequality by a negative number flips the into :

Multiply top and bottom by (flips both signs, cancels out — inequality unchanged) to make it read cleanly:

Reading every symbol:

  • Numerator = the extra error you pay by collapsing (Step 4 said it's ). Small numerator ⇒ the subtree was barely helping ⇒ cheap to cut.
  • Denominator = how many leaves you delete (you go from leaves down to ). Big denominator ⇒ you free lots of "rent" ⇒ better deal.
  • The whole fraction = extra error per leaf saved — a fair price per unit, so nodes of different sizes can be compared apples-to-apples.

Step 7 — Edge cases you must never trip on

Figure — Tree pruning techniques
  • A single-leaf "subtree" (): denominator division by zero, undefined. But this is fine: a subtree that is already one leaf has nothing below it to cut. We simply never compute for leaves — only for internal nodes, where .
  • A perfectly pure subtree (): then . Since , this is — well-defined. It just means "cutting costs you the collapsed leaf's full error."
  • A split that helped nothing (): numerator . This node dies at the very first whiff of penalty (). Correct — a split with zero accuracy gain but extra leaves is pure waste.

Step 8 — Sweeping builds a nested family of trees

Figure — Tree pruning techniques

The loop:

  1. Grow the full tree .
  2. Compute at every internal node.
  3. Prune the smallest- node → tree ; record that as .
  4. Recompute and repeat → until only the root is left.
  5. Result: a nested chain with increasing ( means "contains" — each tree is a pruned-back version of the one before).

Which tree in the chain is best? Not the training-error winner (that's always — see the mistake below). We pick with cross-validation: try each , measure error on held-out folds, keep the with the lowest CV error. The CV curve is U-shaped — too little pruning overfits (high variance), too much underfits (high bias).


Worked check — the numbers behind the pictures


The one-picture summary

Figure — Tree pruning techniques

Figure s09 compresses the whole story: the -axis at the bottom; above it the shrinking trees ( root) appearing as climbs past each recorded ; and overlaid, the U-shaped CV-error curve whose minimum marks the we finally choose.

Recall Feynman retelling — the walkthrough in plain words

You grew a huge tree that memorized everything, even the flukes. Every leaf is a room, and every room charges rent . Your total cost is mistakes you make plus rent on all your rooms. When rent is free () you keep every room; when rent is sky-high you knock the whole house down to one room. To decide which branch to knock down first, you look at each branch and ask: "if I collapse this bushy branch into one room, how much extra wrongness do I take on, and how many rooms of rent do I stop paying?" Extra-wrongness ÷ rooms-saved is the branch's price per room — its . The branch with the cheapest price is the weakest link; you cut it first. Cut, recompute, cut again — the prices you pay only go up — until just the root remains. That gives you a whole ladder of trees, tiny to huge. You don't trust your own homework (training error) to pick one, because homework always says "keep everything." You test each tree on hidden animals (cross-validation). Too big overfits, too small underfits — so the error curve dips in the middle. You pick the dip. Done.


Connections