2.3.5 · D4Tree-Based & Instance Methods

Exercises — Overfitting in decision trees

2,760 words13 min readBack to topic

Before we start, one quick symbol refresher so nothing is used unexplained:


Level 1 — Recognition

Exercise 1.1 (L1)

A model scores train 99%, validation 71%. In one word, what is happening, and which term of the bias–variance decomposition is large?

Recall Solution

Overfitting. A near-perfect training score with a much worse validation score is the textbook signature.

  • Why this step? The gap (99 − 71 = 28 points) means the model learned training-specific noise. In the decomposition , that is the variance term being large: tiny wobbles in the data got baked into the tree.

Exercise 1.2 (L1)

Match each tree to underfit / good / overfit: (a) train 70%, val 69%; (b) train 100%, val 74%; (c) train 92%, val 90%.

Recall Solution
  • (a) Underfit — both scores low and close: the tree is too simple to capture the pattern (high bias).
  • (b) Overfit — perfect train, big gap (26 pts): high variance.
  • (c) Good — high train, small gap (2 pts): the sweet spot.
  • Why this step? We read level (are both high?) and gap (train − val). Low-and-close = bias problem; high-and-wide = variance problem.

Exercise 1.3 (L1)

True or false: "Adding one more split to a decision tree can increase its training error."

Recall Solution

False. A greedy split is only accepted if it does not raise training error — it either lowers it or leaves it unchanged.

  • Why this step? This is exactly why trees overfit: training error can only fall as you grow, so pure greed always says "split more," and nothing stops the memorization.

Level 2 — Application

Exercise 2.1 (L2)

A subtree has leaves and training error . Collapsing it to a single leaf gives . Find its effective .

Recall Solution

The effective- (weakest-link) formula from the parent note:

  • Why this formula? is the price per leaf at which keeping the subtree exactly ties with pruning it. The numerator is the accuracy you lose by collapsing; the denominator is the number of leaves you save (remove 5, add 1 back = net 4). So = error paid per leaf removed = 0.03. Meaning: for any penalty this subtree is not worth its extra leaves and gets pruned.

Exercise 2.2 (L2)

Two subtrees compete to be pruned first:

  • : , , .
  • : , , .

Which is the weakest link (pruned first)?

Recall Solution

They tie at . Both are equally weak links.

  • Why this step? The "weakest link" is the node with the smallest effective — it costs the least accuracy per leaf saved, so it goes first. Here both cost the same 0.03 per leaf, so either can be cut first; standard implementations prune both at that level.

Exercise 2.3 (L2)

From the parent's Example 1 style, a tree gives: depth 3 → train 88%, val 85%; depth 6 → train 97%, val 90%; depth 10 → train 100%, val 82%. Pick the best depth and state the gap there.

Recall Solution

Best depth = 6. It has the highest validation accuracy (90%) and a modest gap of points.

  • Why this step? We choose the depth that maximizes validation accuracy, the bottom of the U-curve — not the one with the best training score. Depth 10 is perfect on train but has the worst val (82%) and largest gap (18 pts) = overfit.
Figure — Overfitting in decision trees

Level 3 — Analysis

Exercise 3.1 (L3)

Cost-complexity pruning produces a nested sequence of trees as rises. Explain what happens to bias and variance as goes from to , and where the best sits.

Recall Solution
  • : no penalty on leaves → the full, deepest tree survives → low bias, high variance (overfit).
  • : every leaf is unaffordable → the tree collapses to a single root leaf (predict one value for everyone) → high bias, low variance (underfit).
  • As increases, the weakest links are cut first, so the tree shrinks smoothly: bias rises, variance falls.
  • Best : the one whose tree has the highest cross-validated score — the balance point of the bias–variance tradeoff.
  • Why this step? is a knob that trades capacity for stability, exactly like a regularization strength. We don't guess it — we let cross-validation find the U-curve's bottom.

Exercise 3.2 (L3)

On XOR data (label depends on the combination of two features), pre-pruning with min_impurity_decrease = 0.05 refuses the first split and post-pruning keeps the tree. Explain why the first split shows near-zero impurity gain, yet the tree is still worth building.

Recall Solution

Look at the figure: XOR has two classes arranged like a checkerboard.

Figure — Overfitting in decision trees

  • A single cut on feature splits the plane left/right. But each side still holds a 50/50 mix of both classes — so impurity barely drops. The gain looks useless (< 0.05).
  • Yet after that first cut, a second cut on inside each half perfectly separates the classes. The first split is the doorway to two excellent splits.
  • Pre-pruning is myopic: it judges the first split in isolation, sees no gain, and stops → single node → underfit.
  • Post-pruning is global: it grows the full tree, sees the two useful splits below, and keeps the whole structure.
  • Why this step? This is the textbook demonstration that "small gain now" ≠ "useless" — the classic reason to prefer post-pruning.

