TL;DR
Gradient boosting builds a prediction in rounds. Each round fits a small decision tree to the errors that the current prediction still makes, then adds a scaled-down copy of that tree to the prediction. Each round moves the model a little closer to the data, much as Gradient Descent moves a weight closer to a good value.
Because the trees are added one at a time, each new tree can focus on the cases the earlier trees got wrong. This is why gradient boosting is one of the strongest methods for tables of numbers and categories.
Builds on: Decision Tree, Gradient, Gradient Descent, Ensemble Learning · Leads to: XGBoost, Weighted Quantile Sketch
Where it's used
- Predicting trip times for a bike-share company. The company has a table of past rides with the distance, the hour and the bike type, and wants to predict how long a new ride will take. Boosted trees handle a mix of numbers and categories without rescaling the columns, and the worked example at the end of this lesson follows this case.
- Ranking search results. LambdaMART, a method that trains boosted trees to order documents, is a widely used baseline for learning to rank.
- Tabular machine learning competitions. In the 2010s, boosted trees won many competitions that used spreadsheet-style data. XGBoost was built to make this kind of training fast.
How it works
Take four points. The input x is 1, 2, 3 or 4, and the target y is 2, 3, 5 or 4. We want a model that maps x to a prediction. We use a learning rate of 0.5, which means each tree's contribution is halved before it is added (the learning rate is also called shrinkage). Each tree is a decision stump: a tree with a single question, such as "is x at most 2.5?"
- Start with a constant. The first prediction is the average of the targets: (2 + 3 + 5 + 4) / 4 = 3.5. Every point gets 3.5. Check: the targets sum to 14, and 14 / 4 = 3.5.
- Find the errors. Subtract the prediction from each target. The errors, also called residuals, are 2 - 3.5 = -1.5, 3 - 3.5 = -0.5, 5 - 3.5 = 1.5 and 4 - 3.5 = 0.5. Check: they sum to zero, as they always do when the prediction is the average.
- Fit a tree to the errors, not to y. Try three splits: x at most 1.5, at most 2.5, and at most 3.5. For each split, predict the average error on each side and add up the squared misses. The split at 1.5 scores 2.0, the split at 2.5 scores 1.0, and the split at 3.5 scores 5.0. The best is 2.5. Its leaves are -1.0 on the left (the average of -1.5 and -0.5, for x = 1 and 2) and +1.0 on the right (the average of 1.5 and 0.5, for x = 3 and 4). Check: each leaf value is the average of the errors it covers, so no other constant on that side would score lower.
- Add a shrunken copy. The new prediction is the old prediction plus 0.5 times the tree's leaf value. For x = 1 and 2 that is 3.5 - 0.5 = 3.0. For x = 3 and 4 it is 3.5 + 0.5 = 4.0. Check: each prediction moved halfway toward what the tree said, not all the way.
- Repeat on the new errors. The predictions are now 3, 3, 4 and 4, so the errors are -1, 0, 1 and 0. The sum of squared errors fell from 5.0 to 2.0. Round two fits a new stump to these errors. The best split is at 1.5, with leaves -1 (x = 1) and +1/3 (x = 2, 3 and 4). Check: the average of 0, 1 and 0 is 1/3, and this split scores 0.67, beating 1.0 for the split at 2.5 and 2.0 for the split at 3.5.
- Update again. The new predictions are 3 - 0.5 = 2.5 at x = 1, 3 + 1/6 = 3.17 at x = 2, and 4 + 1/6 = 4.17 at x = 3 and 4. The errors are now -0.5, -0.17, 0.83 and -0.17, with a sum of squared errors of 1.0. Check: 0.25 + 0.03 + 0.69 + 0.03 = 1.0.
| Round | Predictions at x = 1, 2, 3, 4 | Sum of squared errors |
|---|---|---|
| 0 (constant) | 3.5, 3.5, 3.5, 3.5 | 5.0 |
| 1 | 3.0, 3.0, 4.0, 4.0 | 2.0 |
| 2 | 2.5, 3.17, 4.17, 4.17 | 1.0 |
Each round uses only the errors left by the rounds before it. The loop is always the same:
flowchart LR A["Current prediction"] --> B["Errors: target minus prediction"] B --> C["Fit a small tree to the errors"] C --> D["Add the learning rate times the tree"] D --> A
The math
We have n training pairs . Write for the model after m rounds, for the tree added in round m, and (the Greek letter nu) for the learning rate, a number between 0 and 1. For one example the loss is the squared error
The 1/2 cancels a factor of 2 that appears when we differentiate.
Step 1 in symbols
The starting model is the constant that makes the total loss smallest. For squared error that constant is the average of the targets:
Step 2 in symbols
The errors of step 2 are the pseudo-residuals, the negative gradient of the loss with respect to the prediction:
For squared error, , so the negative gradient is . That is the ordinary residual. In round 1 the residuals are .
Other losses give other pseudo-residuals. For a 0/1 label y with predicted probability , where is the Sigmoid Function, the negative gradient of the Cross-Entropy Loss is : the gap between the label and the predicted probability.
Step 3 in symbols
Tree is fitted by least squares to the pairs . In each leaf, the tree predicts the average of the pseudo-residuals that fall in that leaf. The split is chosen to make
as small as possible, where is the average pseudo-residual in the leaf that holds point i. This is a Residual Sum of Squares computed on the pseudo-residuals. For the split at 2.5 the left leaf is and the right leaf is . Each side contributes to the sum, so the score is .
Step 4 in symbols
With , the prediction at after round 1 is .
The update is a Gradient Descent step, but in function space. Ordinary gradient descent changes each weight by a small multiple of its partial derivative. Here the "parameters" are the values of the function at the training points, and the tree is a learned approximation of the negative gradient with respect to them.
Step 5 in symbols
Round 2 starts from and adds with :
The error at that point is . The number of rounds M and the learning rate are chosen together, usually with validation data. Small values of , such as 0.1, need more rounds but usually generalise better.
Worked examples
Data: (x, y) = (1, 1) and (2, 3), with learning rate 0.5.
- Start: the average target is (1 + 3) / 2 = 2. Both predictions are 2.
- Errors: 1 - 2 = -1 at x = 1, and 3 - 2 = +1 at x = 2.
- Stump: split between x = 1 and x = 2. Each leaf holds one point, so the leaf values are -1 on the left and +1 on the right.
- Update: at x = 1, 2 + 0.5 times (-1) = 1.5. At x = 2, 2 + 0.5 times (+1) = 2.5.
- New errors: 1 - 1.5 = -0.5 and 3 - 2.5 = +0.5. The sum of squared errors fell from 2 to 0.5.
Interpretation: one round moved each prediction halfway to its target. With a learning rate of 1, the same round would have fit both points exactly.
Data: the four points from the walkthrough, with learning rate 1 and one round.
- Start: the average target is 3.5, so every prediction is 3.5. The errors are -1.5, -0.5, 1.5 and 0.5.
- Stump: the best split is still at 2.5, with leaves -1 and +1.
- Update with learning rate 1: at x = 1 and 2, 3.5 - 1 = 2.5. At x = 3 and 4, 3.5 + 1 = 4.5.
- New errors: 2 - 2.5 = -0.5, 3 - 2.5 = 0.5, 5 - 4.5 = 0.5, and 4 - 4.5 = -0.5.
- Sum of squared errors: 0.25 + 0.25 + 0.25 + 0.25 = 1.0.
Interpretation: with learning rate 0.5 the sum after one round was 2.0, so the full step fits faster. Faster early progress is also the risk: a full step follows the training data's noise as readily as its pattern. This is why the shrinkage is kept small in practice.
A bike-share company records four trips. The distance x is in kilometres and the duration y is in minutes: (1, 8), (2, 10), (3, 12) and (4, 14). Learning rate 0.5.
- Start: the average duration is (8 + 10 + 12 + 14) / 4 = 11 minutes. Every trip is predicted at 11.
- Errors: 8 - 11 = -3, 10 - 11 = -1, 12 - 11 = +1 and 14 - 11 = +3. The sum of squared errors is 9 + 1 + 1 + 9 = 20.
- Best stump: split at 2.5 km. The left leaf is the average of -3 and -1, which is -2. The right leaf is the average of 1 and 3, which is +2. The score is 1 + 1 + 1 + 1 = 4. The other splits score worse: the split at 1.5 km scores 8, and the split at 3.5 km also scores 8.
- Update: trips of 2 km or less are predicted at 11 + 0.5 times (-2) = 10 minutes. Trips over 2 km are predicted at 11 + 0.5 times 2 = 12 minutes.
- New errors: 8 - 10 = -2, 10 - 10 = 0, 12 - 12 = 0, 14 - 12 = +2. The sum of squared errors fell from 20 to 8.
Interpretation: one stump and a halved step already separate short rides from long ones. The two remaining misses are the 1 km and 4 km trips. Later rounds would split those further, and validation data would say when to stop.
Try it: suppose the learning rate for the bike-share model were 1 instead of 0.5. What are the predictions after one round, and what is the sum of squared errors?
Answer: the leaves are -2 and +2, so the predictions are 11 - 2 = 9 for trips of 2 km or less, and 11 + 2 = 13 for longer trips. The errors are 8 - 9 = -1, 10 - 9 = +1, 12 - 13 = -1 and 14 - 13 = +1. The sum of squared errors is 4, down from 8 with the smaller step, which repeats the trade-off from example 2.
Common mistakes and tradeoffs
It is tempting to think gradient boosting is just a random forest with more trees. In fact the trees are built one after another, and each one is fitted to what the ensemble still gets wrong. A random forest builds its trees independently on resampled data and averages them (Bagging), which mainly lowers variance. Boosting mainly lowers bias by stacking corrections, and that is why it can overfit when run too long.
It is tempting to think each tree should be as deep as possible. In fact boosting usually uses shallow trees, often a few levels deep, because each tree is only meant to correct a small part of the error. Deep trees fit the noise in the errors and make the sum overfit quickly (see Decision Tree and Overfitting).
It is tempting to think more rounds always help. In fact the training error keeps falling, but the error on held-out data eventually rises. Watch a validation set and stop at its lowest point, which is what Early Stopping does.
It is tempting to think the Learning Rate is a detail. In fact a small value needs many more trees, and a large one fits the training data fast but overfits sooner. The learning rate and the number of rounds are the two settings to tune together.
It is tempting to think pseudo-residuals are always the plain errors. They are for squared error, but for other losses they are the negative gradient, which can look quite different, as the logistic case in the math section shows.
When to use it: tables of numbers and categories, moderate data sizes, and cases where accuracy matters more than a simple model. It usually needs less tuning than a neural network on the same table.
When not to use it: images, audio and free text, where neural networks learn their own features and do better. It is also slow to train on very large data, because each round needs the previous one. Its many trees are hard to read, and outliers hurt the squared loss, so a more robust loss may be needed.
Alternatives: a Random Forest is easier to tune and its trees can be built in parallel. A linear model is easier to explain. A neural network is the choice for unstructured data.
History
The starting question was theoretical: could many weak learners, each only a little better than guessing, be combined into a strong one? In 1990 Robert Schapire showed that the answer was yes, which gave the family the name boosting. The first practical algorithm, AdaBoost, followed in the mid-1990s. It reweighted the training examples so that each new learner focused on the ones the earlier learners had got wrong.
In 2001 Jerome Friedman published "Greedy function approximation: a gradient boosting machine". It showed that this reweighting procedure is a form of Gradient Descent in function space. That view made boosting work with any differentiable loss, including squared error for regression. Its wide adoption came later, with fast implementations such as XGBoost in the mid-2010s, which helped it win many competitions on tabular data.
Recap
- Gradient boosting starts from a constant prediction and adds trees in rounds.
- Each tree is fitted to the pseudo-residuals of the current model, which point in the direction in which the loss falls fastest.
- Each tree's contribution is scaled by the learning rate. Small steps usually generalise better.
- Friedman's view makes the rounds gradient descent in function space.
- Choose the number of rounds on validation data, and keep the trees shallow.
What to read next: XGBoost adds regularisation and speed tricks to this method, and Weighted Quantile Sketch explains how it finds split points at scale. For the parallel alternative, read Random Forest.