Gradient boosting (XGBoost, LightGBM, CatBoost)

Build small trees one after another, each one correcting the mistakes of the trees before it.

Lesson 29 of 6615 minIntermediate

You will learn to

  • Explain how boosting adds small trees that each correct the current errors
  • Describe how learning rate, number of trees and tree size trade off against each other
  • Say when gradient boosting is a strong first choice and how the three popular libraries differ

Before this lesson

Intuition

Imagine you are learning to throw darts at a target. Your first throw lands to the left. For the next throw you do not start again: you correct for the miss. Each throw makes a small correction for the error of the throws before. After enough corrections you hit the centre.

Boosting works like that. The first model is a crude guess, such as the average of the target. Then a small tree is trained to predict the errors of the guess, and its output, scaled down, is added to the prediction. A second small tree is trained on the errors that remain, and so on. Many weak steps add up to a strong model.

The word "gradient" refers to a generalisation. Squared error has a simple error to fix (the difference between the target and the prediction). For other loss functions, each tree is fitted to the direction in which the loss falls fastest, the negative gradient. The mechanism stays the same: fit a small tree to what is still wrong, add a fraction of it.

Gradient boosting on tables of data is one of the strongest general methods, and it often tops the leaderboard of a run. Its power comes with more settings to get right than a random forest.

Picture it

Start: the meanMSE 26After tree 1MSE 7.25After tree 2MSE 2.56
Boosting on four points. The flat line is the starting guess. Each small tree adds a step that moves the prediction closer to the points; the orange segments are the remaining errors.

Notice how the error (the mean squared error, MSE, under each panel) drops quickly at first and then more slowly. Each tree adds a little, which is why the learning rate and the number of trees go together.

The math

Let Fm(x)F_m(x) be the prediction after mm trees. The procedure is:

F0(x)=yˉ,Fm(x)=Fm−1(x)+η hm(x)F_0(x) = \bar{y}, \qquad F_m(x) = F_{m-1}(x) + \eta \, h_m(x)

where hmh_m is a small tree fitted to the residuals ri=yi−Fm−1(xi)r_i = y_i - F_{m-1}(x_i) and η\eta is the learning rate (also called shrinkage), a number between 0 and 1. A smaller η\eta means smaller steps.

For a general loss LL, the residuals are replaced by the negative gradient −∂L/∂F-\partial L / \partial F evaluated at the current prediction. For classification, FF is a score that a sigmoid (or softmax) turns into a probability, and the trees correct the log-loss errors.

Three families of settings control the fit:

  • Step size and count: η\eta (learning_rate) and the number of trees (n_estimators). Together they control how far the model goes.
  • Tree size: max_depth (or num_leaves) and minimum rows per leaf. Shallow trees, often depth 3 to 6, capture low-order interactions and keep each step weak.
  • Randomness and penalties: subsample (a fraction of rows per tree), column sampling, and L1/L2 penalties on leaf values (reg_alpha, reg_lambda). These reduce variance.

Worked example

Four rows with one input and the targets 10, 12, 20 and 22 at x = 1, 2, 3, 4. Use stumps (trees with a single split) and a learning rate of 0.5.

  1. Start: the mean is 16, so the predictions are 16, 16, 16, 16. The residuals are -6, -4, 4, 6 and the mean squared error is (36 + 16 + 16 + 36) / 4 = 26.
  2. Tree 1: the best split is between x = 2 and x = 3. The left leaf predicts the average residual -5 and the right leaf predicts +5. Add them scaled by 0.5: the predictions become 13.5, 13.5, 18.5, 18.5. The residuals are now -3.5, -1.5, 1.5, 3.5 and the error falls to (12.25 + 2.25 + 2.25 + 12.25) / 4 = 7.25.
  3. Tree 2: the same split is best again. The leaf averages are -2.5 and +2.5, and half of those are added: predictions 12.25, 12.25, 19.75, 19.75. The residuals are -2.25, -0.25, 0.25, 2.25 and the error falls to 2.56.

Each tree moved the predictions only half of the way (the learning rate) toward the average error of its group, so no single tree decides the result. With more trees the staircase gets closer to the data, and with a real, noisy dataset it eventually starts to chase the noise. We checked these numbers with scikit-learn's GradientBoostingRegressor.

Now the same trade-off with computed numbers. On a sample dataset with 768 rows, 5-fold stratified cross-validation in scikit-learn (seed 42, accuracy), a gradient boosting classifier with 100 trees of default depth gives:

Learning rateTreesAccuracy
0.11000.762
1.01000.716
0.15000.747

A learning rate of 1.0 takes huge steps and overfits. A small rate with 500 trees runs past the best point on this small dataset, so the score drops: too many trees is also a form of overfitting. This is why boosting is tuned with cross-validation, or with early stopping where available.

How mlflow does it

The algorithms are Gradient Boosting Classifier (gbc) and Gradient Boosting Regressor (gbr), plus three popular libraries: Extreme Gradient Boosting (xgboost), Light Gradient Boosting Machine (lightgbm) and CatBoost (catboost). They share the idea and differ in details:

  • XGBoost grows trees level by level and has explicit L1 and L2 penalties (reg_alpha, reg_lambda).
  • LightGBM grows trees leaf by leaf, so num_leaves is its main size control. It is fast on large tables.
  • CatBoost uses balanced (symmetric) trees, which are stable and tend to work well with little tuning. Its learning rate parameter is called eta in the tuning space and its tree size depth.

The three libraries appear in the algorithm list only when they are installed in the environment. The tuning ranges include learning_rate up to 0.5 and n_estimators from 10 to 300. See the pages linked below for the full lists.

Common mistakes

  • Raising the number of trees with a large learning rate. Big steps plus many rounds overfit quickly. Lower the learning rate first, then add trees.
  • Making the trees deep. Deep trees make each step strong, and the model memorises. Start at depth 3 to 6.
  • Tuning on the training score. The training error keeps falling with every tree. Always compare cross-validated scores.
  • Assuming a boosting library is always best. On small or very noisy tables a regularised linear model or a random forest can match it with less tuning. Compare against a baseline.
  • Forgetting the cost. Trees are trained one after another, so training does not parallelise across trees as in a forest. Plan for the time when you tune many settings.

Try it in mlflow

Slow down a boosting model and watch the score

  1. Open Pipelines and pick a classification or regression pipeline.
  2. Open the Models part and add Gradient Boosting Classifier (or Regressor) in Automatic mode.
  3. Add it again, switch that card to Manual and set learning_rate to 0.05 and n_estimators to 300.
  4. Click Train and compare the two cross-validated scores and the training times.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. In gradient boosting with squared error, what does each new tree try to predict?
  2. 2. You halve the learning rate. What usually has to change to keep the same quality?
  3. 3. Compared with a random forest, which is true of gradient boosting?