Gradient boosting
Gradient boosting computed live, stage by stage: each small regression tree fits the residuals, scaled by the learning rate. Matches scikit-learn at every stage, in 1D and as a 3D surface.
Updated
- Boosting as gradient descent in function space
- Residuals as negative gradients
- Learning rate and number of stages
- Tree depth and interactions
- Overfitting with too many stages
Intuition
Correct a little at a time
Start by guessing every point with one constant (the mean): the error is large. Look at where the guess is too high and where too low — those are the residuals. Sketch the shape of the residuals roughly with a small tree of a few leaves, and add a small fraction of it to the prediction. Compute the new residuals, grow another tree… Every tree is weak, but hundreds of them added together can approximate a complicated curve.
The three small labs below really boost stage by stage. Grey points are the training data (the true function plus noise 0.3), the dashed line is the true function, the thick orange line is the current prediction and the faint orange line the previous stage's . Defaults: learning rate 0.15, tree depth 2 (at most 4 leaves).
Stage 1: one tree can only cut the flat line into a few pieces, and only 15% of it is added, so the curve barely moves; training MSE falls from 2.29 to 1.75. Stage 30: the prediction already hugs the true wave, with test MSE 0.117. Stage 400: training MSE is down to 0.0011 and the curve bends to chase every noisy point, while test MSE has risen to 0.168 — it has overfitted.
Math
The algorithm
Training data , squared loss :
- (the constant that minimises squared loss);
- for :
- residuals ;
- fit a CART regression tree (splitting on mean squared error, maximum depth ) to , giving , whose leaf values are the means of the residuals falling into each leaf;
- .
Why "gradient" boosting
Treat the predictions at the training points as parameters. The gradient of the loss with respect to each is
The residual is the negative gradient. Ordinary gradient descent steps along ; gradient boosting does the same, except the "direction" is approximated by a tree so that it also gives values at unseen . With a different loss (log loss, Huber), fit that loss's negative gradient instead.
Blue points are the residuals before this stage, and the black line is the tree fitted to them. At first the residuals are the whole wave; by stage 60 almost nothing but noise is left, and the tree starts fitting the noise — that is where overfitting comes from.
Learning rate and number of stages
Look at training and test MSE after every stage. The noise has standard deviation 0.3, so test MSE cannot go much below 0.09. Within 400 stages:
| Learning rate ν | Best test MSE | At stage |
|---|---|---|
| 1 | 0.147 | 17 |
| 0.3 | 0.113 | 20 |
| 0.15 (default) | 0.116 | 35 |
| 0.05 | 0.115 | 190 |
| 0.01 | 0.123 | 400 (still falling) |
The smaller the learning rate, the more stages it takes to reach the best point, and the more slowly the error rises afterwards. means no shrinkage: each tree swallows the residuals whole, memorises the noise quickly, and its best is only 0.147. In practice is set fairly small (0.01–0.1) and a validation set decides when to stop (early stopping).
Tree depth
Depth sets how complicated a shape each tree can express. Default data, learning rate 0.15:
- depth 1 (stumps, one cut per tree): best test MSE 0.112, but only at stage 254;
- depth 2: 0.116;
- depth 6: by stage 120 training MSE is close to 0 and test MSE 0.182, and its best is only 0.156.
A tree of depth has at most leaves. Each tree should be a "weak learner", leaving the complexity to the sum of stages.
How it is computed, with real numbers
Once the lab above has loaded, this panel works through the same formulas with its current numbers.
Experiment
Follow the lesson
Course
Lesson 1: residuals, learning rate and overfitting
5 steps · each one: set up and run → look → answer
1. The first tree
Default wave data, one stage. Look at the residuals and the update in "How it is computed" below.
2. The residuals shrink
Go to stage 30 and open "Residuals and tree".
3. Too far
Run all 120 stages and look at the MSE curves on the right.
4. No shrinkage
Set the learning rate to 1.
5. Trees that are too deep
Learning rate back at 0.15, tree depth 6.
Try these settings
On the step data at stage 120, stumps give test MSE 0.208 and depth 2 gives 0.232: the target is itself a few constant pieces, so cutting once per tree fits it well. With learning rate 0.01, test MSE after 120 stages is still 0.533 — not overfitting, just not finished learning.
Exercises
Exercises
Exercise 1: early stopping
After the default 120 stages test MSE is 0.126. Without changing the learning rate or tree depth, finish with test MSE at most 0.12.
Hint
Cut the number of stages to 60 (0.118) or 35 (0.116).
- Test MSE ≤ 0.120
Exercise 2: learn slowly
After 400 stages at the default learning rate, test MSE is 0.168. Change only the learning rate so that test MSE after 400 stages is at most 0.13.
Hint
Learning rate 0.05: 0.126; 0.01: 0.123.
- Stage ≥ 400
- Test MSE ≤ 0.130
Quiz
Further reading
Frequently asked questions
Why is it called “gradient” boosting?
Treat the predictions at the training points as parameters: the negative gradient of the loss with respect to them is what each tree fits. With squared loss the negative gradient is exactly the residual, so "fitting residuals" is gradient descent in function space.
How should I choose the learning rate (shrinkage)?
A smaller learning rate needs more stages but usually generalises better. A common choice is 0.01–0.1, with early stopping on a validation set to pick the number of stages. On this page learning rate 1 reaches a best test MSE of 0.147, while 0.3 to 0.05 all land at 0.113–0.116.
Can gradient boosting overfit?
Yes. Too many stages, trees that are too deep or a learning rate that is too large all let the model fit noise. With this page's defaults test MSE is lowest at stage 35 and has risen to 0.168 by stage 400. Early stopping, subsampling (stochastic gradient boosting), limits on the number of leaves and regularisation keep it in check.
How are XGBoost and LightGBM related to gradient boosting?
They are all gradient-boosted decision trees. XGBoost uses second derivatives and regularised leaf weights, LightGBM speeds things up with histograms and leaf-wise growth, and CatBoost specialises in categorical features. The core loop is the same as on this page: fit the negative gradient, multiply by the learning rate, add.
Can gradient boosting do classification?
Yes. Switch to log loss: F is the log-odds, each tree fits y − σ(F), and leaf values are computed with a Newton step. For multi-class problems each stage grows one tree per class.
Further reading
- Friedman, Greedy Function Approximation: A Gradient Boosting Machine, Annals of Statistics, 2001: the original gradient-boosting paper.
- Friedman, Stochastic Gradient Boosting, 2002: adding subsampling.
- Chen, Guestrin, XGBoost: A Scalable Tree Boosting System, KDD 2016.
- Hastie, Tibshirani, Friedman, The Elements of Statistical Learning, chapter 10: boosting and additive trees.
- scikit-learn, Gradient Boosting: this page's reference implementation.