Boosting
Last updated
A random forest grows its trees side by side, each on its own. Boosting grows them one after another, and each new tree studies what the trees before it got wrong. Started with stumps, trees with a single question, it builds models that win most competitions on tables of data. You'll boost by hand, watch the residuals shrink, and then see how it goes wrong.
By the end of this lesson you can
- Explain how AdaBoost reweights examples, and how gradient boosting fits residuals
- Build gradient boosting by hand from stumps
- Trade the learning rate against the number of trees, and stop early
- Use HistGradientBoostingClassifier and HistGradientBoostingRegressor, and name XGBoost, LightGBM and CatBoost
Each tree fixes the last
A random forest makes its trees different by chance, and averages away their errors. Boosting makes them different on purpose: tree 2 is trained to fix tree 1's mistakes, tree 3 to fix what's still wrong after both, and so on. The model is their sum.
That changes which trees work best. A forest needs deep, flexible trees, whose errors average out. Boosting starts from weak learners, trees so small they're only a little better than guessing, often stumps with one question, and builds up the flexibility one tree at a time. Forests reduce variance; boosting reduces bias.
AdaBoost: weight the mistakes
The first boosting method that really worked, AdaBoost (Freund and Schapire, 1997), gives every training example a weight, all equal at first. Each round:
- Train a stump on the weighted examples, and find its weighted error rate, .
- Give the stump a say in the final vote of : the more accurate, the louder.
- Multiply the weight of every example the stump got wrong by , every one it got right by , and rescale so the weights add up to 1.
The next stump has to care about the hard examples, because they now weigh more. The final model is the weighted vote of all the stumps.
One stump on the moons: 79.5%. Two hundred, boosted: 89.5%. A stump can only cut the plane in two with one straight line; their weighted sum follows the curve.
Gradient boosting: fit what's left
Gradient boosting (Friedman, 2001) says it more simply. Start with a constant, the mean of . Then, again and again:
- Work out the residuals, : what the model so far still gets wrong (lesson 3.1).
- Fit a small tree to the residuals.
- Add a small part of it: , with the learning rate.
On a noisy wave, with stumps and , by hand:
RMSE 0.447 after one stump, 0.199 after ten, 0.154 after thirty. Watch it build:
1 stump, each one question (“is x at most this?”). Their sum is a staircase, and its RMSE is 0.509. The residuals shrink as the stairs follow the wave; later stumps fit smaller and smaller leftovers, and eventually the noise.
Why "gradient"? For squared error, , the gradient with respect to the prediction is , minus the residual. So fitting each tree to the residuals and adding it is gradient descent (lesson 3.3), taking steps in the space of functions instead of weights. For other losses, such as cross-entropy for classification, each tree is fitted to that loss's negative gradient instead; the recipe is the same.
Learning rate and number of trees
The learning rate scales each tree's contribution. Small steps mean each tree corrects only part of the error, leaving room for later trees to correct it differently, which generalises better (Friedman called it shrinkage). The price: more trees for the same fit. The two settings trade against each other:
- A learning rate of 0.1 with a few hundred trees is a common start; 0.01 with a few thousand is often a little better, and slower.
- The number of trees can overfit, unlike a forest's: each new tree fits the remaining residuals, and once the signal is used up, what remains is noise.
So the number of trees is chosen by watching a validation score, and stopping when it stops improving: early stopping. Lesson 4.13 does it with one argument.
Wrong way: too many trees, too fast
Trees of depth 3 on the moons, at two learning rates:
With and 100 trees: 99.5% on training, 88.5% on test. With , every tree corrects the full error at once, and the model fits the training points perfectly within 100 trees; the test score drops to 87.5%. Keep going to 2,000 trees, and it drops again to 87%, with every extra tree carving the noise more finely. The training score can't warn you: it's 100% the whole time. Only a validation score can.
HistGradientBoosting, XGBoost, LightGBM, CatBoost
Plain gradient boosting tries every threshold of every feature at every split, which is slow on big data. Modern versions first sort each feature into at most 255 bins, a histogram, and only try the bin edges: many times faster, and hardly less accurate. scikit-learn's is HistGradientBoostingClassifier:
90% on the moons test set with the defaults, which grow 100 trees. It also handles missing values itself (lesson 4.1), and can take categorical features directly. The regression version, on penguin mass, on the same folds as lesson 4.5:
347 g, a little ahead of the random forest's 352 g.
Outside scikit-learn, three libraries do the same thing at larger scale, and you'll meet them everywhere:
- XGBoost (Chen and Guestrin, 2016): the one that made boosting the default winner of data-science competitions.
- LightGBM (Microsoft): histograms and leaf-wise growth, very fast on large data.
- CatBoost (Yandex): careful handling of categorical features.
All three follow scikit-learn's fit and predict, so they drop into the same pipelines. On most tables, the differences between them matter less than the features you give them.
When to use it
Good at
- Tables of thousands of rows or more: usually the most accurate model there is
- Mixed features, missing values, no scaling
- Classification, regression, ranking, with many losses to choose from
Bad at
- Images, sound and raw text, where neural networks win
- Reading the model
- Tiny data sets, where it overfits easily and simpler models are as good
- Predicting beyond the range of the training labels
Needs from you
- The learning rate and the number of trees, with early stopping
- Then tree size (max_leaf_nodes or max_depth) and regularisation, by cross-validation (lesson 4.13)
In scikit-learn
- Classification:
HistGradientBoostingClassifier,AdaBoostClassifier - Regression:
HistGradientBoostingRegressor
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Freund and Schapire, A decision-theoretic generalization of on-line learning and an application to boosting, Journal of Computer and System Sciences 55(1), 1997: AdaBoost
- Friedman, Greedy function approximation: a gradient boosting machine, Annals of Statistics 29(5), 2001: gradient boosting, and shrinkage by a learning rate
- Chen and Guestrin, XGBoost: a scalable tree boosting system, KDD 2016: the library that made boosting the default for tables
- scikit-learn: Gradient-boosted trees: HistGradientBoostingClassifier and Regressor, early stopping