← All AI/ML articles

Bagging, visualised.

A deep decision tree chases every noisy point and wobbles when the data changes. Bagging resamples the data, grows many trees, and averages them. The wobble cancels and the curve calms down.

1. Resample the data2. Average the trees3. Know what it can't fix
training points (drag-free: click to add, double-click to remove)test pointsbagged averageeach tree
Bagged test MSE0
Single tree test MSE0
Out-of-bag MSE0
Verdict-
Behind the scenes · 1

Every tree gets its own bootstrap sample

A bootstrap sample draws n points with replacement: some points appear twice or more, and others are left out entirely. Each square below is one training point, in left-to-right order. Hollow squares were not drawn for this tree. Their stored predictions come later: those are the out-of-bag points. The inspected tree is also drawn in violet in the lab.


hollow = left outdrawn oncedrawn twicedrawn 3+ times

Behind the scenes · 2

More trees, steadier answers

The test error of the average falls quickly as trees are added, then levels off: past a point, more trees cost time but add little. The out-of-bag error is a free estimate that uses no test data, because every tree is scored on the points it never saw. It is usually in the same ballpark as the test error, but with few points or few trees it can be noticeably higher or lower. The dashed line is one tree trained on all the data. Click the chart to set the number of trees.


test MSEout-of-bag MSEtrain MSEsingle tree test MSE

Behind the scenes · 3

Bagging fixes variance, not bias

Averaging removes wobble, but it cannot make a too-simple model smarter. This table compares one tree with a bagged crowd at each depth, using your current number of trees and sample size. Deep trees (high variance) gain a lot; stumps (high bias) gain almost nothing. Click a row to use that depth.



With several input features, random forests add one more trick: each split looks at only a random subset of features, so the trees are even less alike and the average is even steadier.

Experiment

Break it on purpose

Overfit one tree

A single depth-8 tree on noisy data. It builds a staircase through every noisy point, so test error is high.

Average the crowd

Same data, same depth, but 60 bootstrap trees. The jagged steps blend into a smooth curve and test error falls.

Bag the stumps

Depth-1 stumps, 60 of them. The average is still crude, because bagging cannot add flexibility the trees don't have.

Check yourself

Three quick questions

Key takeaways

  • Bagging = bootstrap aggregating: train many models on resampled data, then average (regression) or vote (classification).
  • A bootstrap sample draws with replacement, so each tree misses about 37% of the points.
  • Out-of-bag error scores each tree on the points it never saw, giving a free validation estimate.
  • It reduces variance: great for deep, wobbly trees; of little use for simple, high-bias models.
  • More trees never overfit more, but returns diminish. Random forests add random feature choice on top.
← Back to all articles