Ensemble Methods
Single decision trees are transparent but overfit easily. This lecture shows how combining many trees into an ensemble dramatically improves performance. We cover decision tree basics (splits, impurity, pruning), then bagging (bootstrap aggregating) and random forests (bagging + random feature selection). Key topics include the variance of an average of correlated trees — ρσ² + (1−ρ)σ²/B, the formula that explains why more trees stop helping and why max_features exists — along with feature importance, out-of-bag error, and the key hyperparameters to tune.
Re-run the worked example
Download the complete Python example and its pinned requirements into the same folder. The breast-cancer data are included with scikit-learn; no data download is needed. The published numbers were verified with Python 3.14.10 and scikit-learn 1.9.0.
In a fresh Python environment, run:
python -m pip install -r requirements-lecture-10.txt
python lecture_10_ensembles.pyThe script splits the data 80/20 with stratification and compares a single
decision tree, bagging and a random forest under one 5-fold CV protocol, with
f1 for the benign class as the metric. The tree reaches CV f1 0.932 ± 0.014,
bagging 0.966 ± 0.017 and the forest 0.972 ± 0.014; on the test set the tree
scores 0.929 and both ensembles 0.958. It also prints the forest’s
out-of-bag accuracy (0.956) and compares impurity importance with permutation
importance. The full run can take a few minutes.
The optional variance supplement measures the variance of an average of trees on simulated data, for bagging and for a random forest, and checks it against the formula from the lecture, ρσ² + (1 − ρ)σ²/B. It uses the same requirements:
python lecture_10_ensembles_supplement.py