Skip to main content

XGBoost by hand, part 1: a sum of small trees

machine learning
xgboost
concepts
r
animation
XGBoost on four data points, one round at a time: start from 0.5, fit a tree to the residuals, shrink it, add it. Animated, and checked against the real xgboost.
Author

Athos Damiani

Published

December 3, 2026

I have a slide deck, in Portuguese, that computes XGBoost by hand on a dataset of four points. This series turns it into animations. Every number in the images comes from a small implementation I wrote for the animations, and I checked each one against the xgboost R package. Each post ends with the R code that prints the same numbers.

  1. A sum of small trees (this post): what the model is and how it learns, round by round.
  2. How one tree is grown and pruned: similarity, gain, γ and λ.
  3. Gradients, hessians and classification: where the formulas come from, and the other hyperparameters.

The data

Four doses of a drug and their effect. Too little or too much is bad, so a straight line can’t fit this. Trees can.

dose y
2 -6
8 4
12 5
16 -5

The model

An XGBoost model is a starting value plus a sum of trees, each one multiplied by a small number:

\[ f(x) = 0.5 + \varepsilon \cdot \text{tree}_1(x) + \varepsilon \cdot \text{tree}_2(x) + \dots \]

  • 0.5 is the starting prediction for everyone (base_score).
  • ε is the learning rate (eta). Here it’s 0.3.
  • each tree is fitted to the residuals left by everything before it: y − f(x).

That’s the whole loop: compute the residuals, fit a small tree to them, shrink the tree by ε, add it to the model, repeat.

The trees here have depth 2, so each one cuts the doses into at most three groups. With squared error and no regularization, a leaf outputs the mean residual of its doses. Part 2 shows how the tree picks its cuts, and part 3 shows why the leaf output is a mean.

After 20 rounds the curve goes through all four points. With four points that’s memorizing, not learning, and that’s where the regularization in part 2 comes in.

The first two trees, piece by piece

The prediction for dose 2 after two trees is 0.5 − 1.95 − 1.365 = −2.815. Each tree moves it 30% of the way it would like to go, and the next tree works on what is left: the residual of dose 2 goes from −6.5 to −4.55, then to −3.185.

The same numbers from xgboost

The R package returns the same two trees. A few settings make it match the hand computation:

  • lambda = 0, so the leaves output plain means. The default is 1.
  • base_score = 0.5. Recent versions estimate it from the data when you don’t set it (here it would be −0.5, the mean of y).
  • tree_method = "exact" tries every threshold, as we do by hand. The default, "hist", groups the values into bins first.
library(xgboost)

d <- data.frame(dose = c(2, 8, 12, 16), y = c(-6, 4, 5, -5))
dtrain <- xgb.DMatrix(as.matrix(d["dose"]), label = d$y)

fit <- xgb.train(
  params = list(
    objective = "reg:squarederror", base_score = 0.5, eta = 0.3,
    max_depth = 2, lambda = 0, gamma = 0, tree_method = "exact"
  ),
  data = dtrain,
  nrounds = 2
)

xgb.model.dt.tree(model = fit)[, .(Tree, ID, Feature, Split, Gain)]
     Tree     ID Feature Split     Gain
    <int> <char>  <char> <num>    <num>
 1:     0    0-0    dose     5 40.33333
 2:     0    0-1    Leaf    NA -1.95000
 3:     0    0-2    dose    14 60.16667
 4:     0    0-3    Leaf    NA  1.20000
 5:     0    0-4    Leaf    NA -1.65000
 6:     1    1-0    dose     5 19.76333
 7:     1    1-1    Leaf    NA -1.36500
 8:     1    1-2    dose    14 29.48166
 9:     1    1-3    Leaf    NA  0.84000
10:     1    1-4    Leaf    NA -1.15500
predict(fit, dtrain)
[1] -2.815  2.540  2.540 -2.305

In the Leaf rows, the Gain column holds the leaf’s output, already multiplied by eta: −1.95 is 0.3 × −6.5. xgboost puts the second cut at 14, halfway between 12 and 16. The predictions are −2.815, 2.54, 2.54 and −2.305, the same as in the picture above.

Why shrink each tree?

Without ε, each tree would jump straight to the residuals, and noise is part of the residuals. Below are the same data, the same depth-2 trees and three learning rates. The data are 60 noisy points around a curve, and the bottom panel shows the error against the true curve (the dashed line), which in real life you’d estimate on validation data.

  • ε = 1 is at its best by round 3, then starts fitting the noise.
  • ε = 0.3 gets lower, with its best at round 14, then slowly gets worse.
  • ε = 0.05 needs many more trees and is still improving at round 100.

A smaller learning rate needs more trees, and each tree copies less of the noise. In practice you set a small eta and pick the number of trees with early stopping on validation data. The untuned vs tuned xgboost post shows the same idea on 1,000 noisy points.

How the animations were made

The images come from Python: an exact, one-feature version of XGBoost in about 100 lines, and matplotlib frames saved as GIFs. The code is in this post’s folder on GitHub (_xgb_core.py and _make_gifs.py). Running python _xgb_core.py checks it against the xgboost numbers in this series.

Next: part 2, how one tree is grown and pruned.