Skip to main content

XGBoost by hand, part 2: how one tree is grown and pruned

machine learning
xgboost
concepts
r
animation
Similarity scores, gain, the greedy split search, pruning with gamma and shrinking with lambda, animated on four data points and checked against xgboost.
Author

Athos Damiani

Published

December 3, 2026

This is part 2 of a series based on a slide deck I made, in Portuguese, that computes XGBoost by hand.

  1. A sum of small trees
  2. How one tree is grown and pruned (this post)
  3. Gradients, hessians and classification

Part 1 took the trees as given. Here we grow the first one. It’s fitted to the residuals of the starting prediction 0.5: −6.5, 3.5, 4.5 and −5.5 for doses 2, 8, 12 and 16.

Similarity: how well a group of residuals agrees

XGBoost scores a group of residuals with a similarity score:

\[ \text{similarity} = \frac{(\text{sum of residuals})^2}{\text{number of residuals} + \lambda} \]

Residuals with the same sign add up, and the score is high. Residuals with opposite signs cancel out, and the score is low. For now λ = 0. With all four residuals in one group:

\[ \frac{(-6.5 + 3.5 + 4.5 - 5.5)^2}{4} = \frac{(-4)^2}{4} = 4 \]

That’s low: the residuals cancel out, so one number can’t describe them.

Gain: is a split worth it?

A split divides a group in two. Its gain is how much the similarity improves:

\[ \text{gain} = \text{similarity}_\text{left} + \text{similarity}_\text{right} - \text{similarity}_\text{parent} \]

XGBoost tries every threshold between two neighbouring doses, keeps the split with the highest gain and repeats inside each side until it reaches max_depth:

node split gain
root dose < 5 40.33
root dose < 10 1.00
root dose < 14 27.00
right node dose < 10 10.67
right node dose < 14 60.17

The search is greedy: it picks the best split at each node and never goes back. Each leaf then outputs the mean residual of its doses: −6.5, 4 and −5.5. With λ the formula becomes sum of residuals / (number of residuals + λ). Part 3 shows where both formulas come from.

γ (gamma): pruning splits that don’t gain enough

gamma is the minimum gain a split needs. After the tree is grown, XGBoost goes back from the bottom up and removes every split with gain − γ < 0. It stops at the first split it keeps, so a weak split survives when a stronger split below it survives.

  • γ = 45: the root’s gain (40.33) is below γ, but its child (60.17) is above, so both stay.
  • γ above 60.17: the child goes, then the root has only leaves below it and its gain is below γ, so it goes too. The tree becomes a single leaf, the mean of all residuals: −1.

For a whole model, here are the two trees from part 1 with gamma = 50. The first tree keeps both splits, as above. The second tree’s gains are 19.76 and 29.48, both below 50, so it becomes a single leaf: the mean of its residuals, (−4.55 + 2.3 + 3.3 − 3.85) / 4 = −0.7, times 0.3:

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 <- function(lambda, gamma, nrounds) {
  xgb.train(
    params = list(
      objective = "reg:squarederror", base_score = 0.5, eta = 0.3,
      max_depth = 2, lambda = lambda, gamma = gamma, tree_method = "exact"
    ),
    data = dtrain,
    nrounds = nrounds
  )
}

fit <- fit_xgb(lambda = 0, gamma = 50, 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    Leaf    NA -0.21000
predict(fit, dtrain)
[1] -1.66  1.49  1.49 -1.36

The xgboost paper writes the gain with a factor of ½. The R package reports the gain without it and compares that number with γ, and so do the numbers in this post. You can see it above: with the ½, the 60.17 split would be 30.08 and γ = 50 would prune it.

λ (lambda): shrinking the leaves

lambda goes in the denominator of both formulas, the similarity and the leaf output. As λ grows, every leaf output moves toward zero, and leaves with few residuals move faster: with λ = 1, a leaf with one residual loses half of its output, and a leaf with two loses a third.

λ also lowers the gains, which means more splits fall below γ. With λ = 1:

fit <- fit_xgb(lambda = 1, gamma = 0, nrounds = 1)
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 19.4874992
2:     0    0-1    Leaf    NA -0.9750000
3:     0    0-2    dose    14 34.8958359
4:     0    0-3    Leaf    NA  0.8000001
5:     0    0-4    Leaf    NA -0.8250000

The gains drop from 40.33 and 60.17 to 19.49 and 34.90. The leaves, divided by 0.3, are −3.25, 2.67 and −2.75, where they were −6.5, 4 and −5.5. γ and λ work together: λ makes every split look less useful, and γ sets how useful it has to be.

Next: part 3, gradients, hessians and classification. The code for the animations is in this post’s folder on GitHub.