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, and :
The term is an interaction: the effect of depends on the value of . We want to explain one prediction, for the input , where .
To say what a feature "contributes", we need a reference. Use a background dataset with two rows, and . 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 . This is the same kind of simplification the paper uses to make SHAP values computable.
-
Start with the base value. Run the model on each background row and average the results. and , so the base value is . 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.
-
Value a group of features. For any group of features, define as the average prediction when the features in take their values from and the other features take their values from each background row in turn. For the group , set and let come from the background: and . So . Check: , so knowing moves the average up by 2.
-
Value the other single feature. For , set and let come from the background: and . So . Check: .
-
Value both features together. For nothing comes from the background, so . Check: . This is the total we have to split between and .
-
Follow one order. Imagine adding the features one at a time, first. A feature's gain is how much its arrival raises the group's value. enters at a group that was empty, so its gain is . Then enters a group that already holds , so its gain is . Check: , the total.
-
Follow the other order. Now add first. gains , and then gains . Check: , the total again. But got 2 in the first order and 4 in the second, so the answer depends on the order. That is caused by the interaction.
-
Average over the orders. The Shapley idea is to average each feature's gain over all orders. For : . For : . Check: , which is .
-
Read the result. The prediction equals the base value , plus from , plus from . 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 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 that takes binary inputs . Here means feature is present (taken from ) and means it is absent. The explanation has the form
where is the number of features, is the base value, and is the effect attributed to feature .
Step 1 in symbols: the base value
Here is the number of background rows and is row . In the toy model, . It is an expected value taken over the background.
Step 2 in symbols: the value of a group
For a set of features, let be the features not in . Then
where means the features in take their values from , and means the other features take their values from background row . This is the step that stood for "value a group" in the walkthrough. In the toy model, .
Step 3 in symbols: the Shapley weights
The SHAP value of feature is
where is the set of all features. The bracket is the gain from adding to a group . The weight counts how many orders put exactly the features of before : there are orders of and orders of the rest, out of orders in total. For both weights are , which is why the walkthrough took a plain average.
Written out for the toy model:
These match the orders in steps 5 to 7 of the walkthrough.
Step 4 in symbols: the shares add up
This is the local accuracy property. In the toy model, .
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 () gets .
- 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 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 , 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 with independent features, the paper's Linear SHAP result is
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:
Here 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
A model is . The background values of are and , equally likely. The input is .
- Base value: the background predictions are and . Their average is .
- The prediction for the input is .
- The gap to explain is .
- There is only one feature, so it receives the whole gap: its SHAP value is .
- Check: the base value plus the SHAP value is .
Interpretation: the input pushes the prediction 3 units above the average prediction, and that push belongs entirely to .
Use the model , the background rows and , and the input . Here .
- Base value: and . Average .
- : set , let come from the background. and . Average .
- : set , let come from the background. and . Average .
- .
- Gains for : when enters first, and when enters first. Both are , so the average is .
- Gains for : when enters first, and when enters first. Both are , so the average is .
- Check: base plus shares is .
Interpretation: this input is lower than average, and the feature is the reason. Its share is , a large negative push. Notice that here the order did not change the answer, because and happen to have the same gain in both orders for this input.
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:
where is the distance in kilometres, is the number of items, and is 1 if it is raining and 0 otherwise. Background averages from past orders are , , and . The input is , , .
- Predicted time for the input: minutes.
- Base value: minutes.
- Gap to explain: minutes.
- Distance share: .
- Items share: .
- Rain share: .
- Check: , 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 items, with and 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 . The new prediction is minutes, so the gap is . The shares are , 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 , not to .
It is tempting to think the choice of background does not matter. In fact the background sets the base value and every group value . 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 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 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: . 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 , , . For an input with , , , what is the SHAP value of rain?Show answer
The rain coefficient is 2, so the share is . 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 interaction showed: 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 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
- Lundberg and Lee, A Unified Approach to Interpreting Model Predictions, 2017.
- Wikipedia, Shapley value.