Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Splitting the bill fairly
Three colleagues share a taxi with different destinations. How should they split the fare? Shapley’s answer: imagine them joining the ride in every possible order, see how much each person adds to the cost when they join, and average over all orders. SHAP splits a model’s prediction the same way. The “fare” is the difference between this prediction and the average prediction, and the passengers are the features.
SHAP is the most widely used explanation method in industry and research. It produces per-prediction attributions that add up exactly, global importance summaries, and dependence plots, for any model, and very quickly for tree ensembles. Knowing what its numbers mean, and what choice sits behind “missing features”, is essential before using them in a design review or an incident report.
- 1. SHAP values add up to…
- 2. SHAP comes from…
- 3. A feature the model never uses gets…
Level 1: Beginner
★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.Shapley values by hand
Value function. For a subset S of features, v(S) is the model’s expected prediction when only the features in S are known.
Shapley value. φⱼ = the sum, over subsets S of the other features, of [|S|!(p − |S| − 1)!/p!] × [v(S + j) − v(S)]. This is the average, over all orders of adding features, of the change j causes when it joins.
Two players. Suppose v(∅) = 0, v(0) = 10, v(1) = 20 and v(0, 1) = 60.
- φ₀ = ½[(10 − 0) + (60 − 20)] = 25.
- φ₁ = ½[(20 − 0) + (60 − 10)] = 35.
They sum to 60. The extra 30 from working together is split equally.
Weights. For p = 3, coalitions of size 0, 1 and 2 (not containing j) get weights 1/3, 1/6 and 1/3. All orders count equally.
Axioms (they make Shapley values unique):
- efficiency: Σφⱼ = v(F) − v(∅);
- symmetry: equal contributions give equal shares;
- null player: a feature that never changes v gets 0;
- additivity: for a sum of two models, the values add.
Linear model. With independent background data, f = b + w·x gives φⱼ = wⱼ(xⱼ − E[xⱼ]). For w = (2, 0, 0.5), x = (1, 3, 0.4) and mean (0.1, 0, 0.2), φ = (1.8, 0, 0.1).
- 1. v(∅) = 0, v(A) = 4, v(B) = 4, v(AB) = 10: φ_A is…
- 2. Weight of the empty coalition for p = 3 is…
- 3. For a linear model, φⱼ equals…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.Exact values, Kernel SHAP and Tree SHAP
Interventional value function. v(S) = average of f(x_S, x′₋S) over background rows x′: the features in S are set to the instance’s values and the rest come from real data.
Kernel SHAP. Sample coalitions S. Regress v(S) − v(∅) on the indicator vector z_S, with the Shapley kernel weight (p − 1)/[C(p, |S|) |S| (p − |S|)], under the constraint Σφ = v(F) − v(∅). With all 2ᵖ coalitions, the solution equals the exact Shapley values (Lundberg and Lee, 2017). For p = 4 the weight is 0.25 for a coalition of size 1 and 0.125 for size 2: the most informative coalitions (very small or very large) count most.
Tree SHAP’s value function. For a tree, v(S) can be computed by one pass: at a split on a feature in S, follow the instance’s branch; at other splits, average both branches, weighted by the share of training samples that went each way (the “cover”). Lundberg et al. (2020) turned this into an exact polynomial-time algorithm, O(TLD²) for T trees, L leaves and depth D, instead of 2ᵖ evaluations.
Exercise 1.
- Exact Shapley values for f = 2x₀ + x₁x₂ + 0.5x₄ (x₃ unused), at x = (1, 2, −1, 3, 0.4) with 200 background rows: φ = (2.241, −1.106, −1.029, 0.000, 0.136).
- They sum to f(x) − E f = 0.241.
- φ₀ = 2(x₀ − mean) and φ₄ = 0.5(x₄ − mean), exactly.
- The interaction (x₁x₂ = −2) was shared almost equally between x₁ and x₂ (−1.106 and −1.029), and the unused x₃ got 0.
- Kernel SHAP, largest error against exact (mean over 5 seeds): 0.835 with 10 sampled coalitions, 0.331 with 40, 0.125 with 200.
- Tree SHAP’s value function on a depth-4 tree gave (1.442, 0.536, 0.000), which sums to the prediction 2.382 minus the training mean 0.404.
import shap
explainer = shap.TreeExplainer(forest) # exact, fast for trees
sv = explainer(X_test); shap.plots.waterfall(sv[0]); shap.plots.beeswarm(sv)
shap.KernelExplainer(model.predict, shap.sample(X_train, 100)).shap_values(x) # any model- 1. Kernel SHAP with all coalitions gives…
- 2. Tree SHAP's value function at a split on a feature not in S…
- 3. In exercise 1, the x₁x₂ interaction was…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.What “missing” means, and other pitfalls
Interventional versus conditional.
- Interventional (marginal) SHAP fills missing features from background data independently of the known ones: v(S) = E[f(x_S, X₋S)]. It is “true to the model”: a feature the model does not use gets 0. But it evaluates unrealistic combinations, as PDP and LIME do.
- Conditional (observational) SHAP uses v(S) = E[f(X) | X_S = x_S]. It is “true to the data”: features correlated with used ones receive credit, even if the model ignores them. It is also hard to estimate (Chen, Janizek, Lundberg and Lee, 2020; Janzing, Minorics and Blöbaum, 2020).
Tree SHAP’s default (path-dependent) approximates the conditional expectation by tree covers, and lies between the two. TreeExplainer with a background dataset uses the interventional version.
Other pitfalls.
- Shapley values are attributions relative to a baseline (the background average), so changing the background changes them.
- They are not causal effects.
- Interaction effects are split between features. SHAP interaction values, which assign one interaction value to each pair of features, separate them.
- Global “mean |SHAP|” importances inherit all of the above.
Interview questions.
- Why do SHAP values for a feature change when I change the background data? Because v(∅) and every v(S) are averages over the background. The baseline defines what “missing” means.
- Two features are 0.99 correlated and the model uses only one. What will interventional and conditional SHAP say? Interventional gives the unused one 0. Conditional splits the credit between them.
- 1. Interventional SHAP is described as…
- 2. Conditional SHAP gives an unused but correlated feature…
- 3. SHAP interaction values…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Twins that the model treats differently
History.
- L. Shapley (1953) introduced the Shapley value.
- E. Štrumbelj and I. Kononenko (2010, 2014) applied it to explaining predictions.
- S. Lundberg and S.-I. Lee (2017) introduced SHAP and Kernel SHAP.
- S. Lundberg et al. (2020) introduced Tree SHAP and global explanations for trees.
- H. Chen, J. Janizek, S. Lundberg and S.-I. Lee (2020) contrasted “true to the model” with “true to the data”.
Exercise 2. Two linear models on standard-normal features with corr(x₀, x₁) = ρ:
- “uses x₀”: f = x₀ + 0.5x₂;
- “uses x₁”: f = x₁ + 0.5x₂.
The table gives mean |φ| over 60 points, exact, for interventional and conditional (Gaussian) value functions.
| ρ | model | corr of the two models’ predictions | interventional (x₀, x₁, x₂) | conditional (x₀, x₁, x₂) |
|---|---|---|---|---|
| 0 | uses x₀ | 0.209 | (0.91, 0.00, 0.40) | (0.91, 0.00, 0.39) |
| 0 | uses x₁ | 0.209 | (0.00, 0.67, 0.40) | (0.00, 0.66, 0.39) |
| 0.9 | uses x₀ | 0.920 | (0.91, 0.00, 0.34) | (0.58, 0.40, 0.33) |
| 0.9 | uses x₁ | 0.920 | (0.00, 0.90, 0.34) | (0.41, 0.53, 0.33) |
| 0.99 | uses x₀ | 0.992 | (0.91, 0.00, 0.34) | (0.47, 0.45, 0.33) |
| 0.99 | uses x₁ | 0.992 | (0.00, 0.91, 0.34) | (0.45, 0.46, 0.33) |
- At ρ = 0.99, the two models predict almost identically (correlation 0.992), yet interventional SHAP explains them in opposite ways: all of the twin credit goes to the feature each model happens to use. This is correct about the models, and unstable with respect to the data, since retraining could flip which twin is used.
- Conditional SHAP gave the twins nearly equal credit in both models, and nearly identical explanations for the two near-identical models. But it gave credit to a feature the model never reads.
- At ρ = 0 the two value functions agreed.
- The ρ = 0.5 rows are in the full output.
- Neither is wrong. They answer different questions, so state which one you report.
SHAP in AI, robotics and aerospace
- Case explanations: waterfall plots showing why a model predicted a short remaining life for one engine.
- Fleet-level insight: beeswarm summaries of SHAP values across thousands of flights, showing which parameters drive risk and in which direction.
- Model debugging: SHAP dependence plots reveal leakage (a timestamp or ID column with large SHAP values) and unexpected interactions.
- Report the choice: interventional or conditional, and which background, especially for correlated flight parameters.
References
- L. S. Shapley (1953). “A value for n-person games.” In Contributions to the Theory of Games II.
- S. M. Lundberg, S.-I. Lee (2017). “A unified approach to interpreting model predictions.” Advances in NeurIPS 30.
- S. M. Lundberg et al. (2020). “From local explanations to global understanding with explainable AI for trees.” Nature Machine Intelligence 2.
- H. Chen, J. D. Janizek, S. Lundberg, S.-I. Lee (2020). “True to the model or true to the data?” ICML Workshop on Human Interpretability.
- D. Janzing, L. Minorics, P. Blöbaum (2020). “Feature relevance quantification in explainable AI: a causal problem.” Proc. AISTATS.