From one tree to a forest
A single tree overfits and wobbles. The fix is almost embarrassingly simple, and it is one of the most powerful ideas in machine learning.
In Lesson 1 you saw one deep tree hit 96% on training data but only 66% on test, and shift wildly when the data changed. Now you will turn that weakness into a strength.
- Watch averaging crush a tree's variance, live
- See why bootstrap samples make trees differ
- Meet the one trick that makes a forest beat plain bagging
Prerequisites: Lesson 1 (decision trees, and why one deep tree overfits).
The wisdom of crowds
Ask one noisy expert and you get a noisy answer. Ask hundreds and average them, and the random errors cancel while the real signal survives. That is the whole trick behind a forest.
The catch is hiding in one word: independent. Hold that thought.
Build a forest, tree by tree
Each tree below is grown deep (the jagged, overfitting kind from Lesson 1) on its own random resample. Drag the slider to average more of them, and watch the boundary smooth out while accuracy climbs.
No single tree got better. You did not prune or tune them. The average simply cancels their individual mistakes.
Where did the accuracy come from?
As you dragged the slider and averaged more trees, the boundary smoothed and accuracy climbed. What actually got better?
Clones do not help
Averaging only cancels errors if the trees make different mistakes. Grow a hundred identical trees and their average is just that same tree again. No variance reduction at all.
So the real engineering problem of a random forest is: how do we force the trees to be different on purpose? There are two tricks, and a forest uses both.
Give each tree different data
Before growing a tree, draw a bootstrap sample: pick rows from the training set at random with replacement, until you have a set the same size. Some rows appear twice, others not at all.
Different data in means a different tree out. But there is a sneakier source of sameness left to kill.
Bootstrap is not enough
Suppose one feature, say tenure, is much more predictive than the rest. Then every tree, even on its own bootstrap sample, will choose tenure for its very first split. The trees end up looking nearly identical near the top.
Statisticians call this correlation between the trees, and it is poison for averaging: remember the variance only divides by B when the trees are independent. Correlated trees barely reduce variance.
With pairwise correlation \(\rho\), the averaged variance is \(\rho\sigma^2 + \frac{1-\rho}{B}\sigma^2\). The second term shrinks as you add trees, but the first does not: \(\rho\sigma^2\) is a floor you cannot average away. The only way down is to lower \(\rho\), which is exactly what the next trick does.
Random features
Here is the fix that makes it a random forest: at each split, the tree may only consider a random handful of features, not all of them. The dominant feature cannot win every time, so the trees diverge. Toggle below and watch what each tree picks for its first split.
Quick question
Why does a random forest let each split consider only a random subset of the features?
The whole forest, in three rules
That is it. A random forest is just these three ideas stacked:
- Bootstrap: grow each tree on its own random resample of the rows.
- Random features: at each split, only consider a random subset (mtry) of the features.
- Average: let all the trees vote; the majority (or mean) is the forest's answer.
Grow the forest in R
First, load the randomForest package and build the churn data to grow the forest on. Each lesson runs in a fresh R session, so we set it all up right here (run this once):
Show answer
rf <- randomForest(churned ~ ., data = train,
ntree = 200, mtry = 3)References
- Breiman (2001), Random Forests, Machine Learning 45(1) - bootstrap + random features, the full method.
- Breiman (1996), Bagging Predictors, Machine Learning 24(2) - where bootstrap aggregation began.
- The Elements of Statistical Learning, ch. 15 (free PDF) - the variance-of-an-average and decorrelation math.
Lesson 2 complete
You now understand why a random forest works, not just what it does. Next, you will build and tune a real one.
Up next, Lesson 3: Training, tuning and reading a forest in R. OOB error, tuning mtry and trees on a live model, reading variable importance, and the code to do it for real, ending in your Machine Learning certificate.