Exercise 3.3 (L3)

A student says: "My tree with max_depth=3 has train 91% and val 90%. Since the gap is tiny, I've beaten overfitting — I'm done." Critique this.

Recall Solution

The small gap only rules out overfitting; it does not rule out underfitting. Both scores could be capped low because the tree is too simple to express the true pattern.

  • Diagnostic: grow a deeper tree. If deeper trees reach higher validation accuracy (say 96%), then depth 3 was underfitting — leaving signal on the table.
  • Why this step? A tight train–val gap is necessary but not sufficient for a good model. You must also confirm the validation accuracy is as high as it can reasonably get, i.e. you're at the U-curve bottom, not stuck up its left wall.

Level 4 — Synthesis

Exercise 4.1 (L4)

Design a full anti-overfitting pipeline for a decision-tree classifier, in order, justifying each stage.

Recall Solution

A defensible pipeline:

  1. Split data into train / validation / test (or use k-fold cross-validation). Why: you cannot detect overfitting without unseen data.
  2. Grow the full tree on the training set. Why: post-pruning needs the whole tree to avoid the XOR-style myopia of early stopping.
  3. Cost-complexity prune to get the nested sequence of trees, one per in the weakest-link order. Why: this yields a small, ordered menu of candidate trees instead of blindly guessing knobs.
  4. Cross-validate each candidate; pick the with the best average validation score. Why: this locates the bottom of the U-curve robustly, not on one lucky split.
  5. (Optional) Ensemble with random forests if variance is still high. Why: averaging many de-correlated deep trees drives variance down by roughly with little bias cost.
  6. Report on the untouched test set once, at the end. Why: the test set stays sacred so your accuracy estimate isn't inflated by tuning.
  • Why this ordering? Each stage attacks a different failure: (1) enables detection, (2–4) tame variance via the tradeoff knob, (5) is a stronger variance hammer, (6) keeps the final number honest.

Exercise 4.2 (L4)

You have three pruned trees from the sequence:

  • : 20 leaves, CV accuracy 0.86
  • : 9 leaves, CV accuracy 0.885
  • : 4 leaves, CV accuracy 0.87

Using the "1-standard-error" style simplicity preference (pick the smallest tree within a small tolerance of the best), which do you deploy if the tolerance is 0.02?

Recall Solution
  • Best CV accuracy = 0.885 (). Tolerance band = .
  • Trees within the band (accuracy ): (0.885) and (0.87). at 0.86 is outside the band.
  • Among the qualifying trees, pick the smallest: with 4 leaves.
  • Deploy .
  • Why this step? When two trees are statistically comparable, the simpler one generalizes more safely (lower variance, easier to interpret). The tolerance stops us from chasing a tiny, possibly-noise accuracy bump into a bushier, riskier tree.

Level 5 — Mastery

Exercise 5.1 (L5, edge case)

Suppose a subtree does not reduce training error at all — a greedy tie so , with . Compute its effective and say what it means.

Recall Solution

Effective . This subtree costs zero training accuracy to prune.

  • Meaning: it is the weakest possible link — the very first thing pruned as soon as any penalty is applied. It adds 3 leaves and buys nothing on the training set (pure over-fitting fluff).
  • Why this step? The formula gracefully handles the degenerate "no-gain" subtree: numerator 0 → → cut immediately. This is the mechanism by which cost-complexity pruning automatically deletes splits that only chase noise.

Exercise 5.2 (L5, limiting behaviour)

Show the two extreme trees produced by cost-complexity pruning as and , and give each one's bias/variance character.

Recall Solution
  • : penalty vanishes → the full unpruned tree (every leaf free). One point per leaf in the limit → train error 0, minimum bias, maximum variance → overfit.
  • : every leaf priced out of existence → the tree collapses to a single root leaf predicting the majority class for everyone → maximum bias, minimum (zero) variance → underfit.
  • The best model lives strictly between these poles, chosen by cross-validation.
  • Why this step? Confirming both endpoints proves the knob spans the entire bias–variance spectrum, so a good middle must exist.

Exercise 5.3 (L5, design + numeric)

A dataset has an irreducible noise variance . Your deep tree achieves test MSE on training points but on test points, with . Using , solve for the variance and state the diagnosis.

Recall Solution

Rearrange the decomposition:

  • Variance = 0.23, which dwarfs both the and the noise floor .
  • Diagnosis: severe overfitting — the variance term dominates the test error.
  • Cure: prune (raise ) or ensemble with random forests to slash that 0.23 variance; bias is already tiny so we can afford to trade a little of it away.
  • Why this step? Splitting test error into its three parts tells you which lever to pull: a giant variance term means "reduce capacity / average trees," not "add complexity."


Connections