Sub topic 2
Boosting: building an ensemble one tree at a time
This chapter assumes you know what a single decision tree does, how a train/validation split works, and what overfitting means. It covers the idea behind boosting. The gradient-based formulation and XGBoost's additions come in later chapters.
Why one tree is not enough
A deep decision tree has low bias and high variance. It fits the training data closely, but small changes in that data produce a very different tree. A shallow tree, such as a stump with one split, has the opposite problem. It is stable but too simple, so it has high bias.
Ensembles combine many trees to get a better model than any single one. There are two main strategies, and they attack different halves of the bias-variance trade-off.
Bagging versus boosting
Bagging (bootstrap aggregating) trains many deep trees independently, each on a random resample of the data, and averages their predictions. Averaging cancels out noise, so bagging mainly reduces variance. Random forests are the best-known example.
Boosting trains many shallow trees sequentially. Each new tree is trained knowing what the ensemble so far gets wrong. Boosting mainly reduces bias, because every round makes the model more expressive.
| Property | Bagging (random forest) | Boosting (XGBoost) |
|---|---|---|
| Training order | Independent, parallel | Sequential, each tree depends on the last |
| Typical tree depth | Deep, low bias | Shallow (often 3 to 8 levels), high bias |
| Main effect | Reduces variance | Reduces bias |
| Combining trees | Average or vote | Weighted sum |
| Risk of adding more trees | Plateaus, rarely hurts | Can overfit |
| Sensitivity to label noise | Low | Higher, because it keeps chasing errors |
The last two rows matter in practice. Adding trees to a random forest rarely hurts. Adding trees to a boosted model eventually fits noise, which is why later chapters cover learning rate, regularization and early stopping.
The sequential idea
A boosted model is an additive model. Its prediction is a starting value plus a sum of small corrections:
prediction(x) = base + η·tree₁(x) + η·tree₂(x) + … + η·tree_M(x)
Here η (eta) is the learning rate, a number between 0 and 1 that shrinks each tree's contribution. The procedure is:
- Start with a constant prediction, such as the mean of the labels.
- Measure where the current ensemble is wrong.
- Train a shallow tree whose job is to predict those errors.
- Add the tree to the ensemble, scaled by η.
- Repeat from step 2 until you reach the round limit or stop improving.
Each tree is a weak learner. A weak learner is a model only slightly better than guessing, and it is often just a stump or a small tree. A weak learner can still fix part of what the ensemble gets wrong, so many of them together can fit complex patterns.
A worked example with residuals
For squared-error regression, "where the model is wrong" has a simple form: the residual, which is the label minus the current prediction. The script below fits noisy sine data using only stumps. Each stump is trained on the residuals, not on the labels.
import numpy as np
from sklearn.tree import DecisionTreeRegressor
rng = np.random.default_rng(0)
X = np.sort(rng.uniform(0, 6, 200)).reshape(-1, 1)
y = np.sin(X).ravel() + rng.normal(0, 0.2, 200)
lr = 0.3
pred = np.full_like(y, y.mean())
print("round 0 MSE:", round(float(np.mean((y - pred) ** 2)), 4))
for k in range(1, 21):
residual = y - pred
stump = DecisionTreeRegressor(max_depth=1).fit(X, residual)
pred = pred + lr * stump.predict(X)
if k in (1, 5, 10, 20):
print(f"round {k} MSE:", round(float(np.mean((y - pred) ** 2)), 4))The training error falls with every round. A single stump can only produce two output levels. Twenty of them, each added at 30% strength, produce a staircase that follows the sine curve. No individual stump gets close to the target, but the sum does.
Three things in this script carry over to XGBoost:
- The target of each tree is not the original label. It is a description of the current error.
- The new tree's output is scaled by
lrbefore it is added. - The loop has no built-in stopping point. You choose the number of rounds.
What the learning rate does
A small η means each tree makes a modest correction. The model needs more rounds to reach the same training fit, but the result usually generalizes better. A large η fits quickly but can overshoot, since each tree's correction is trusted fully, and it can overfit within a few rounds.
In practice η and the number of rounds are linked. Halving η usually means roughly doubling the rounds. This is why later chapters treat the number of rounds as something to find with early stopping, not something to guess.
Where boosting came from
The first practical boosting algorithm, AdaBoost, took a different route to the same sequential structure. After each round it increased the weights of misclassified examples, so the next weak learner focused on them. The final prediction was a weighted vote of all learners.
Gradient boosting reframes this. Instead of reweighting examples, it fits each new learner to the direction that reduces a chosen loss function. AdaBoost turns out to be a special case of that view. The reframing lets one framework handle regression, classification and ranking by swapping the loss.
Edge cases and failure modes
- Overfitting with many rounds. Training error keeps falling as you add trees, but validation error eventually rises. Track validation error, not training error.
- Noisy labels. Boosting concentrates on examples the ensemble gets wrong. Mislabeled points or outliers can draw many trees toward them. Shallow trees, a small η and subsampling all limit this.
- Trees that are too deep. Deep trees are strong learners, not weak ones. Each tree then fits much of the signal and noise at once, and the sequential advantage shrinks. Depth is a bias-variance control in boosting, not a way to improve each tree.
- No parallelism across trees. Tree k needs the predictions of tree k−1, so rounds cannot run side by side. Speed comes from making each tree fast to build, for example by parallelizing the split search inside a tree, which XGBoost does.
- Prediction cost. Prediction evaluates every tree, so a model with 2,000 trees is slower to score than one with 200.
Exercise
You train a random forest and a boosted model on the same data. The random forest's trees are depth 20, and the boosted model's trees are depth 3. Which ensemble relies on its trees having low bias, and why does adding 500 more trees hurt the boosted model more easily than the forest?
Show answer
The random forest relies on low-bias trees. Its deep trees already fit the signal, and averaging independent trees removes variance. More trees only average more, so error plateaus. The boosted model uses high-bias shallow trees and lowers bias round by round. Each extra tree fits the remaining error, which after enough rounds is mostly noise, so validation error can rise. This is why boosting needs a learning rate, regularization and early stopping.
Exercise
In the worked example, you change lr from 0.3 to 0.03 and keep 20 rounds. What do you expect for the training MSE at round 20, and what would you change to compensate?
Show answer
The training MSE at round 20 will be higher, because each stump moves the prediction only 3% of the way toward its fitted residual, so the model is still underfit. To compensate, increase the number of rounds, roughly tenfold for the same fit, and pick the final count by watching validation error.