Explainable AI (XAI) Intermediate 15 minutes

Shapley Values and SHAP

Attributing a prediction to the features in a way derived from game theory, with clear mathematical properties.

Your progress in the track0%

Definition of the Shapley value

$$\phi_j = \sum_{S \subseteq F \setminus \{j\}} \frac{|S|!\,(|F|-|S|-1)!}{|F|!}\Big[v(S \cup \{j\}) – v(S)\Big]$$

\(v(S)\) is the value of coalition \(S\); in SHAP it is the expected prediction when only the features in \(S\) are known.

The key property

$$f(x) = \mathbb{E}[f(X)] + \sum_{j=1}^{M} \phi_j$$

A worked example

# pip install shap xgboost
import shap, xgboost
from sklearn.datasets import fetch_california_housing
X, y = fetch_california_housing(return_X_y=True, as_frame=True)
model = xgboost.XGBRegressor(n_estimators=300, max_depth=4).fit(X, y)
explainer = shap.Explainer(model, X.sample(200, random_state=0))   # background data
sv = explainer(X.iloc[:500])
shap.plots.waterfall(sv[0])     # explaining one example
shap.plots.beeswarm(sv)         # a global picture

Types of explainers

ExplainerFor which modelsNote
TreeSHAPTrees and boosting modelsExact and fast
KernelSHAPAny modelApproximate and slow with many features
DeepSHAP / GradientSHAPNeural NetworksGradient-based approximations
LinearSHAPLinear modelsClosed form

Reading the plots

  • Waterfall: From the base value to the prediction through the contribution of each feature.
  • Beeswarm: For each feature, a dot for each example; the colour is the feature value and the position is the SHAP value.
  • Dependence: SHAP value against feature value, revealing interactions.

Warnings

  • The choice of background data and the definition of “interventional” or “conditional” change the results.
  • Correlated features share the contribution in ways that may be counter-intuitive.
  • Precise numbers do not mean certainty; compare with other methods before drawing conclusions.

Lesson terms

Background data
A reference sample used to represent the absence of a feature when computing SHAP values.
Efficiency property
The sum of the SHAP values of all features equals the difference between the example's prediction and the model's expected value.
Shapley value
The average marginal contribution of a player across all possible orders in which players join the coalition.

Abstract

Shapley values distribute the "payout" of a coalition among the players according to their average marginal contribution across all orders of joining. In SHAP the players are the features, and the payout is the difference between the example's prediction and the average prediction. They satisfy the properties of efficiency, symmetry and the null player. Exact computation is exponential, so approximations such as KernelSHAP are used, along with fast exact methods for trees such as TreeSHAP. The results depend on the background data and on how the "absence" of a feature is defined, and they are not causal explanations.

Review questions

  1. What does the efficiency property mean in practice?

    That the SHAP values of an example add up exactly to the difference between its prediction and the average prediction on the background data.

  2. Why may SHAP values change when the background data changes?

    Because the "absence" of a feature is represented by its values in the background, so changing the reference changes the contributions.

Sources and references

  1. Lundberg & Lee (2017) — A Unified Approach to Interpreting Model Predictions
  2. Lundberg et al. (2020) — From local explanations to global understanding with explainable AI for trees
  3. SHAP Documentation

Complete the lesson