Part V — Non-Linear Models, Trees, Ensembles & Kernel Methods · Chapter 3

Ensembles I: Bagging & Random Forests

Hook

Last chapter, pruning a tree back was the only way to stop it from memorizing noise. What if you didn't have to prune at all?

Intuition
1 tree — validation accuracy = 79%

Every tree here is grown to the same reckless max depth as Chapter 8's worst offender — each one individually overfits. But each is trained on a different random resample of the data, so each one overfits differently. Add more of them and watch the jagged, noise-chasing regions smooth out into the one boundary they all actually agree on.

Formalize

This is bagging — bootstrap aggregating. Train NN trees, each on a random sample drawn with replacement from the training set (a "bootstrap sample"), then combine their predictions by majority vote:

y^(x)=majority(T1(x),T2(x),,TN(x))\hat{y}(x) = \text{majority}\big(T_1(x), T_2(x), \ldots, T_N(x)\big)
  • y^(x)\hat{y}(x) — the ensemble's final prediction for input xx.
  • Ti(x)T_i(x) — the prediction of the ii-th tree in the forest.
  • NN — the number of trees in the ensemble.
  • majority — the vote rule: whichever label the most trees predicted wins.
  1. Each tree still overfits, but differently

    Each TiT_i still overfits its own bootstrap sample. But the noise it overfits to is specific to which points that resample happened to include — different resamples chase different noise, so it rarely lines up.

  2. Random forests also subsample features

    A random forest adds one more trick on top: at every split, each tree only gets to consider a random subset of the available features.

  3. Not in play with a single feature

    With just one feature here, that trick has nothing to act on — bagging alone is already the whole story.

Play
1 tree — train = 84%, validation = 79%

Watch training accuracy stay roughly flat while validation accuracy climbs as trees are added. No single tree in the forest got any less overfit — the ensemble generalizes better anyway, purely because their individual mistakes don't overlap.

Worked example

At x=14.25x = 14.25 (true label B):

  1. Vote with the first three trees

    B, A, A — majority vote says A, wrong.

  2. Add two more trees

    Predicting B and B, the vote among all five becomes B, A, A, B, B — majority B, correct.

  3. Notice what actually changed

    No tree changed its mind; two more independent (wrong-in-different-ways) opinions were enough to outvote the two that agreed on the mistake.

Checkpoint

Grow the forest until validation accuracy reaches its best — 92%. One tree alone won’t get there.

1 tree — train = 84%, validation = 79%
Move the tree-count slider to try it
Summary
y^(x)=majority(T1(x),T2(x),,TN(x))\hat{y}(x) = \text{majority}\big(T_1(x), T_2(x), \ldots, T_N(x)\big)

Bagging doesn't fix any individual tree's tendency to overfit — it cancels it out. As long as each tree's mistakes are driven by different noise, averaging enough of them together drowns the noise out and leaves the shared signal standing.