DMatrix is XGBoost’s native data container for training or prediction data and related metadata. It is optimized for memory efficiency and processing speed, making it the format used when working directly with XGBoost’s Native API. section 5.4.2
The Scikit-learn API accepts pandas DataFrames or NumPy arrays and converts them to DMatrix internally. This keeps pipelines and model-selection workflows straightforward while retaining XGBoost performance; the APIs mainly differ in parameters, defaults, and available options. section 5.4.2
Use DMatrix explicitly when an XGBoost component needs its native input format:
- Feed an extracted booster directly for prediction or SHAP contribution values after preprocessing the features outside a pipeline. section 7.3
- Send transformed data to a compiled XGBoost model, because compilation retains the model rather than the preprocessing pipeline. section 6.4.3
- Support efficient split finding through column blocks that scan presorted entries and enable parallel processing. section 5.4.3
Preparing data for direct booster predictions
Retrieve the booster from a fitted pipeline, apply the pipeline’s preprocessing step, preserve the resulting feature names, and wrap the transformed matrix in DMatrix. The booster can then return ordinary predictions or per-feature SHAP contributions with pred_contribs=True. section 7.3
Listing 7.19 SHAP values as an XGBoost output
from XGBoost import DMatrix
booster = model_pipeline['xgb'].get_booster() #1
Xt = model_pipeline['processing'].transform(X) #2
feature_names = (
model_pipeline['processing']
.get_feature_names_out()
) #3
Xd = DMatrix(Xt) #4
shap_values = booster.predict(Xd, pred_contribs=True) #5
preds = booster.predict(Xd) #6
This listing shows the full handoff from pipeline preprocessing to DMatrix, followed by direct booster predictions and SHAP contribution predictions. section 7.3
