Search

Search papers and lessons

SHAP Values

Level 4 · 17 min read

SHAP values split one prediction of any model into a share for each input feature, using the Shapley value from game theory, so the shares add up to the gap between this prediction and the average prediction.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Game theory
  • Classical models
  • ReadReady to readNot yet

TL;DR

A model turns several inputs into one prediction, and often nobody can say which input pushed the answer up and which pushed it down. SHAP values give each input a number for one prediction: how much that input moved the prediction away from the average prediction. The numbers are the Shapley values from game theory, averaged over the orders in which inputs could be added, and they always add up to the gap between this prediction and the average.

Builds on: Shapley Value, Conditional Expectation, Linear Regression · Leads to: the SHAP paper, Integrated Gradients

Where it's used

Explaining one decision to a reviewer. Suppose a team scores loan applications with a gradient-boosted model. It needs to tell an applicant or an auditor why one application got its score. A SHAP breakdown lists each fact about that applicant (income, debt, account age and so on) with a signed number. A positive number says that fact raised the score, a negative number says it lowered it. The reviewer reads the list instead of the model's internals.

Checking whether a model leans on the right things. Suppose a fraud-detection team sees that one harmless field pushes many transactions toward "fraud". Looking at the per-transaction breakdowns shows that field carries most of the push, which is a clue that it leaks information about the label or was recorded differently for fraud cases. The team can then investigate the field instead of trusting the overall accuracy number.

Ranking features across many predictions. Averaging the size of the SHAP values over a test set gives a rough overall ranking of which inputs matter most to the model. This ranking is a summary of many single-prediction explanations, so it inherits their assumptions (see the common mistakes below).

Explaining any model, not just simple ones. A linear regression can be read from its weights. A single Decision Tree, a random forest or a deep network cannot. The SHAP framework applies to all of them, which is why the authors of the paper presented it as one unified measure rather than a method for one model family.

How it works

Take a tiny model with two inputs, aa and bb:

f(a,b)=2a+3b+abf(a, b) = 2a + 3b + ab

The abab term is an interaction: the effect of aa depends on the value of bb. We want to explain one prediction, for the input x=(a=2,b=2)x = (a = 2, b = 2), where f(x)=4+6+4=14f(x) = 4 + 6 + 4 = 14.

To say what a feature "contributes", we need a reference. Use a background dataset with two rows, r1=(0,0)r_1 = (0, 0) and r2=(2,2)r_2 = (2, 2). Each row stands in for a typical input the model might see. To keep the calculation simple, we treat the features as independent when we mix values from the background with values from xx. This is the same kind of simplification the paper uses to make SHAP values computable.

  1. Start with the base value. Run the model on each background row and average the results. f(0,0)=0f(0, 0) = 0 and f(2,2)=14f(2, 2) = 14, so the base value is (0+14)/2=7(0 + 14) / 2 = 7. This is the prediction when we know nothing about the input. Check: the base value is an average of predictions, so it sits between the smallest and largest background prediction, here 0 and 14.

  2. Value a group of features. For any group SS of features, define v(S)v(S) as the average prediction when the features in SS take their values from xx and the other features take their values from each background row in turn. For the group S={a}S = \{a\}, set a=2a = 2 and let bb come from the background: f(2,0)=4f(2, 0) = 4 and f(2,2)=14f(2, 2) = 14. So v({a})=(4+14)/2=9v(\{a\}) = (4 + 14) / 2 = 9. Check: v({a})−7=2v(\{a\}) - 7 = 2, so knowing a=2a = 2 moves the average up by 2.

  3. Value the other single feature. For S={b}S = \{b\}, set b=2b = 2 and let aa come from the background: f(0,2)=6f(0, 2) = 6 and f(2,2)=14f(2, 2) = 14. So v({b})=(6+14)/2=10v(\{b\}) = (6 + 14) / 2 = 10. Check: v({b})−7=3v(\{b\}) - 7 = 3.

  4. Value both features together. For S={a,b}S = \{a, b\} nothing comes from the background, so v({a,b})=f(2,2)=14v(\{a, b\}) = f(2, 2) = 14. Check: 14−7=714 - 7 = 7. This is the total we have to split between aa and bb.

  5. Follow one order. Imagine adding the features one at a time, aa first. A feature's gain is how much its arrival raises the group's value. aa enters at a group that was empty, so its gain is v({a})−v(∅)=9−7=2v(\{a\}) - v(\emptyset) = 9 - 7 = 2. Then bb enters a group that already holds aa, so its gain is v({a,b})−v({a})=14−9=5v(\{a, b\}) - v(\{a\}) = 14 - 9 = 5. Check: 2+5=72 + 5 = 7, the total.

  6. Follow the other order. Now add bb first. bb gains v({b})−v(∅)=10−7=3v(\{b\}) - v(\emptyset) = 10 - 7 = 3, and then aa gains v({a,b})−v({b})=14−10=4v(\{a, b\}) - v(\{b\}) = 14 - 10 = 4. Check: 3+4=73 + 4 = 7, the total again. But aa got 2 in the first order and 4 in the second, so the answer depends on the order. That is caused by the abab interaction.

  7. Average over the orders. The Shapley idea is to average each feature's gain over all orders. For aa: (2+4)/2=3(2 + 4) / 2 = 3. For bb: (5+3)/2=4(5 + 3) / 2 = 4. Check: 3+4=73 + 4 = 7, which is 14−714 - 7.

  8. Read the result. The prediction 1414 equals the base value 77, plus 33 from aa, plus 44 from bb. Every feature has a signed share, and the shares add up exactly to the difference between the prediction and the base value.

