Sub topic 2
Boosting: building an ensemble one tree at a time
This chapter assumes you know what a shallow decision tree is, how a train/validation split works, and what overfitting means. It covers the idea behind boosting. The exact gradient formulation comes in the next chapters.
Bagging versus boosting
An ensemble combines many models into one prediction. There are two common ways to build one from trees.
- Bagging trains many trees independently on random resamples of the data, then averages them. A random forest is the standard example. Averaging mainly reduces variance.
- Boosting trains trees one after another. Each new tree is built knowing what the earlier trees got wrong. Boosting mainly reduces bias.
Because every tree depends on the ones before it, boosting cannot train its trees in parallel the way bagging can. The trees are usually weak learners. A weak learner is a model that is only slightly better than guessing, such as a tree with depth 2 or 3. A single shallow tree underfits, but a long sequence of them can fit complex patterns.
The core loop
For squared-error regression, the loop looks like this:
- Start with a constant prediction, such as the mean of the labels.
- Compute the residuals: the label minus the current prediction.
- Fit a small tree to predict those residuals.
- Add the tree's output to the prediction, scaled by a learning rate between 0 and 1.
- Repeat from step 2.
The learning rate, also called shrinkage, makes each tree contribute only a small correction. Small steps need more trees, but they usually generalize better.
A hand-built example
This script runs the loop with scikit-learn trees. It prints the training mean squared error after each round.
import numpy as np
from sklearn.datasets import make_regression
from sklearn.tree import DecisionTreeRegressor
X, y = make_regression(n_samples=500, n_features=5, noise=10, random_state=0)
pred = np.full(len(y), y.mean())
for step in range(1, 6):
residual = y - pred
tree = DecisionTreeRegressor(max_depth=2).fit(X, residual)
pred = pred + 0.5 * tree.predict(X)
print(step, round(float(np.mean((y - pred) ** 2)), 1))The error shrinks each round, because every tree targets what is left over. This is training error, though. Nothing stops the loop from fitting noise if you keep going.
Common errors
- Too many rounds. Unlike a random forest, adding trees to a boosted model can make it worse on new data. Choose the number of rounds with a validation set.
- Trees that are too deep. Deep trees fit noise in each round. Boosting works best with shallow trees.
- Learning rate of 1.0. Each tree then makes a full correction, which overfits quickly.
- Expecting parallel tree training. XGBoost is fast, but it parallelizes work inside each tree, such as split search. It does not parallelize across trees.
Exercise
You double the number of boosting rounds and the training error drops, but the validation error rises. What is happening, and what are two ways to respond?
Show answer
The model is overfitting: the extra trees are fitting noise in the training data. Respond by stopping at the round where validation error is lowest (early stopping), or by lowering the learning rate, using shallower trees, or both.