2.3.4 · D4Tree-Based & Instance Methods

Exercises — Tree pruning techniques

2,551 words12 min readBack to topic

Before we start, one shared picture of the machinery. Everything on this page lives on the cost-complexity curve: as we raise the penalty knob , the tree shrinks in discrete jumps.

Figure — Tree pruning techniques

Read that figure like a story: on the far left () we keep the giant full tree; each vertical dashed line is an value at which one node gets guillotined; on the far right the tree has collapsed to just the root. The exercises below make you compute those jump points and choose between the trees.

Quick symbol refresher (all built in the parent):

  • ::: total error/impurity of tree (sum of errors over its leaves)
  • ::: number of leaves (terminal nodes) of — our measure of size
  • ::: cost-complexity — accuracy pull plus a size tax
  • ::: the at which node becomes worth pruning

Level 1 — Recognition

Recall Solution 1.1
  • : no size tax, so the cost is pure error → the full tree wins → (b) and (c) (a fully grown tree has the lowest training error).
  • : the term dominates everything, so the cheapest tree is the one with the fewest leaves — the root(a).

Picture: on figure s01, (b)/(c) are the far-left end, (a) is the far-right end.

Recall Solution 1.2

Pre-pruning stops the tree early (decides before/while growing); post-pruning grows fully then chops.

  • max_depthpre (caps growth)
  • ccp_alphapost (cost-complexity, applied after full growth)
  • reduced-error pruning → post (validation-set chopping)
  • min_samples_leafpre
  • min_impurity_decreasepre

Mnemonic: "PRE stops, POST chops."


Level 2 — Application

Recall Solution 2.1

What this means: is the "price per leaf removed." Only once the penalty does collapsing this node pay for itself.

Recall Solution 2.2

Since , prune to . We paid extra training error to shed leaves; at this the trade is a net win.

Recall Solution 2.3

Now , so keep the full tree . The same two trees — only changed — and the decision flipped. This is exactly why must be chosen with care (Cross-Validation).


Level 3 — Analysis

Recall Solution 3.1

Compute for each and take the smallest: Smallest is , so node C is the weakest link — it's cheapest to remove per leaf saved, so it gets guillotined first. (A and B tie at ; a tie is broken by convention, e.g. lower node id — but neither is first here.)

Recall Solution 3.2

A subtree is grown by splitting node to reduce impurity/error. Each split can only keep or lower the training error, so the whole subtree's error is the collapsed-node error . Hence . A negative numerator would mean the subtree is worse on training data than the single leaf — impossible for a properly grown tree because CART never accepts a split that increases training error. If you ever see it, your and are on different datasets (e.g. validation vs training) — that's a different (and valid) game, but not cost-complexity's numerator. See Decision Trees (CART).

Recall Solution 3.3

At each step we prune the node with the smallest current and record that ; the next step's smallest is the previous, so recorded thresholds increase: The sequence is nested because every prune only removes a subtree from the previous tree — we never add anything back. So each is literally with one subtree collapsed → . On figure s01 this is why the tree size only steps down as we cross each dashed line, never up.


Level 4 — Synthesis

Recall Solution 4.1

Pick the with minimum CV error: (CV error ). Shape: the curve is U-shaped in .

  • Left side ( small): tree is large → high variance, it overfits noise, CV error high (Bias-Variance Tradeoff).
  • Right side ( large): tree is tiny → high bias, it underfits, CV error high again.
  • The minimum is the sweet spot. This is exactly why "just crank " is wrong — past the bottom of the U, error climbs.
Recall Solution 4.2

REP rule: replace the subtree with a leaf if validation accuracy does not decrease (leaf keep).

  • N1: leaf prune (accuracy even rose — subtree was overfitting).
  • N2: leaf prune (tie; simpler wins by Occam).
  • N3: leaf keep (pruning would lose real signal). Why validation and not training: on the training set the subtree always ties or beats the leaf, so training never votes to prune. The validation set is data the tree did not memorize, so it exposes fake (noise-fitting) splits.

Level 5 — Mastery

Recall Solution 5.1

Derivation. Keeping costs . Collapsing to one leaf costs . Prune when collapsing is no worse: Since , divide safely: Break-even for this node: So pruning is at least as good for all ; below that, keeping the subtree wins. This ties back to Regularization: is a regularization strength on tree size.

Recall Solution 5.2

(a) They tie at . (b) Both become worth pruning at the same . Weakest-link breaks ties by a fixed rule (e.g. prune the node that removes more error, or lowest node index); either way both are recorded at and the sequence collapses them one after another at that same threshold. So recorded : first prune at , second prune at . (c) Prune P first: it removes leaves. Remaining leaves . (Then pruning Q removes leaves → ; but Q sits in the original leaf count — since P and Q are disjoint, after both prunes: leaf, i.e. the root plus P and Q each as leaves would over-count; disjoint means removing then from the , leaving ? Careful: leaves total, P's subtree owns , Q's owns — but , so they cannot both be disjoint subtrees of a 10-leaf tree.)

Resolution of (c): the disjoint counts exceed the tree's leaves, which is impossible — so as stated the two subtrees cannot both be disjoint inside . The teaching point: always sanity-check that . Taking the intended answer for the first prune only: after pruning P, leaves remain, and its recorded .

Recall Solution 5.3

For: pruning each tree lowers per-tree variance and shrinks memory/inference cost. Against: a random forest already controls variance by averaging many high-variance, low-bias trees — pruning raises each tree's bias, and the ensemble relies on deep, decorrelated trees to average well, so heavy pruning can hurt accuracy. Verdict: light pruning (or a small ccp_alpha) can trim size cheaply, but the aggressive pruning we'd use for a single tree is usually counterproductive in a forest — the variance reduction it targets is already handled by averaging.


Recall One-paragraph self-check (cover the answers)

The knob is a ::: tax on the number of leaves (bigger → smaller tree) The weakest link is the node with the ::: smallest divides by because ::: that is the number of leaves actually removed when the subtree collapses The CV-error-vs- curve is shaped like ::: a U (overfit on the left, underfit on the right) REP uses a validation set instead of training because ::: training error never votes to prune (it always drops with more splits)

Connections