Exact computation of these shares needs v(S)v(S) for every group of features, so the work grows exponentially with the number of features. The paper's approximations avoid most of that work.

flowchart LR
  A["Model f, input x, background rows"] --> B["Value v(S) for every group S of features"]
  B --> C["Gain of each feature in each order of adding them"]
  C --> D["Average the gains over all orders"]
  D --> E["One signed number per feature, summing to f(x) minus base value"]

The math

Think of the features as players in a game whose payoff is the model's prediction. The Shapley Value is the standard fair way to split a joint payoff among players, and SHAP applies it to the inputs of one prediction.

The explanation model

The paper describes an explanation of a prediction as a simpler model gg that takes binary inputs zi′∈{0,1}z'_i \in \{0, 1\}. Here zi′=1z'_i = 1 means feature ii is present (taken from xx) and zi′=0z'_i = 0 means it is absent. The explanation has the form

g(z′)=ϕ0+∑i=1Mϕizi′g(z') = \phi_0 + \sum_{i=1}^{M} \phi_i z'_i

where MM is the number of features, ϕ0\phi_0 is the base value, and ϕi\phi_i is the effect attributed to feature ii.

Step 1 in symbols: the base value

ϕ0=v(∅)=1N∑k=1Nf(rk)\phi_0 = v(\emptyset) = \frac{1}{N}\sum_{k=1}^{N} f(r_k)

Here NN is the number of background rows and rkr_k is row kk. In the toy model, ϕ0=(0+14)/2=7\phi_0 = (0 + 14) / 2 = 7. It is an expected value taken over the background.

Step 2 in symbols: the value of a group

For a set SS of features, let Sˉ\bar{S} be the features not in SS. Then

v(S)=1N∑k=1Nf(xS,  rk,Sˉ)v(S) = \frac{1}{N}\sum_{k=1}^{N} f\bigl(x_S,\; r_{k,\bar{S}}\bigr)

where xSx_S means the features in SS take their values from xx, and rk,Sˉr_{k,\bar{S}} means the other features take their values from background row kk. This is the step that stood for "value a group" in the walkthrough. In the toy model, v({a})=9v(\{a\}) = 9.

Step 3 in symbols: the Shapley weights

The SHAP value of feature ii is

ϕi=∑S⊆F∖{i}∣S∣! (M−∣S∣−1)!M! [v(S∪{i})−v(S)]\phi_i = \sum_{S \subseteq F \setminus \{i\}} \frac{|S|!\,(M-|S|-1)!}{M!}\,\bigl[v(S \cup \{i\}) - v(S)\bigr]

where FF is the set of all features. The bracket is the gain from adding ii to a group SS. The weight counts how many orders put exactly the features of SS before ii: there are ∣S∣!|S|! orders of SS and (M−∣S∣−1)!(M-|S|-1)! orders of the rest, out of M!M! orders in total. For M=2M = 2 both weights are 12\tfrac{1}{2}, which is why the walkthrough took a plain average.

Written out for the toy model:

ϕa=12[v({a})−v(∅)]+12[v({a,b})−v({b})]=12(2)+12(4)=3\phi_a = \tfrac{1}{2}\bigl[v(\{a\}) - v(\emptyset)\bigr] + \tfrac{1}{2}\bigl[v(\{a, b\}) - v(\{b\})\bigr] = \tfrac{1}{2}(2) + \tfrac{1}{2}(4) = 3 ϕb=12[v({b})−v(∅)]+12[v({a,b})−v({a})]=12(3)+12(5)=4\phi_b = \tfrac{1}{2}\bigl[v(\{b\}) - v(\emptyset)\bigr] + \tfrac{1}{2}\bigl[v(\{a, b\}) - v(\{a\})\bigr] = \tfrac{1}{2}(3) + \tfrac{1}{2}(5) = 4

These match the orders in steps 5 to 7 of the walkthrough.

Step 4 in symbols: the shares add up

ϕ0+∑i=1Mϕi=f(x)\phi_0 + \sum_{i=1}^{M} \phi_i = f(x)

This is the local accuracy property. In the toy model, 7+3+4=14=f(x)7 + 3 + 4 = 14 = f(x).

Why these values, and not others

The paper asks which attributions satisfy three properties:

  • Local accuracy: the explanation adds up to the prediction, as in step 4.
  • Missingness: a feature absent from the input (zi′=0z'_i = 0) gets ϕi=0\phi_i = 0.
  • Consistency: if a change to the model makes a feature's contribution larger or the same in every case, its attribution should not shrink.

The paper's main theorem says there is exactly one additive attribution that satisfies all three, and it is the Shapley value formula above. That is the sense in which SHAP is the unique answer within this class.

The approximation the paper uses

The formula needs v(S)v(S) for groups of features, but a trained model cannot easily be run with some features "missing". The paper handles this by defining the value through a conditional expectation E[f(z)∣zS]\mathbb{E}[f(z) \mid z_S], then approximates it by averaging over the other features. When features are treated as independent, the approximation becomes the background average used in the walkthrough. When the model is linear, it simplifies further.

For a linear model f(x)=∑jwjxj+bf(x) = \sum_j w_j x_j + b with independent features, the paper's Linear SHAP result is

ϕi=wi (xi−E[xi]),ϕ0=b\phi_i = w_i\,(x_i - \mathbb{E}[x_i]), \qquad \phi_0 = b

This is why linear models are the easy case: each feature's share is its weight times how far its value is from the average value. We use it in the third worked example.

The paper also gives a weighting for a fast estimator, Kernel SHAP, that turns the problem into a weighted linear regression:

π(z′)=M−1(M∣z′∣) ∣z′∣ (M−∣z′∣)\pi(z') = \frac{M-1}{\binom{M}{|z'|}\,|z'|\,(M-|z'|)}

