XGBoost (eXtreme Gradient Boosting) is a highly optimized, scalable implementation of gradient-boosted decision trees that consistently ranks among the top-performing algorithms in structured-data competitions and production ML systems. It solves regression, classification, ranking, and survival problems by sequentially fitting trees to residuals, with second-order Taylor expansion of the loss enabling both speed and strong regularization. The key mental model: XGBoost is not one algorithm β it is a framework; every major behavior from tree structure to sampling to the objective function is configurable, and nearly every real-world win comes from understanding which lever to pull first.
What This Cheat Sheet Covers
This topic spans 19 focused tables and 126 indexed concepts, 93 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: DMatrix β Data Loading and Construction
The DMatrix is XGBoost's native data container; all training, evaluation, and prediction flows through it. Feeding data via DMatrix rather than raw arrays enables efficient internal compression and avoids redundant work across boosting rounds.
| Method | Example | Description | |
|---|---|---|---|
dtrain = xgb.DMatrix(X, label=y) | Wraps a NumPy array or Pandas DataFrame with optional label, weight, and base_margin. | ||
dtrain = xgb.DMatrix(df[feats], label=df['y']) | β’ Accepts a pd.DataFrameβ’ column names are preserved as feature names automatically | ||
dtrain = xgb.DMatrix(csr_matrix) | β’ Accepts scipy.sparse.csr_matrixβ’ implicit zeros are treated as missing, not as the value 0 β convert to dense if zeros are real values | ||
dtrain = xgb.DMatrix(X, label=y, missing=np.nan) | β’ Explicitly declares which value should be treated as missing β’ default is np.nan. | ||
dtrain = xgb.DMatrix(X, label=y, weight=w) | β’ Per-sample training weights β’ higher weights increase a sample's influence on gradient updates | ||
dtrain = xgb.DMatrix(X, label=y, base_margin=prior_scores) | β’ Per-sample initial prediction offset (raw margin, before link function) β’ overrides base_score when providedβ’ used to warm-start from another model's output | ||
dtrain = xgb.DMatrix(X, feature_names=['age','income']) | Names are stored inside the DMatrix and propagate to feature importance plots automatically. | ||
dm = xgb.QuantileDMatrix(X, label=y) | β’ Pre-quantizes data for the hist tree method, cutting memory use by ~8Γ (float64 β 1 byte per entry)β’ only compatible with tree_method="hist". | ||
dtrain = xgb.DMatrix('train.csv?format=csv&label_column=0') | β’ Loads files directly from disk β’ LibSVM format via 'train.libsvm'β’ useful for large files that should not be loaded into Python memory | ||
dm = xgb.ExtMemQuantileDMatrix(it, max_bin=256) | β’ Streams data in batches via a user-supplied iterator β’ batches are never fully loaded into RAM, enabling terabyte-scale training β’ requires tree_method="hist". | ||
dtrain.set_info(weight=w, feature_names=names) | Updates metadata (label, weight, base_margin, group/qid, feature_names/types) on an already-created DMatrix without rebuilding it. |