Home / ML 101 / Module 3 / Lesson 3

Gradient Boosting

Sequential ensembles that learn from their mistakes — the algorithm behind XGBoost and most Kaggle winners.

~14 min read M3 · L3 Advanced

The Core Idea: Learn from Mistakes

Random forests build many trees in parallel, each one independently trained on a bootstrap sample. Gradient boosting takes a completely different approach: it builds trees sequentially, where each new tree specifically targets the errors made by all previous trees combined.

Think of it like a team of specialists. The first specialist makes a rough prediction. A second specialist studies only what the first got wrong and corrects it. A third specialist studies what the first two combined still got wrong, and corrects that. Repeat until satisfied. Each specialist is humble — they only tackle the residual error — but together they can achieve remarkable accuracy.

Residuals: What You Got Wrong

After the first tree makes predictions, we compute residuals — the difference between true labels and predicted values. These residuals represent the signal that the first tree failed to capture. The second tree's job is not to predict the original target, but to predict these residuals. When we add the second tree's predictions to the first tree's predictions, the combined model is more accurate.

T₁: Base model
→
initial predictions → compute residuals r₁
T₂: Fit residuals
→
predict r₁ → update: F₂ = F₁ + η·T₂ → compute residuals r₂
T₃: Fit residuals
→
predict r₂ → update: F₃ = F₂ + η·T₃ → compute residuals r₃
⋮
→
continue for M rounds

The Math: Gradient Descent in Function Space

Why are these called "residuals" in the regression case but "gradients" in the general case? Because gradient boosting is doing gradient descent — not in parameter space, but in function space.

At each step, we compute the negative gradient of the loss function with respect to our current predictions. For mean squared error (MSE), the negative gradient is exactly the residuals. For other loss functions (log-loss, Huber loss, etc.), the negative gradient gives us a generalized form of residuals that still has the same intuitive meaning: "what direction should we push our predictions to reduce the loss?"

Update Rule
F_m(x)=F_{m-1}(x)+\eta\,h_m(x)
The model after m steps is the model after m−1 steps plus a new tree h_m scaled by learning rate η. The tree h_m fits the negative gradient of the loss at the current predictions.

The Learning Rate: Shrinkage

Notice the η (eta) in the update rule. This is the learning rate, typically set between 0.01 and 0.3. Rather than adding each new tree at full strength, we scale it down by η before adding it to the ensemble.

This shrinkage technique is crucial. A small learning rate means each tree makes only a small correction, so you need more trees to reach the same training accuracy. But the benefit is substantial: smaller learning rates generally produce better-generalizing models that are less prone to overfitting. The trade-off: computation time increases proportionally.

In practice, η = 0.1 with ~100–500 trees is a reasonable starting point. For highest accuracy with enough compute, η = 0.01–0.05 with 1000–5000 trees (using early stopping) often wins.

Weak Learners: Shallow Trees

Gradient boosting deliberately uses shallow trees as its base learners — typically with maximum depth of 3 to 6. These are called "weak learners" because individually they perform only slightly better than random guessing.

Why Not Deep Trees?

Deep trees would overfit the residuals, capturing noise rather than signal. Shallow trees only capture the most important patterns at each step, leaving meaningful signal in the residuals for subsequent trees to address. The ensemble of many shallow trees can model complex interactions without any single tree memorizing the training data.

Depth 3 means each tree can capture at most 3-way feature interactions (e.g., if feature A > threshold AND feature B > threshold AND feature C > threshold, then predict Y). For most problems this is enough — and the sequential boosting process handles the rest through iteration.

XGBoost and LightGBM: Modern Gradient Boosting

Classic gradient boosting (described by Friedman in 1999) is powerful but can be slow and memory-intensive. Two modern implementations transformed the landscape:

XGBoost (2014)

Chen and Guestrin's XGBoost introduced several key improvements: regularization terms in the objective function (L1 and L2 penalties on tree weights and leaf scores), parallelized tree construction (via sorted feature pre-processing), out-of-core computation for data that doesn't fit in RAM, and sparsity awareness for handling missing values efficiently. XGBoost dominated Kaggle from 2014–2017.

LightGBM (2017)

Microsoft's LightGBM added two crucial algorithmic improvements: Gradient-based One-Side Sampling (GOSS) — keep instances with large gradients, randomly sample those with small gradients — and Exclusive Feature Bundling (EFB) — bundle mutually exclusive sparse features together. The result: 10–100× speedup over XGBoost on large datasets with comparable accuracy. For most new projects, LightGBM is the practical default.

The XGBoost Objective Function

XGBoost minimizes a regularized objective that explicitly penalizes model complexity:

XGBoost Objective
\mathcal{L}=\sum_i l(y_i,\hat{y}_i)+\sum_k\Omega(f_k)
The first term is the standard loss (e.g., MSE or log-loss). The second term Ω(f_k) penalizes each tree f_k for complexity — specifically the number of leaves and the magnitude of leaf weights. This regularization is built into tree construction, not added as an afterthought.

Key Hyperparameters

Gradient boosting has more hyperparameters than random forests, but the most important ones are well-understood:

n_estimators
Use early stopping
Number of boosting rounds. Set high and let early stopping find the optimal value.
learning_rate (η)
0.01 – 0.3
Shrinkage factor per tree. Lower = better generalization, more trees needed.
max_depth
3 – 6
Maximum depth of each tree. Controls the degree of feature interactions captured.
subsample
0.6 – 0.9
Fraction of training rows used per tree. Adds stochasticity, reduces overfitting.
colsample_bytree
0.5 – 0.9
Fraction of features sampled per tree. Similar to random forest feature selection.
reg_lambda / alpha
0 – 10
L2 and L1 regularization on leaf weights. Penalizes tree complexity directly.

Early Stopping: The Essential Technique

Unlike random forests (where more trees always help), gradient boosting can overfit if you add too many trees. The standard solution is early stopping: monitor validation loss at each boosting round and stop when it starts increasing.

The practical recipe: set n_estimators high (e.g., 5000), use a small learning rate (0.05), and specify early_stopping_rounds=50. The model will stop when validation loss hasn't improved for 50 consecutive rounds. You don't need to grid-search n_estimators — early stopping finds the optimal value automatically. The best round is often 200–800 trees, depending on the problem and learning rate.

Rule of Thumb: η ↓ ⟹ Rounds ↑

Smaller learning rates require more trees but produce better-generalizing models. If you halve the learning rate, roughly double the number of trees. XGBoost and LightGBM with learning_rate=0.01 and early stopping often outperform tuned random forests on the same data.

Gradient Boosting vs. Random Forests

Both are tree ensemble methods, but their philosophies differ fundamentally:

Real-World Applications

Gradient boosting excels wherever tabular data is involved:


Key Takeaways

Gradient boosting builds trees sequentially, each fitting the residuals (negative gradient of the loss) left by all previous trees. A learning rate η controls shrinkage — smaller η means better generalization but more trees. Use shallow trees (depth 3–6) as weak learners. XGBoost and LightGBM add regularization and speed. Always use early stopping to find the optimal number of trees automatically. On tabular data, gradient boosting is often the highest-accuracy method available.

Previous M3-L2: Random Forests Module Overview Next Lesson M4-L1: Maximum Margin Classifier