Here ∣z′∣|z'| is the number of present features. The weight is infinite for the empty and full groups, which is how the regression enforces the base value and the sum property. Its details are in the paper, and we do not need them to use the idea.

Worked examples

Example
Example 1: one feature, by hand

A model is f(x)=3xf(x) = 3x. The background values of xx are 00 and 22, equally likely. The input is x=2x = 2.

  1. Base value: the background predictions are f(0)=0f(0) = 0 and f(2)=6f(2) = 6. Their average is (0+6)/2=3(0 + 6) / 2 = 3.
  2. The prediction for the input is f(2)=6f(2) = 6.
  3. The gap to explain is 6−3=36 - 3 = 3.
  4. There is only one feature, so it receives the whole gap: its SHAP value is 33.
  5. Check: the base value plus the SHAP value is 3+3=6=f(2)3 + 3 = 6 = f(2).

Interpretation: the input x=2x = 2 pushes the prediction 3 units above the average prediction, and that push belongs entirely to xx.

Example
Example 2: two features with an interaction

Use the model f(a,b)=2a+3b+abf(a, b) = 2a + 3b + ab, the background rows (0,0)(0, 0) and (2,2)(2, 2), and the input x=(a=0,b=2)x = (a = 0, b = 2). Here f(x)=0+6+0=6f(x) = 0 + 6 + 0 = 6.

  1. Base value: f(0,0)=0f(0, 0) = 0 and f(2,2)=14f(2, 2) = 14. Average =7= 7.
  2. v({a})v(\{a\}): set a=0a = 0, let bb come from the background. f(0,0)=0f(0, 0) = 0 and f(0,2)=6f(0, 2) = 6. Average =3= 3.
  3. v({b})v(\{b\}): set b=2b = 2, let aa come from the background. f(0,2)=6f(0, 2) = 6 and f(2,2)=14f(2, 2) = 14. Average =10= 10.
  4. v({a,b})=f(0,2)=6v(\{a, b\}) = f(0, 2) = 6.
  5. Gains for aa: v({a})−v(∅)=3−7=−4v(\{a\}) - v(\emptyset) = 3 - 7 = -4 when aa enters first, and v({a,b})−v({b})=6−10=−4v(\{a, b\}) - v(\{b\}) = 6 - 10 = -4 when bb enters first. Both are −4-4, so the average is −4-4.
  6. Gains for bb: v({b})−v(∅)=10−7=3v(\{b\}) - v(\emptyset) = 10 - 7 = 3 when bb enters first, and v({a,b})−v({a})=6−3=3v(\{a, b\}) - v(\{a\}) = 6 - 3 = 3 when aa enters first. Both are 33, so the average is 33.
  7. Check: base plus shares is 7+(−4)+3=6=f(x)7 + (-4) + 3 = 6 = f(x).

