Back openDesk Edu for a sovereign, open-source education β every vote counts.
Vote nowGradient boosting is the workhorse of tabular machine learning. From Kaggle competitions to production fraud detection, boosted trees consistently outperform neural networks on structured data. Here's how it works, from first principles to XGBoost.
The metaphor is teamwork. A single weak learner β a model barely better than random guessing β can't solve hard problems. But combine many weak learners, and you get a strong one.
Two fundamentally different approaches:
| Method | Strategy | Parallelism | Weak Learners |
|---|---|---|---|
| Bagging (Bootstrap Aggregating) | Train each model independently on random data subsets, then average | Fully parallel | High-variance models (deep trees) |
| Boosting | Train models sequentially, each correcting the previous one's errors | Inherently sequential | Low-variance models (shallow trees) |
Bagging reduces variance. Boosting reduces bias. Random Forest is the most famous bagging method. Gradient boosting β and XGBoost specifically β dominates among boosting methods.
The simplest possible model: predict the mean of the target variable for all observations.
For a regression problem predicting weight (kg) from features like height, gender, and favorite color:
Initial prediction: mean(weights) = 72.2 kg
Residuals are the errors β what the model hasn't captured yet:
| Height (m) | Gender | Favorite Color | Weight (kg) | Initial Pred. | Residual |
|---|---|---|---|---|---|
| 1.6 | Male | Blue | 88 | 72.2 | +15.8 |
| 1.7 | Female | Green | 76 | 72.2 | +3.8 |
| 1.5 | Female | Blue | 56 | 72.2 | β16.2 |
| 1.8 | Male | Red | 77 | 72.2 | +4.8 |
| 1.6 | Female | Green | 64 | 72.2 | β8.2 |
Fit a shallow decision tree (typically depth 2β6) to predict the residuals, not the original target. The tree splits on the features that best separate positive and negative residuals:
Tree splits:
1. Gender = Female?
β Yes: Height < 1.7? β Yes: predict β2.2
β No: predict +4.8
β No: Favorite Color = Blue? β Yes: predict +15.8
β No: predict β16.2
Add the tree's predictions, scaled by a learning rate Ξ· (typically 0.01β0.3), to the running prediction:
New prediction = Previous prediction + Ξ· Γ tree_prediction
A small learning rate means each tree contributes only a tiny correction β which seems inefficient, but is essential for generalization. More trees (hundreds or thousands) compensate for the small step size.
Steps 2β4 repeat: compute new residuals, fit another tree, update predictions. Each tree corrects a small portion of the remaining error.
The "gradient" in gradient boosting comes from optimization theory. The residual at each step is proportional to the negative gradient of the loss function with respect to the current prediction.
For mean squared error (MSE) loss:
Loss = Ξ£(y_true β y_pred)Β² / n
Gradient = β2 Γ (y_true β y_pred) / n = β2 Γ residual / n
By fitting trees to the negative gradient, gradient boosting performs gradient descent in function space β each tree is a step in the direction that most reduces the loss.
This generalizes beyond MSE: you can use any differentiable loss function (log loss for classification, Huber loss for robustness, quantile loss for prediction intervals). The tree just fits the gradient of whichever loss you choose.
XGBoost (eXtreme Gradient Boosting) took the core ideas and made them production-ready. Key innovations:
Standard gradient boosting can overfit. XGBoost adds L1 (Lasso) and L2 (Ridge) regularization terms to the objective function, penalizing both the number of leaves and leaf weights. This is a major reason XGBoost dominates in competitions where overfitting is the enemy.
XGBoost uses a second-order Taylor expansion of the loss function, incorporating both the gradient (first derivative) and Hessian (second derivative). This provides a more accurate approximation of how the loss changes with each split, leading to better split choices.
For large datasets, finding optimal split points by scanning every value is expensive. XGBoost's weighted quantile sketch algorithm finds approximate split candidates in a single pass, weighted by the Hessian of each instance β faster and nearly as accurate as exact greedy search.
Real-world data has missing values. XGBoost learns the optimal default direction for missing values at each split. If a feature is missing, the instance goes left or right depending on which direction minimizes loss. No imputation needed.
| Feature | XGBoost | LightGBM | CatBoost | Random Forest |
|---|---|---|---|---|
| Tree growth | Level-wise | Leaf-wise | Symmetric | Independent |
| Categorical support | Manual encoding | Manual encoding | Native | Manual encoding |
| GPU training | Yes | Yes | Yes | Limited |
| Missing values | Learned | Handled | Handled | Imputation needed |
| Regularization | L1 + L2 | L1 + L2 | L2 | None |
When to use each:
early_stopping_rounds β stop training when validation performance plateaus. Prevents overfitting without guessing tree count.scale_pos_weight or a custom evaluation metric like AUC rather than accuracy.For a step-by-step walkthrough with visual decision trees, residual plots, and ensemble method comparisons, download the lecture slides: