Gradient boosting (and XGBoost)
Add small trees one at a time, each fitted to the gradient of the loss: the champion of tabular data.
Animated explanation
The idea
Start with a crude prediction, the average. Look at what is left over, the residuals. Fit a small tree to predict those residuals and add a fraction of it to the model. Now the leftovers are smaller; fit another small tree to them; add it; repeat a few hundred times.
That is gradient boosting. For squared error, the residuals are exactly the negative gradient of the loss with respect to the predictions, so each step is gradient descent in the space of functions, taking steps shaped like trees. Swap in a different loss (log-loss for classification, absolute error for robustness, ranking losses) and the same recipe works. Its modern implementations, XGBoost, LightGBM and CatBoost, win most competitions on tabular data.
Shrinkage, depth and the number of trees
The learning rate and the number of trees trade off: smaller needs more trees but generalises better. Trees are kept shallow (depth 3–8), because each one is only a small correction; deep trees would overfit the residuals. Always use early stopping on a validation set to choose .
What XGBoost and LightGBM add
- Second-order (Newton) steps using the Hessian of the loss.
- L1/L2 penalties on leaf values and a cost per leaf ().
- Histogram-based split finding: bin each feature into ~256 buckets; very fast.
- Row and column subsampling; native missing-value and categorical handling.
- LightGBM grows trees leaf-wise (best leaf first) rather than level-wise.
Boosting versus forests
Forests average independent deep trees (variance reduction) and are hard to overfit; boosting adds dependent shallow trees (bias reduction) and can overfit if run too long. Tuned boosting usually wins on accuracy; forests win on robustness with zero tuning.
The algorithm, step by step
- 1Initialise to the constant minimising the loss (mean of ).
- 2For : compute pseudo-residuals .
- 3Fit a shallow regression tree to ; set each leaf to its loss-minimising value.
- 4Update .
- 5Stop when validation loss stops improving.
The maths
Additive model
Pseudo-residuals
Log-loss pseudo-residuals (classification)
XGBoost leaf weight
Strengths and weaknesses
Strengths
- State-of-the-art accuracy on tabular data
- Any differentiable loss: regression, classification, ranking
- Handles mixed feature types, missing values and interactions
Weaknesses
- Many hyperparameters; can overfit without early stopping
- Sequential training (each tree depends on the last)
- Less interpretable than a single tree
Reach for it when…
Check your understanding
Lambdian Lab
Boosting regression trees by hand
Implement gradient boosting for squared error with scikit-learn's DecisionTreeRegressor as the base learner.
- fit_gb(X, y, M, eta, depth): start from the mean; each stage fits DecisionTreeRegressor(max_depth=depth, random_state=0) to the residuals. Return (F0, trees, eta).
- predict_gb(model, X, m=None): prediction using the first m trees (all if None).
- staged_mse(model, X, y): list of MSE after 0, 1, …, M trees.