Interpretation: this input is lower than average, and the feature a=0a = 0 is the reason. Its share is −4-4, a large negative push. Notice that here the order did not change the answer, because aa and bb happen to have the same gain in both orders for this input.

Example
Example 3: a realistic linear model

This is a made-up delivery-time model, so the numbers are illustrative. A courier service predicts the delivery time in minutes from three inputs with a linear model:

f(d,n,w)=12+0.8 d+3 n+2 wf(d, n, w) = 12 + 0.8\,d + 3\,n + 2\,w

where dd is the distance in kilometres, nn is the number of items, and ww is 1 if it is raining and 0 otherwise. Background averages from past orders are E[d]=4\mathbb{E}[d] = 4, E[n]=2\mathbb{E}[n] = 2, and E[w]=0.5\mathbb{E}[w] = 0.5. The input is d=6d = 6, n=5n = 5, w=1w = 1.

  1. Predicted time for the input: 12+0.8(6)+3(5)+2(1)=12+4.8+15+2=33.812 + 0.8(6) + 3(5) + 2(1) = 12 + 4.8 + 15 + 2 = 33.8 minutes.
  2. Base value: 12+0.8(4)+3(2)+2(0.5)=12+3.2+6+1=22.212 + 0.8(4) + 3(2) + 2(0.5) = 12 + 3.2 + 6 + 1 = 22.2 minutes.
  3. Gap to explain: 33.8−22.2=11.633.8 - 22.2 = 11.6 minutes.
  4. Distance share: 0.8 (6−4)=1.60.8\,(6 - 4) = 1.6.
  5. Items share: 3 (5−2)=93\,(5 - 2) = 9.
  6. Rain share: 2 (1−0.5)=12\,(1 - 0.5) = 1.
  7. Check: 1.6+9+1=11.61.6 + 9 + 1 = 11.6, which matches the gap.

Interpretation: most of the extra time comes from the five items, not from the distance, and rain adds one minute on top.

Try it. Keep the same background and the same model, but change the input to n=1n = 1 items, with d=6d = 6 and w=1w = 1 as before. What is the SHAP value for the number of items? Then check that the three shares add to the gap between the new prediction and the base value.

Answer: The items share is 3 (1−2)=−33\,(1 - 2) = -3. The new prediction is 12+4.8+3+2=21.812 + 4.8 + 3 + 2 = 21.8 minutes, so the gap is 21.8−22.2=−0.421.8 - 22.2 = -0.4. The shares are 1.6+1−3=−0.41.6 + 1 - 3 = -0.4, which matches.

Common mistakes and tradeoffs

It is tempting to think a SHAP value is the cause of the prediction. In fact it describes how the model's output responds to each input, relative to a background, averaged over orders. Because the model may use a feature as a proxy for something else, a large SHAP value does not show that changing the feature would change the real outcome.

It is tempting to think the base value is zero. In fact it is the average prediction over the background, and every SHAP value is measured from that point. A prediction of 5 with base value 7 is explained by shares that sum to −2-2, not to 55.

