Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 40 ยท Feature Importance and SHAP

SHAP Values for Tree Models

Understand SHAP values as fair per-prediction contributions, verify their additivity with pred_contribs, and use shap.TreeExplainer for local and global explanations.

Importance scores tell you how much a model uses a feature overall. They do not tell you why the model gave this customer a score of 0.87, or whether a high income pushes predictions up or down. SHAP (SHapley Additive exPlanations) answers both questions. For every single prediction it splits the difference between the model's output and the average output into one contribution per feature, and the contributions add up exactly.

For tree ensembles there is a fast exact algorithm (TreeSHAP), built into XGBoost, LightGBM and the shap package. This lesson shows what the numbers mean, proves the additivity property in code, and builds global explanations out of local ones.

The idea in one picture

A SHAP explanation for one row has this shape:

Text
prediction (raw margin) = base value + shap(f1) + shap(f2) + ... + shap(fn)

The base value is the average raw model output over the data. Each SHAP value is positive if the feature pushed this prediction above the average and negative if it pushed it below. For a classifier the sum is in raw margin (log-odds) units, not probabilities, because that is the space where the trees add up.

flowchart LR
  A["Base value<br/>average output"] --> B["+ age: +0.4"]
  B --> C["+ income: -0.9"]
  C --> D["+ tenure: +1.3"]
  D --> E["Final raw prediction"]

Contributions are added one feature at a time, in a way that is fair to every ordering of the features.

Shapley values come from game theory. Each feature is a "player", the prediction is the "payout", and the value of a feature is its average marginal contribution over all orders in which the features could be added. TreeSHAP computes this exactly in polynomial time by walking the trees, so you do not need to sample.

Free SHAP values from XGBoost

XGBoost can return SHAP values directly with pred_contribs=True, no extra package needed. The result has one extra column, the last one, holding the base value:

Python pred_contribs and the additivity property

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The reconstruction error is at floating-point level, which is the additivity (local accuracy) property. If a library ever gives you contributions that do not add up to the prediction, you are not looking at SHAP values.

From local to global

Averaging the absolute SHAP values over many rows gives a global importance that, unlike gain, is measured on the data you choose and comes with a direction for every row:

Python Mean |SHAP| versus the effect direction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A positive correlation between a feature's value and its SHAP value means "higher values push the prediction up", as for income. A negative one, as for debt, means the opposite. The noise feature has a small mean |SHAP| and no clear direction. Correlation is only a rough summary of a possibly non-linear effect; plots like the dependence plot below show the full shape.

The shap package

The shap library wraps the same algorithm and adds plots and many conveniences (pip install shap). Its entry point for trees is shap.TreeExplainer:

Python shap.TreeExplainer on an XGBoost model

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On your own machine, plotting functions turn these numbers into pictures. They need matplotlib and are covered in the next lessons: shap.plots.bar(values) for global importance, shap.plots.beeswarm(values) for the distribution of effects, shap.plots.waterfall(values[0]) for one explanation and shap.plots.scatter(values[:, "x"]) for a dependence plot.

What SHAP does and does not mean

  • Explains the model, not the world. A large SHAP value shows what the model used. It does not prove that changing the feature changes the real-world outcome.
  • Units. For binary classification the default values are in log-odds. A SHAP value of +1 on log-odds does not mean "+1 percentage point".
  • Correlated features share credit, but unlike permutation importance, the total contribution of the group is preserved.
  • Background data matters for model-agnostic explainers (KernelExplainer). TreeSHAP's default ("tree path dependent") uses the training cover stored in the trees; the "interventional" variant uses a background dataset.
  • Cost. TreeSHAP is fast, but computing it for millions of rows is still a batch job; compute it on a sample.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does additivity of SHAP values mean?

    Local accuracy: for each row, base value plus the sum of feature contributions reproduces the raw model output.
  2. 2. In which units are SHAP values for a binary XGBoost classifier by default?

    Trees add up in margin space, so contributions are in log-odds for the logistic objective.
  3. 3. How do you build a global importance from SHAP values?

    Positive and negative effects cancel in a plain mean; the mean absolute value measures average impact.
  4. 4. Which pred_contribs column holds the base value in XGBoost?

    The output has n_features + 1 columns and the final column is the bias (base value).

Summary

  • SHAP splits each prediction into a base value plus one contribution per feature, and the parts add up exactly.
  • TreeSHAP is exact and fast for XGBoost and LightGBM; XGBoost exposes it as pred_contribs=True.
  • Mean absolute SHAP gives a global importance; the sign relation between value and contribution gives direction.
  • Values are in log-odds for classifiers and describe the model, not causes.

Next: Dependence, Interactions and Monotone Constraints