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?
- Yes: enough stumps can make any shape
- No: each stump looks at one column, and their sum can't express "x and y have the same sign"
- Only with a learning rate of 1
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 + λ)
- 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\).
- 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\).
- 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)\).
- 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.
- 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:
| Library | Its big idea | What it buys |
|---|---|---|
| XGBoost (2014) | Newton leaves with λ and γ penalties; sparse-aware splits (missing values learn which way to go); parallel split search | Accuracy 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 rest | Speed 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 HistGradientBoosting | LightGBM's histogram idea, inside scikit-learn | No 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?
- Too many trees
- The encoded column wasn't scaled
- Each row's encoding includes its own label: a branch with 3 calls, one a yes, says 33% partly because of that very yes
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):
| Model | Default settings | Learning rate 0.03, early stopping on validation |
|---|---|---|
| XGBoost | 0.744 | 0.774 (171 rounds) |
| LightGBM | 0.755 | 0.772 (104 rounds) |
| CatBoost (categories as they are) | 0.773 | 0.778 (367 rounds) |
| scikit-learn histogram boosting | 0.760 | 0.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
- Overfitting by rounds. Unlike a forest, more rounds eventually fit noise. Hold out validation data and use early stopping; a smaller learning rate with more rounds is almost always better (chapter 13).
- Many knobs. Rounds, learning rate, depth or leaves, leaf size, λ, row and column subsampling. Defaults can be several points off (XGBoost here: 0.744 default, 0.774 tuned). Tune the learning rate with early stopping first, then depth and leaf size.
- Target leaks through categories. As measured above. Never encode a category by its target rate on the same rows you train on.
- Past the data, and smooth trends. Still trees: flat outside the training range, staircases for smooth effects.
- Images, sound, text. Columns that are pixels or words have structure (neighbors, order) that trees can't use; networks (stages 2 and 5) can. Boosting on embeddings from a network is a strong combination.
6. The knobs that matter
| Knob | XGBoost · LightGBM · CatBoost | What it does |
|---|---|---|
| Rounds + early stopping | n_estimators + early_stopping_rounds · callbacks · iterations + early_stopping_rounds | Set rounds high and let validation data decide where to stop. |
| Learning rate | learning_rate (all three) | 0.01 to 0.1. Smaller is better but needs more rounds. |
| Tree size | max_depth · num_leaves · depth | The order of interactions it can learn. Depth 3 to 8; LightGBM's leaves under 2^depth. |
| Leaf size | min_child_weight · min_child_samples · min_data_in_leaf | No leaf built on a handful of rows. |
| Penalties | reg_lambda, gamma · reg_lambda, min_split_gain · l2_leaf_reg | λ shrinks leaf values; γ refuses small-gain splits. |
| Subsampling | subsample, colsample_bytree · bagging_fraction, feature_fraction · subsample, rsm | Random 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