AI from scratch · Model zoo 7 of 10 · Gradient boosting: XGBoost, LightGBM, CatBoost

Gradient boosting: XGBoost, LightGBM, CatBoost

Chapter 13 built boosting for numbers: small trees, each fit to what the previous ones still got wrong, added a little at a time. This page does it for yes/no questions, the way the three libraries that win most tabular competitions do it, and takes each library apart: what XGBoost added (a second-order step and a penalty in every leaf), what LightGBM added (histograms and leaf-wise growth, for speed), and what CatBoost added (a leak-free way to use categories). Then it measures them, honestly.

1. The idea in one picture: small trees, piling up

Each round adds a small tree (depth 1 to 4) that nudges the log-odds where the current model is most wrong. With a few rounds the map is a handful of blocks; with many, the blocks add up to a shape no single small tree could make.

Pick checkerboard (XOR) and set the tree depth to 1 (stumps: one question each). Predict first: can 300 rounds of stumps learn it?

Boosted stumps add up one-column effects: f(x) + g(y). XOR needs an interaction: the effect of x flips with the sign of y, and no sum of separate pieces does that. Depth 2 lets each tree ask about x and then y, so it learns it within a few rounds. Tree depth is the highest order of interaction the model can learn.

Boosting adds small trees one after another, each fitted to the remaining error; the depth of the trees limits which interactions it can learn, and the learning rate and number of rounds trade fit against noise.

2. How it learns: gradients, and XGBoost's second-order leaves

For log-loss, the "remaining error" of each row is the gradient of the loss with respect to its log-odds, which works out to \(g_i = p_i - y_i\) (the miss, as on page 2), and its curvature is \(h_i = p_i(1 - p_i)\). Each round fits a tree to the misses, then sets each leaf's value with a Newton step (calculus 3: divide the slope by the curvature), shrunk by a penalty λ:

\[ w_{\text{leaf}} = -\frac{\sum_{i \in \text{leaf}} g_i}{\sum_{i \in \text{leaf}} h_i + \lambda}, \qquad F_m(x) = F_{m-1}(x) + \eta \, w_{\text{leaf}(x)} \]

That's XGBoost's core (Chen and Guestrin, 2016): the curvature makes each step the right size for each leaf (rows the model is confidently wrong about, with \(p\) near 0 when \(y = 1\), have a full-size miss but tiny curvature \(h\), so their leaf takes a bold step), and λ keeps leaves with few rows from taking wild ones. It also judges every split by how much it lowers this penalized loss, and refuses splits that gain less than γ.

Why the leaf value is −G/(H + λ)
  1. Adding \(w\) to the log-odds of every row in a leaf changes the loss. Near the current model, a second-order Taylor expansion (calculus 3) gives, for each row, \(\ell(F + w) \approx \ell(F) + g_i w + \tfrac12 h_i w^2\).
  2. Summed over the leaf, plus the penalty \(\tfrac12 \lambda w^2\): \(\text{const} + G w + \tfrac12 (H + \lambda) w^2\), where \(G = \sum g_i\) and \(H = \sum h_i\).
  3. That's a parabola in \(w\) (H + λ is positive), and its bottom is where its slope \(G + (H + \lambda) w\) is zero: \(w = -G/(H + \lambda)\).
  4. Plugging it back in, the leaf lowers the loss by \(\tfrac12 G^2/(H + \lambda)\): a split's gain is the left leaf's plus the right leaf's minus the parent's. That's the score XGBoost uses to choose splits.
  5. Where it breaks: the parabola is only a local picture. With a big learning rate, the step jumps past where the approximation holds; that's one more reason to keep η small and take many steps.

3. Build it: boosting with Newton leaves, from scratch

scikit-learn's regression tree picks the splits (fit to the misses); NumPy sets the leaves with the formula above.

Mine and scikit-learn's agree (0.773). Notice that the test AUC peaked around round 10 (0.781) and then slipped: past that point the rounds fit noise in these 7,700 calls. That's why every library has early stopping.

4. What each library adds

All three are the algorithm above. The differences are engineering, and each answered a real bottleneck:

