- Bagging trains many models on bootstrap samples and averages them.
- Random forests add random feature selection to decorrelate the trees.
- Boosting trains models sequentially, each one focusing on the previous errors.
This lesson follows Nearest Neighbours & Decision Trees and completes the published supervised-learning block. The probability background is in Probability & Statistics.
1. Why combine models
A single decision tree is unstable: a small change in the training data can change its splits and its predictions. That is high variance. If the errors of several trees were independent, averaging them would reduce the variance without changing the bias much.
The trees are not independent — they see the same data — but we can make them more diverse by giving each tree a different sample, a different subset of features, or both. That is the idea behind bagging and random forests.
Boosting takes the opposite approach. It builds trees sequentially, and each new tree focuses on the examples the current ensemble gets wrong. It reduces bias more than variance, and it can overfit noisy labels if pushed too far.
2. Bagging
Bootstrap aggregation, or bagging, draws B bootstrap samples. Each sample has the same size as the training set but is drawn with replacement, so some examples appear several times and others not at all. A model is trained on each sample, and the predictions are averaged:
For classification, each tree votes for a class and the forest takes the majority. For a probability, average the class probabilities or the one-hot votes:
Bagging is most useful for low-bias, high-variance learners such as deep trees. It does little for a model that is already stable. The cost is B times the training and prediction work, though trees can be trained in parallel.
3. Random forests
A random forest is bagging plus feature randomness. At each split, the tree considers only a random subset of features. This stops one strong feature from dominating every tree and makes the trees less correlated.
In two dimensions, a common choice is one feature per split. In higher dimensions, the subset is often the square root of the number of features for classification. The forest is still an average of trees, but the trees see different views of the problem.
Random forests are strong tabular baselines: little preprocessing, handles mixed feature types, and gives an out-of-bag estimate if you track the examples each tree did not see. They are not automatically calibrated, and their feature-importance scores are not causal effects.
4. Boosting
AdaBoost starts with uniform example weights and fits a weak learner, often a one-split stump. It gives the weak learner a vote based on its weighted error:
Then it increases the weight of the examples the stump got wrong and decreases the weight of the ones it got right:
The final prediction is a weighted vote:
A stump with error 0.25 gets α = ½ ln 3 ≈ 0.549. A stump with error near 0.5 gets almost no vote; a stump with error near 0 gets a large vote. Gradient boosting generalizes the same sequential idea to differentiable losses.
5. Worked example
Suppose three trees vote on one example: class 1, class 0, class 1. The forest probability is 2/3 and the prediction is class 1.
Worked example: a vote and a boost
| tree | vote | running class-1 fraction |
|---|---|---|
| 1 | 1 | 1.000 |
| 2 | 0 | 0.500 |
| 3 | 1 | 0.667 |
The forest predicts class 1 with probability 0.667. More trees would reduce the variance of this estimate, but they would not make the training labels correct.
For boosting, take a stump with weighted error ε = 0.25. Its vote is:
Correctly classified examples are multiplied by exp(−α) ≈ 0.577; misclassified examples by exp(α) ≈ 1.732. After renormalizing, the next stump sees a harder problem. That is the sequential correction.
6. Where it fails
Ensembles are not automatically better. If the base model is stable or the data are tiny, averaging can add cost without adding accuracy. A random forest can still overfit a noisy dataset, and a boosted model can chase mislabeled examples until it memorizes them.
Interpretability drops. Feature importance tells you which features the ensemble used, not what would happen if you intervened on one. Correlated features split the importance between them.
Deployment costs grow with the number of trees. A forest of hundreds of deep trees can be expensive in latency and memory. The best ensemble is the one that fits the accuracy, latency, and maintenance budget.
This completes the supervised-learning block. The next phase moves to unsupervised structure: clustering, mixtures, and dimensionality reduction.
7. Going deeper
Out-of-bag error uses the examples that were not drawn in a bootstrap sample as a kind of validation set. It is convenient, but it is not a substitute for a clean held-out test when the data have time or group structure.
Gradient boosting fits each new tree to the negative gradient of the loss. XGBoost, LightGBM, and CatBoost add engineering choices: histogram splits, regularization, handling of missing values, and efficient distributed training. The statistical idea is still stagewise additive modeling.
Stacking learns a meta-model over base-model predictions. Voting averages them. These are different ways to combine information, and they have different leakage risks: the meta-model must be trained on out-of-fold predictions, not on predictions the base models saw during training.
8. Practice
Why does bagging help a deep tree more than a shallow tree?
A stump has weighted error 0.1. What is its AdaBoost vote?
Why is a random forest’s feature importance not a causal statement?
9. Resources
The MIT chapter covers bagging and random forests with the bias-variance view. The scikit-learn ensemble chapter covers forests, boosting, and practical parameters. Both links were opened and checked for this lesson.
Continue the track
The mathematics behind this topic: Probability & statistics.