← Explainer Library

Interactive Explainer

XGBoost, Tree by Tree

Gradient boosting in slow motion: each new tree fits the residual of the previous ensemble. Click Add tree; watch the prediction sharpen and the loss drop. Then see why the second-order (Newton) update XGBoost actually uses beats the vanilla first-order one.

Step 1

The boosting recipe in two lines

Build a sequence of weak learners (small trees), each fitting the residual of the running prediction. The ensemble prediction is the sum of all trees.

$\eta$ is the learning rate. Smaller $\eta$ + more trees is almost always better than larger $\eta$ + fewer trees, up to a budget. $f_t$ is what tree $t$ outputs on input $x$.

What XGBoost adds

Vanilla gradient boosting fits $f_t$ to the negative gradient $-\partial L / \partial \hat y$ (a first-order approximation). XGBoost (Chen & Guestrin, 2016) does a second-order Taylor expansion and fits the tree to optimise the Newton-step direction. Its split-gain and leaf-value formulas are:

where $g_i = \partial L / \partial \hat y_i$ and $h_i = \partial^2 L / \partial \hat y_i^2$. For squared loss $L = \frac{1}{2}(y - \hat y)^2$ this reduces to vanilla GBM ($g = \hat y - y$, $h = 1$). For logistic loss XGBoost's Hessian-aware update converges meaningfully faster.

Step 2

Tree by tree, live

A 1-D regression toy with a hidden curve. Click Add tree; the next tree fits the current residual; the ensemble prediction (teal) updates. Compare to a single deep tree and a random forest of 25 trees on the same data.

trees 0 train RMSE
Training data (●), true curve (dashed), boosted ensemble (teal solid), latest tree's contribution (orange dashed).
Residuals after the current ensemble. Boosting drives this toward zero.
Train RMSE vs number of trees. Watch the elbow shape.

Compare to single tree + random forest

single deep tree (depth 8)
random forest (25 trees, depth 6)
boosted ensemble (current)
Step 3

What you should see

Step 4

Production XGBoost — what to actually tune

The slogan. For tabular data with mixed types, missing values, and $\le$ a few million rows, XGBoost / LightGBM / CatBoost are still the strongest and simplest default. Neural-tabular models (TabNet, FT-Transformer, SAINT) win benchmarks occasionally; gradient-boosted trees still win most of the production time.
Step 5

Variants worth knowing