liveBook · Manning

DMatrix

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

An XGBoost data structure optimized for storing labeled training or prediction data and associated metadata.

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

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Explains API conversion, direct-booster preparation, and column-block storage, without detailing DMatrix construction options or metadata fields.
Examples
Demonstrates transforming pipeline data into DMatrix before computing predictions and SHAP contributions from an extracted booster.
What it assumes
Assumes familiarity with pandas or NumPy inputs, Scikit-learn pipelines, and transformed feature matrices.
What you can do after
Apply it via XGBoost’s Native API to supply transformed data to boosters and compiled predictors.
How it connects
Connects XGBoost Native API, Scikit-learn pipelines, boosters, SHAP values, histogram splitting, and Treelite compilation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.