Skip to content
Lambdian

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 η\eta and the number of trees MM trade off: smaller η\eta 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 MM.

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 (γ\gamma).
  • 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

  1. 1
    Initialise F0F_0 to the constant minimising the loss (mean of yy).
  2. 2
    For m=1..Mm = 1..M: compute pseudo-residuals ri=−∂L/∂F(xi)r_i = -\partial L/\partial F(\mathbf{x}_i).
  3. 3
    Fit a shallow regression tree hmh_m to (xi,ri)(\mathbf{x}_i, r_i); set each leaf to its loss-minimising value.
  4. 4
    Update Fm=Fm−1+η hmF_m = F_{m-1} + \eta\,h_m.
  5. 5
    Stop when validation loss stops improving.

The maths

Additive model

FM(x)=F0+η∑m=1Mhm(x)F_M(\mathbf{x}) = F_0 + \eta\sum_{m=1}^M h_m(\mathbf{x})

Pseudo-residuals

rim=−∂L(yi,F)∂F∣Fm−1(xi)r_{im} = -\frac{\partial L(y_i, F)}{\partial F}\Big|_{F_{m-1}(\mathbf{x}_i)}

Log-loss pseudo-residuals (classification)

ri=yi−σ(F(xi))r_i = y_i - \sigma(F(\mathbf{x}_i))

XGBoost leaf weight

wj∗=−∑i∈jgi∑i∈jhi+λw_j^\ast = -\frac{\sum_{i\in j} g_i}{\sum_{i\in j} h_i + \lambda}

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…

Structured/tabular prediction problems where accuracy matters: credit risk, churn, pricing, ranking.

Check your understanding

Question 1

Q1In gradient boosting with squared error, each new tree is fitted to…

Question 2

Q2You halve the learning rate. To keep the same training fit you should…

Question 3

Q3Why are boosted trees kept shallow?

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.
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.