Ensemble methods combine multiple weak learners into a stronger one. The two dominant families — bagging and boosting — attack the bias-variance tradeoff from opposite directions.
Bagging: variance reduction
Bagging (bootstrap aggregating) trains models independently on bootstrap resamples of the training data, then averages their predictions:
If each has variance and pairwise correlation , the variance of the average is:
As , the second term vanishes, leaving as a floor. This is why bagging works best with high-variance, low-bias base learners (deep decision trees) and why decorrelating trees — as random forests do by subsampling features at each split — helps drive down further.
Boosting: bias reduction
Boosting builds learners sequentially, each one correcting the errors of the ensemble so far. Gradient boosting frames this as functional gradient descent: at each stage , fit a new learner to the negative gradient of the loss with respect to the current prediction,
then update , where is the learning rate. Because each stage explicitly targets residual error, boosting drives bias down — at the cost of being more prone to overfitting than bagging, which is why shrinkage () and early stopping matter so much in practice.
A minimal gradient boosting loop
import numpy as np
from sklearn.tree import DecisionTreeRegressor
def gradient_boost(X, y, n_estimators=100, lr=0.1, max_depth=3):
F = np.full(y.shape, y.mean())
trees = []
for _ in range(n_estimators):
residual = y - F # negative gradient for squared error loss
tree = DecisionTreeRegressor(max_depth=max_depth)
tree.fit(X, residual)
F += lr * tree.predict(X)
trees.append(tree)
return trees, y.mean()
Takeaway
| Bagging | Boosting | |
|---|---|---|
| Trains | in parallel | sequentially |
| Targets | variance | bias |
| Base learner | high-variance (deep trees) | high-bias (shallow trees / stumps) |
| Overfitting risk | low | higher, needs regularization |
Both are instances of the same idea — combine weak learners — but the direction of the bias-variance tradeoff each one attacks explains most of their practical differences.