Sub topic 15
Interpreting a model: importance and contributions
This chapter assumes you can train an XGBoost model with the native API and know what a DMatrix is. It also assumes you understand log loss and the idea of a tree split. Here you will learn two ways to see what a trained model relies on: global importance scores and per-prediction contributions.
Global importance: which features matter overall
Feature importance ranks features across the whole model. A Booster offers several importance types, and each one answers a different question.
| Type | What it measures |
|---|---|
| `weight` | How many times a feature is used in a split |
| `gain` | Average loss reduction from splits on the feature |
| `cover` | Average number of samples affected by those splits |
| `total_gain` | Gain summed over all splits |
Gain is usually the most useful default. Weight favors features with many possible split points, such as continuous ones, even when each split helps little.
Contributions for a single prediction
Importance scores describe the model as a whole. They do not explain why one row got its score. For that, XGBoost can return per-prediction contributions with pred_contribs=True. These are SHAP-style values: each feature gets a share of the difference between the average prediction and this row's prediction.
The code below trains a model, prints the top five features by gain, and computes contributions.
import xgboost as xgb
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer()
dtrain = xgb.DMatrix(data.data, label=data.target, feature_names=list(data.feature_names))
params = {"objective": "binary:logistic", "eta": 0.1, "max_depth": 3}
booster = xgb.train(params, dtrain, num_boost_round=50)
gain = booster.get_score(importance_type="gain")
top = sorted(gain.items(), key=lambda kv: kv[1], reverse=True)[:5]
print(top)
contribs = booster.predict(dtrain, pred_contribs=True)
print(contribs.shape) # (n_samples, n_features + 1); last column is the bias termThe contributions array has one column per feature plus a final bias column. For each row, the columns sum to the raw margin, which is the prediction before the sigmoid is applied. For binary:logistic, the values are therefore in log-odds units, not probabilities.
Common errors
- Reading importance as causation. A high-gain feature helps the model predict. It does not mean changing that feature changes the outcome.
- Trusting one importance type. Compare
weightandgain. If they disagree, look at the splits more closely. - Splitting credit among correlated features. If two features carry the same signal, the model may favor one of them, and the other can look unimportant.
- Forgetting the bias column. When you rank features from
pred_contribs, drop the last column first. - Reading contributions as probabilities. Apply a sigmoid to the summed row to get a probability.
Checking the additivity property
You can verify that contributions add up to the raw margin. This is a useful sanity check when your pipeline changes.
import numpy as np
margin = booster.predict(dtrain, output_margin=True)
print(np.allclose(contribs.sum(axis=1), margin, atol=1e-4))If this prints True, the contributions are consistent with the model output.
Exercise
A row's pred_contribs values sum to 1.5 for a binary:logistic model. What is the predicted probability, and what does the last column represent?
Show answer
The sum is the raw margin in log-odds, so the probability is 1 / (1 + exp(-1.5)), about 0.82. The last column is the bias term, the model's baseline margin before any feature contributions are added.