LibraryIts big ideaWhat it buys
XGBoost (2014)Newton leaves with λ and γ penalties; sparse-aware splits (missing values learn which way to go); parallel split searchAccuracy and robustness out of the box; it made boosting the default on Kaggle
LightGBM (2017)Histograms: each column bucketed into ≤ 255 bins, so a split is chosen from 255 candidates, not every distinct value; leaf-wise growth: always split the leaf that gains most, so trees are lopsided; GOSS keeps rows with big gradients and samples the restSpeed on millions of rows; num_leaves replaces depth as the size knob (and overfits faster if too big)
CatBoost (2018)Ordered target statistics for categories: a row's category is encoded by the yes-rate of that category among rows before it in a random order, never including itself; symmetric trees (one question per level)Categories used directly with no leak; good results with default settings; fast prediction
scikit-learn HistGradientBoostingLightGBM's histogram idea, inside scikit-learnNo extra install; plays with pipelines

The histogram trick is the big speedup. On 200,000 rows and 20 columns, scikit-learn's exact GradientBoostingClassifier took 79 seconds for 100 trees; the histogram version took 0.42 seconds, 190 times faster, for practically the same model.

You encode a category with 3,000 values (say, a bank branch) by each branch's yes-rate in the training data, then boost. Training AUC 0.997, test 0.56. The branch is in fact pure noise. What went wrong?

With about 2.6 training calls per branch, the branch's yes-rate is mostly the row's own answer, so the column is a leak and the trees lean on it (0.997 on training). On test rows, whose labels weren't in the average, it's noise (0.56; without the column, 0.76). CatBoost's ordered statistics compute each row's encoding only from rows before it, so the label never leaks into its own feature: with the same column, CatBoost scored 0.773. Without CatBoost: encode with out-of-fold means (TargetEncoder in scikit-learn does this), or don't encode noise-level categories at all.

5. Where it shines, where it breaks

Shines: on large tables with nonlinear effects and interactions, boosted trees are usually the most accurate model there is (page 1's ridership test: 0.34 million against the line's 0.56). Missing values, categories, mixed scales: handled. Fast to train with histograms, small to store (0.4 MB on the bank calls, against a forest's 54 MB), fast to predict.

Here's the honest scoreboard on the bank calls (test AUC, 10,000 rows):

ModelDefault settingsLearning rate 0.03, early stopping on validation
XGBoost0.7440.774 (171 rounds)
LightGBM0.7550.772 (104 rounds)
CatBoost (categories as they are)0.7730.778 (367 rounds)
scikit-learn histogram boosting0.7600.763 (194 rounds)
logistic regression (page 2)0.779

Tuning mattered more than the library, CatBoost's defaults were the best, and none beat the line: these calls are mostly additive effects of a few columns, and there are only 10,000 of them. That's not a failure of boosting; it's the reason to always fit the baseline first.

Boosted trees are usually the strongest model on tables, the libraries differ mainly in speed and in how they handle categories, and on small or simple data a line can tie them.

Where it breaks, and what people do

6. The knobs that matter

KnobXGBoost · LightGBM · CatBoostWhat it does
Rounds + early stoppingn_estimators + early_stopping_rounds · callbacks · iterations + early_stopping_roundsSet rounds high and let validation data decide where to stop.
Learning ratelearning_rate (all three)0.01 to 0.1. Smaller is better but needs more rounds.
Tree sizemax_depth · num_leaves · depthThe order of interactions it can learn. Depth 3 to 8; LightGBM's leaves under 2^depth.
Leaf sizemin_child_weight · min_child_samples · min_data_in_leafNo leaf built on a handful of rows.
Penaltiesreg_lambda, gamma · reg_lambda, min_split_gain · l2_leaf_regλ shrinks leaf values; γ refuses small-gain splits.
Subsamplingsubsample, colsample_bytree · bagging_fraction, feature_fraction · subsample, rsmRandom rows and columns per tree, as in forests: less overfitting, faster.

Next in the zoo: a model that doesn't use trees or distances or counts, but the widest gap between the classes.

All chapters: AI from scratch. Bank marketing: Moro, Cortez and Rita (2014), UCI Machine Learning Repository, CC BY 4.0.

Last updated