Foundations to Frontier
Tests
D. Classical Machine Learning › D17 Interpretability for Classical Models

SHAP (Kernel SHAP, Tree SHAP)

SHAP (Lundberg and Lee, 2017) explains a prediction by sharing out the difference between it and the average prediction among the features, using Shapley values from cooperative game theory. Each feature's share is its average marginal contribution over all orders in which features could be added, which is the unique sharing rule satisfying efficiency (shares add up), symmetry, a null-player rule and additivity. Computing it exactly needs the model's value on every subset of features, so practical methods approximate: Kernel SHAP fits a specially weighted linear regression to sampled feature subsets, and Tree SHAP computes exact values for tree ensembles in polynomial time. A key choice is how a 'missing' feature is filled in: interventional SHAP replaces it with background data and stays true to the model, while conditional SHAP uses its distribution given the known features and stays true to the data. This page covers Shapley values and their axioms, the value function, Kernel SHAP, Tree SHAP, interactions, global summaries, and pitfalls with correlated features. In the exercises you compute exact Shapley values, Kernel SHAP and Tree SHAP's value function, then compare interventional and conditional SHAP for correlated twins.

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: share out the prediction among the features
E[f] = -0.14x0 (2·x0)+2.29x1 (x1·x2)-0.72x2 (x1·x2)-0.91x3 (0.5·x3)+0.24f(x) = 0.75
sum of SHAP values 0.89 = f(x) − E[f] = 0.89 · Kernel SHAP largest error 0.57
Bars start at the average prediction and add each feature's share until they reach f(x). x0's share is 2·(x0 − its average), and x3's is 0.5·(x3 − its average), exactly as for a linear model. x1 and x2 split their product between them: set x2 to 0 and x1's share almost vanishes, because x1 then does nothing. Hollow circles are Kernel SHAP estimates. With few sampled coalitions they wander; with more they settle on the exact values.
Why engineers care

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.

📝 Quick check (Level 0)
0/3 answered
  1. 1. SHAP values add up to…
  2. 2. SHAP comes from…
  3. 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).

📝 Quick check (Level 1)
0/3 answered
  1. 1. v(∅) = 0, v(A) = 4, v(B) = 4, v(AB) = 10: φ_A is…
  2. 2. Weight of the empty coalition for p = 3 is…
  3. 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
🐍 Exercise 1: exact, Kernel and Tree SHAP
Compute exact interventional Shapley values for a known model, check the axioms, approximate them with Kernel SHAP, and compute Shapley values of a tree with Tree SHAP's path-dependent value function.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Kernel SHAP with all coalitions gives…
  2. 2. Tree SHAP's value function at a split on a feature not in S…
  3. 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.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Interventional SHAP is described as…
  2. 2. Conditional SHAP gives an unused but correlated feature…
  3. 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.
🐍 Exercise 2 (research): interventional versus conditional SHAP
For two linear models that use either x0 or its correlated twin x1, compute exact interventional and conditional (Gaussian) Shapley values as the correlation grows, and compare the explanations.

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

  1. L. S. Shapley (1953). “A value for n-person games.” In Contributions to the Theory of Games II.
  2. S. M. Lundberg, S.-I. Lee (2017). “A unified approach to interpreting model predictions.” Advances in NeurIPS 30.
  3. S. M. Lundberg et al. (2020). “From local explanations to global understanding with explainable AI for trees.” Nature Machine Intelligence 2.
  4. 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.
  5. D. Janzing, L. Minorics, P. Blöbaum (2020). “Feature relevance quantification in explainable AI: a causal problem.” Proc. AISTATS.