Ensembles and Random Forests
About 24 min
Goal
By the end of this lesson, you can explain why combining diverse trees can be more stable than relying on one tree, and describe bootstrap sampling, random feature subsets, and voting in a Random Forest.
Ask several imperfect models instead of one
If several people make different estimation errors, their average can be more stable than one person's guess. If they all make exactly the same error, averaging does not help much.
An ensemble applies the same idea to models: combine several predictors so one fitted model's mistake does not automatically decide the final answer.
Bagging creates different training views
A Random Forest trains many decision trees. For each tree, it draws a bootstrap sample: training rows sampled from the original training set with replacement.
Some original rows appear more than once and some do not appear in that tree's sample.
This is a form of bagging, short for bootstrap aggregating.
Random feature subsets add more diversity
At each split, a Random Forest tree considers only a random subset of features rather than always considering every feature.
That makes it harder for one strong feature to force all trees into nearly identical structures.
The goal is useful diversity among reasonably capable trees.
Combine predictions
For classification, trees vote or their class probabilities are averaged. For regression, numeric predictions are averaged.
For example, suppose five classification trees vote:
tree 1 -> A
tree 2 -> A
tree 3 -> B
tree 4 -> A
tree 5 -> B
Class A wins 3 votes to 2. A noisy mistake from one tree does not automatically decide the forest's answer.
This does not make forests immune to leakage, biased data, or distribution shift. An ensemble can learn the wrong problem very consistently.
Compare one tree with a forest
- Click Run. The validation rows stay fixed while the Lab creates five slightly different training samples.
- Read
tree scores across resampled training sets:andforest scores across resampled training sets:. - Compare
tree score range:withforest score range:. In the starter, the single-tree range is about0.119, while the 25-tree forest range is about0.024. - This does not prove every forest is always better. On this controlled fixture, changing the training sample makes the single tree's validation score vary more than the forest's.
- Find
n_estimators = 25. Change only25to5. - Before running, predict that averaging fewer trees will make the forest's validation score more sensitive to which trees were sampled.
- Click Run and inspect
forest score range:. On this fixture it grows to about0.048. - Press Reset afterward.
Python used here: list accumulation and .items() for key/value pairs (P1 — dictionaries).
Loading lab…
The lesson is not “Random Forest always beats one tree.” In the one-shot comparison, a score difference may be only one validation example. The stronger evidence here is the repeated-sample result: on this fixture, averaging diverse trees makes validation performance more stable across resampled training sets. The Lab measures score stability, not prediction-by-prediction disagreement.
Random Forest versus one deep tree
A single deep tree can produce an irregular boundary and may fit training noise.
A Random Forest can still use expressive trees, but the final prediction aggregates many differently trained trees. That often reduces variance while keeping nonlinear modeling power.
The tradeoff is that the forest is harder to explain as one simple sequence of rules.
Quick Check
Key Takeaways
- Ensembles combine multiple predictors rather than trusting one fitted model.
- Bagging trains models on bootstrap samples of the training rows.
- Random Forest also uses random feature subsets to increase tree diversity.
- Classification forests aggregate votes or probabilities; regression forests average values.
- Diversity matters because identical trees would repeat the same errors.
Next Lesson
Next, you will contrast Random Forest with boosting, where later trees are trained to reduce errors left by earlier trees.
References
- scikit-learn, Ensemble methods.
- scikit-learn, RandomForestClassifier.
Completion is stored locally on this device.