Gradient boosting (XGBoost)
How gradient boosting builds many small trees one after another, each correcting the errors of the last, and how XGBoost and similar libraries make this the leading method for tabular data.
What it is
Gradient boosting is a machine-learning method that builds a strong predictor by adding many weak models, usually shallow decision trees, one at a time, with each new tree trained to correct the errors of the ensemble so far. The idea was formalized by Jerome Friedman in 2001 as gradient descent in function space. Practical libraries made it fast and robust: XGBoost (Chen and Guestrin, 2016), LightGBM (Microsoft) and CatBoost (Yandex). They regularly win tabular-data competitions and are widely used in finance, insurance, chemistry and materials property prediction, ecology, medicine and online advertising. Unlike random forests, which average independent deep trees (see decision trees and random forests), boosting builds shallow trees sequentially.
It answers questions such as: What is the best predictive model for this structured data set? Which features drive the prediction? How can a model be trained quickly on millions of rows with missing values? How does one model handle numeric and categorical variables, interactions and nonlinear effects? It typically gives higher accuracy than a random forest when properly tuned, at the cost of more tuning and more risk of overfitting.
Fundamentals
The boosting idea
The model is an additive sum: FM(x) = F0(x) + η Σm=1M hm(x), where F0 is a constant (the mean for regression), hm are small trees and η is the learning rate (shrinkage), typically 0.01–0.3. At each round, the algorithm computes the pseudo-residuals, the negative gradient of the loss L with respect to the current predictions, ri = −∂L(yi, F(xi))/∂F(xi), and fits a tree hm to them. For squared error the pseudo-residuals are simply the ordinary residuals y − F(x). For binary classification with log-loss they are y − p, with p the predicted probability from the logistic function of F. The new tree is added with a small step η so that learning is gradual. Any differentiable loss can be used: squared error, absolute error or Huber for regression, log-loss for classification, quantile loss for prediction intervals, Poisson, Cox survival and ranking losses. Early boosting methods such as AdaBoost reweighted misclassified samples, which is a special case.
XGBoost: the regularized second-order objective
XGBoost improves the basic algorithm by optimizing a regularized objective with a second-order Taylor expansion of the loss. For a tree with T leaves and leaf weights wj, the objective is Σi L(yi, ŷi) + Σm Ω(hm) with Ω = γT + ½λΣwj2 (+ α‖w‖1). Using the gradients gi and Hessians hi of the loss, the optimal weight for a leaf is w* = −G/(H + λ), where G and H are the sums of g and h in the leaf. The gain of a split is ½[GL2/(HL + λ) + GR2/(HR + λ) − (GL + GR)2/(HL + HR + λ)] − γ, and a split is made only if the gain is positive, so γ also acts as automatic pruning. Engineering features include sparse-aware handling of missing values (learning a default direction at each split), approximate histogram-based split finding, parallel and GPU computation, and out-of-core training. LightGBM adds leaf-wise growth, gradient-based sampling and feature bundling, giving high speed on large data; CatBoost uses ordered boosting and target statistics to handle categorical variables with less leakage.
Key hyperparameters and tuning
The main controls are: n_estimators (number of trees) with a learning rate (smaller rates need more trees and usually generalize better); max_depth (3–8; shallow trees capture low-order interactions); min_child_weight or minimum samples per leaf; subsample (row sampling, 0.5–1) and colsample_bytree (column sampling), which add randomness; and regularization lambda, alpha and gamma. Early stopping on a validation set stops adding trees when validation loss no longer improves for a set number of rounds, which is the standard way to choose the number of trees. Tuning is done by cross-validation with random search or Bayesian optimization (Optuna, see Bayesian optimization), starting with a moderate learning rate such as 0.1 to find the number of trees, then tuning tree structure and sampling, and finally lowering the learning rate. Class imbalance can be handled with scale_pos_weight or sample weights, and monotonic constraints force sensible relationships.
Interpretation and pitfalls
Feature importance can be measured by gain, split count or cover (built-in), by permutation, or with SHAP values (see explainable AI), which give consistent per-prediction contributions and are efficient for trees (TreeSHAP). Partial dependence and accumulated local effects show average feature effects. Pitfalls: overfitting if there are too many deep trees with a high learning rate; leakage when the test data influence preprocessing or target encoding; time-ordered or grouped data needing time-based or group-wise splits; probabilities that may need calibration; and no extrapolation beyond the training range. For small data sets, noisy targets or images, text and audio, other methods may be better (see neural networks).
Typical applications and limits
Used for credit risk, demand forecasting, customer churn, property prediction in chemistry and materials, medical risk scores and ranking in search. Limits include sensitivity to hyperparameters, long training for very large ensembles, lower interpretability than a single model, and the risk of confident predictions on inputs unlike the training data.
Basic operation
- Prepare the data. Clean and split into training, validation and test sets in a way that matches deployment.
- Choose the loss and metric. Match them to the task, such as log-loss for classification.
- Train a baseline. Fit with default settings and early stopping.
- Tune hyperparameters. Search depth, learning rate, sampling and regularization with cross-validation.
- Check for overfitting. Compare training and validation curves.
- Evaluate once on the test set. Report appropriate metrics with uncertainty.
- Interpret. Use SHAP values and partial dependence.
- Collect the output. You obtain a trained model and feature rankings; tuning strategy and early stopping are covered in the video.
Good practice
- Use early stopping. Choose the number of trees from validation data.
- Split data like deployment. Use time or group splits where needed.
- Start simple. Compare with a linear baseline.
- Guard against leakage. Keep target-derived features inside cross-validation folds.
- Record settings and seeds. Keep experiments reproducible.
- Audit high-stakes models. Check fairness, calibration and explanations before decisions affecting people are based on the model.
Key terms
- Boosting — building a model by adding weak learners that correct earlier errors.
- Pseudo-residual — the negative gradient of the loss, which each new tree fits.
- Learning rate (shrinkage) — a factor scaling each tree’s contribution.
- Early stopping — halting training when validation performance stops improving.
- XGBoost — a fast, regularized gradient boosting library.
- SHAP value — a measure of each feature’s contribution to a single prediction.
- Subsampling — training each tree on a random fraction of rows or columns.
Video coming soon
About five minutes on Gradient boosting (XGBoost): how it works in detail, how the data is acquired, and how it is analyzed.
More in 4.3 AI and Machine Learning
- Regularized linear models
- Decision trees and random forests
- Gradient boosting (XGBoost)
- Support vector machines
- k-NN
- k-means
- Gaussian processes
- Neural networks
- CNNs
- RNN/LSTM
- Transformers
- Autoencoders
- GANs
- Diffusion models
- Graph neural networks
- Reinforcement learning
- Transfer learning
- Federated learning
- Explainable AI (SHAP, LIME)
- Physics-informed neural networks
- Bayesian optimization
- Active learning
- Symbolic regression
- Genetic algorithms and particle swarm optimization
- Dimensionality reduction (t-SNE, UMAP)
- NLP and large language models
- Retrieval-augmented generation
- Computer vision (detection, segmentation)
- Anomaly detection
- Machine-learned interatomic potentials
- AutoML