It is tempting to think the choice of background does not matter. In fact the background sets the base value and every group value v(S)v(S). A different background, such as a population with different typical values, gives different shares for the same input.

It is tempting to think correlated features get clean credit. In fact the approximation assumes independence when it mixes background values with input values. Two correlated features can then be credited using combinations of values that the data never shows together. The paper's Figure 1 notes that when the model is non-linear or features are not independent, the order of adding features matters, and SHAP values come from averaging over all orders. The averaged values still rest on the independence approximation.

It is tempting to think a large average ∣ϕi∣|\phi_i| means the model is right. In fact it only means the model relies on that feature. A reliable model and an unreliable one can both rely heavily on the same input.

When to use it. Use SHAP when you need a signed, per-prediction breakdown for a model you cannot read directly, and when you have a reasonable background dataset.

When not to use it. Do not use it to claim causal effects, and do not treat it as a substitute for checking the model on new data.

Alternatives and variants. For a linear model, the weights already say most of this, and Linear SHAP reduces to weight times deviation. For differentiable networks, Integrated Gradients is a related attribution that follows a straight path from a baseline to the input. Kernel SHAP is model-agnostic and estimates the values with a weighted regression, needing fewer model evaluations than sampling the orders directly. Deep SHAP combines the values for small network components into values for the whole network.

History

Lloyd Shapley defined a value for cooperative games: a rule that splits a joint payoff among players by averaging each player's contribution over the orders in which they could join. For decades it was a tool of economics and game theory.

Explaining a model's single prediction became a separate research problem as complex models spread. Methods such as LIME, which the paper cites, explained one prediction by fitting a simple model around it. Many such methods existed, and it was hard to say how they related or which to trust.

In 2017 Scott Lundberg and Su-In Lee showed that, within the class of additive feature attribution methods, exactly one attribution satisfies local accuracy, missingness and consistency, and that it is the Shapley value. They named these values SHAP and proposed approximations that made them practical. The paper links an open-source implementation.

Recap

  • A prediction differs from the base value (the average prediction over a background) by a gap, and that gap is what we explain.
  • For any group of features, the value v(S)v(S) is the average prediction when those features come from the input and the rest come from the background.
  • A feature's gain depends on the order it is added in, so we average its gain over all orders. The average is its Shapley value.
  • The SHAP values of one prediction add up exactly to the gap.
  • Exact computation is exponential in the number of features, so the paper's approximations (Kernel SHAP, Linear SHAP, Deep SHAP) are what make the idea usable.

Read next: A Unified Approach to Interpreting Model Predictions for the derivation, the properties and the approximations. For an alternative attribution for differentiable models, read Integrated Gradients.

Check yourself

QuestionFor one prediction, what do the SHAP values add up to, and what is that number in words?Show answer

They add up to the prediction minus the base value: f(x)−ϕ0f(x) - \phi_0. In words, they account for the whole gap between this prediction and the average prediction over the background.

QuestionIn the delivery model, the background averages are E[d]=4\mathbb{E}[d] = 4, E[n]=2\mathbb{E}[n] = 2, E[w]=0.5\mathbb{E}[w] = 0.5. For an input with d=10d = 10, n=2n = 2, w=0w = 0, what is the SHAP value of rain?Show answer

The rain coefficient is 2, so the share is 2 (w−E[w])=2 (0−0.5)=−12\,(w - \mathbb{E}[w]) = 2\,(0 - 0.5) = -1. Rain is absent here and the average is 0.5, so it pulls the prediction down by one minute.

QuestionWhy does SHAP average each feature's gain over all orders of adding features, instead of picking one natural order such as the order the columns appear in the data?Show answer

Because a feature's gain can depend on which features were already added, as the abab interaction showed: aa gained 2 in one order and 4 in the other. Any single order would favour some features over others. The average over all orders treats the features symmetrically and is the unique fair split that the Shapley axioms require.

QuestionA reviewer sees that the account-age feature has a SHAP value of +0.25+0.25 for a loan application that was rejected. Can the reviewer conclude that account age caused the rejection?Show answer

No. The SHAP value says that, in this model, account age pushed the score up relative to the background, which is a statement about the model's behaviour. The model may use account age as a proxy for something else, and a causal conclusion needs evidence beyond the model's explanation.

Sources