Back to Blog
Ensemble LearningAugust 17, 202616 min read

XGBoost Explained: Gradient Boosting, Regularization, and Feature Importance

How XGBoost builds trees sequentially on residuals, why regularization and shrinkage matter, and how to read gain-based feature importance.

XGBoost (eXtreme Gradient Boosting) is a scalable implementation of gradient boosted decision trees. It won a generation of Kaggle competitions and remains a default model for tabular data: credit risk, click prediction, churn, pricing, and manufacturing quality. Unlike a random forest, which averages independent trees, boosting adds trees sequentially, each one trained to correct the residual errors of the current ensemble.

This XGBoost tutorial covers the boosting idea, the regularized objective, shrinkage, column sampling, feature importance, and how XGBoost differs from random forests and neural nets on tables.

Boosting in one picture

Start with a constant prediction (often the mean for regression). Compute residuals — the part of yy still unexplained. Fit a shallow tree to those residuals. Add it, scaled by a learning rate η\eta:

Fm(x)=Fm1(x)+ηhm(x).F_m(\mathbf{x})=F_{m-1}(\mathbf{x})+\eta\, h_m(\mathbf{x}).

Repeat. Early trees capture main effects; later trees mop up interactions and leftovers. If you add too many trees with too large η\eta, you memorize noise.

Gradient boosting generalizes residuals: each tree fits the gradient of the loss with respect to the current predictions. For squared error, that gradient is the residual. For logistic loss, it is piyip_i-y_i.

What XGBoost adds

Chen and Guestrin’s XGBoost engine popularized several production details:

  • A regularized objective with both leaf weights and tree complexity.
  • Second-order (Newton) information, not just gradients.
  • Shrinkage (learning rate) after every tree.
  • Column subsampling and row subsampling, forest-style.
  • Handling of missing values by learning a default direction at each split.
  • System tricks: cache-aware blocking, sparsity-aware split finding, optional histogram approximations.

A simplified regularized objective for a tree with leaf weights wjw_j is

L=il(yi,y^i)+γT+λ2j=1Twj2,\mathcal{L}=\sum_i l(y_i,\hat y_i)+\gamma T+\frac{\lambda}{2}\sum_{j=1}^{T}w_j^2,

where TT is the number of leaves. γ\gamma penalizes extra leaves; λ\lambda shrinks leaf weights. That is why XGBoost trees are often shallower and less wild than an unregularized CART fit.

Experiment with the XGBoost Calculator to see boosting rounds and feature importance without installing the library first.

XGBoost versus random forest versus neural nets

Random forest trains deep trees independently on bootstrap samples and averages them. Variance drops; bias stays close to that of a single tree. XGBoost reduces bias by focusing later trees on mistakes. Forests are harder to overfit with default settings; boosting overfits if n_estimators is huge and η\eta is large without early stopping.

On tabular data with mixed types, missing cells, and nn in the thousands to millions, gradient boosting (XGBoost, LightGBM, CatBoost) usually beats an off-the-shelf neural net. On images, audio, and raw text, the opposite is true.

Read decision trees and random forests first if splits, Gini, and bagging are still fuzzy — boosting reuses the same split machinery.

Feature importance (and its traps)

XGBoost reports several importance scores:

  • Gain: average loss reduction from splits on that feature.
  • Cover: how many samples pass through those splits.
  • Frequency: how often the feature is chosen.

Gain is the usual default. It is not a causal ranking. Correlated features share credit; a proxy can outrank the scientifically meaningful variable. Permutation importance on a validation set is a useful cross-check: shuffle one column and measure the drop in a metric you care about.

Do not tune the model by staring at importance plots. Tune with cross-validation and a locked test set.

A practical hyperparameter map

KnobEffect if increased
n_estimatorsMore trees; more capacity; need early stopping
learning_rate (η\eta)Smaller = slower, usually more trees, often better
max_depthDeeper interactions; overfitting risk
min_child_weightConservative splits
subsample / colsample_bytreeStochastic boosting; often more robust
reg_lambda / reg_alphaStronger shrinkage of leaf weights
gammaHigher split cost; simpler trees

Use hyperparameter search with early stopping on a validation fold. Searching n_estimators without early stopping wastes budget.

Frequently asked questions

Is XGBoost a boosting or bagging method?

Boosting. Bagging (random forest) builds trees in parallel on resamples. Boosting builds them in series on residuals/gradients.

XGBoost vs LightGBM vs CatBoost?

All are gradient-boosted trees. LightGBM grows leaf-wise and bins features for speed. CatBoost handles categoricals with ordered target statistics. XGBoost is the common baseline and has excellent documentation. Try all three on a serious tabular problem.

Can XGBoost output probabilities?

Yes for classification objectives (binary:logistic, multi:softprob). Check calibration; boosting can be overconfident. Platt scaling or isotonic regression on a validation set helps if you need honest probabilities.

Why does training accuracy keep rising while validation AUC falls?

You are past the early-stopping point. More trees are fitting noise. Restore the iteration with the best validation score.

Next steps

Train a random forest and an XGBoost model on the same table with the same CV splits. Compare AUC and calibration. Then open the XGBoost Calculator and the Random Forest solver to see bagging versus boosting as algorithms, not as library names. When you tune max_depth and η\eta, use the hyperparameter tuning guide so the search does not leak.

Continue reading