Sub topic 4
The gradient boosting algorithm
This chapter assumes you know how a shallow decision tree makes predictions, what a loss function is, and what a gradient is at an intuitive level. It also assumes you are comfortable with NumPy.
The core idea
Gradient boosting builds a model as a sum of trees. It starts with a constant prediction. Each round then adds one small tree that corrects the mistakes of the ensemble so far.
The key step is deciding what the new tree should learn. It is trained on the negative gradient of the loss with respect to the current predictions. For squared error, that negative gradient is proportional to the residual, y - prediction. Fitting a tree to residuals is therefore gradient descent, with the step taken in function space instead of parameter space.
The algorithm
- Start with a constant prediction, such as the mean of
yfor squared error. - For each round, compute the negative gradient of the loss at every training row.
- Fit a shallow tree to those values.
- Add the tree's output to the running prediction, scaled by a learning rate.
- Repeat for the chosen number of rounds.
A minimal implementation
This version handles squared error only. Pass the same lr to both functions.
import numpy as np
from sklearn.tree import DecisionTreeRegressor
def fit_gbm(X, y, n_rounds=100, lr=0.1, max_depth=3):
base = float(np.mean(y))
pred = np.full(len(y), base)
trees = []
for _ in range(n_rounds):
residual = y - pred # negative gradient of squared error (up to a constant factor)
tree = DecisionTreeRegressor(max_depth=max_depth).fit(X, residual)
pred = pred + lr * tree.predict(X)
trees.append(tree)
return base, trees
def predict_gbm(model, X, lr=0.1):
base, trees = model
pred = np.full(X.shape[0], base)
for tree in trees:
pred = pred + lr * tree.predict(X)
return predEach tree sees the residuals left by all earlier trees, so the trees cannot be trained in parallel across rounds. This is the main difference from bagging, where trees are independent.
Why shrinkage matters
The learning rate lr scales each tree's contribution. A small value, such as 0.05, means each tree corrects only a fraction of the remaining error. You need more rounds, but the final model usually generalizes better than one built with large steps.
Common errors
- Mismatched learning rates. Predicting with a different
lrthan you trained with gives wrong outputs, because the base value and the trees were fit assuming that scale. - Deep trees. Boosting works best with weak learners. Trees of depth 3 to 6 are typical. Deep trees fit the residuals too closely and overfit within a few rounds.
- Too many rounds. Training error keeps falling as you add trees, so you cannot choose the number of rounds from training data. A later chapter covers early stopping for this.
- Treating residuals as universal. Residuals are the negative gradient only for squared error. Other losses, such as log loss, have different gradients, and the tree must be fit to those.
Exercise
In the implementation above, why does each tree get fit to y - pred instead of to y?
Show answer
Because each new tree is meant to correct what the current ensemble gets wrong. For squared error, y - pred is the negative gradient of the loss, so adding a tree fit to it, scaled by the learning rate, moves the predictions downhill on the loss. Fitting to y would just rebuild the same model every round.
Exercise
You halve lr from 0.1 to 0.05 and keep n_rounds=100. What do you expect to happen to training error, and what should you change?
Show answer
Training error will usually be higher after 100 rounds, because each tree makes a smaller correction. Increase n_rounds, roughly doubling it, to reach a similar fit. The smaller steps often generalize better.