Sub topic 1
From game theory to feature attribution: Shapley values
Prerequisites
This chapter assumes you can train and call a scikit-learn style model in Python. It also assumes basic NumPy and pandas use, and a rough idea of what feature importance means. You do not need to know any game theory.
The attribution problem
A model returns one number for a row of data, such as a predicted price. You often want to know how much each feature pushed that number up or down. A good answer should split the prediction fairly. It should also add up exactly, with no leftover.
SHAP does this with Shapley values. A Shapley value is a rule from cooperative game theory for dividing a group's payout among its players according to what each player contributed.
The game analogy
Map the model onto a game:
| Game idea | Model idea |
|---|---|
| Player | A feature |
| Coalition | A subset of features whose values are "present" |
| Payout of a coalition | The model output when only those features use the real row, and the rest are replaced by baseline values |
| Fair share of a player | The SHAP value of that feature |
A feature has a different effect depending on which other features are already present. So the method considers every possible order in which features could join.
The definition
For feature i, take every subset S of the other features. Measure how much the output changes when i is added to S. Then average those changes, weighting each subset by how often it occurs among all orderings:
phi_i = sum over S of [ |S|! (n - |S| - 1)! / n! ] * ( v(S + i) - v(S) )
Here n is the number of features and v(S) is the model output with only the features in S taken from the real row. Features outside S are replaced with baseline values. The next chapters cover how that replacement is done.
Shapley values have a property that matters in practice. They sum to the difference between the prediction and the baseline prediction. This is called local accuracy. If features cannot be told apart by their effect, they get equal values. A feature that never changes the output gets zero.
A worked example by brute force
The code below computes exact Shapley values for a tiny function. It tries every subset of the other features for each feature. This takes time that grows exponentially with the number of features, so it only works for very small n.
from itertools import combinations
from math import factorial
def shapley_values(f, x, baseline):
n = len(x)
def value(subset):
z = [x[i] if i in subset else baseline[i] for i in range(n)]
return f(z)
phi = []
for i in range(n):
others = [j for j in range(n) if j != i]
total = 0.0
for size in range(n):
for subset in combinations(others, size):
s = set(subset)
weight = factorial(len(s)) * factorial(n - len(s) - 1) / factorial(n)
total += weight * (value(s | {i}) - value(s))
phi.append(total)
return phi
f = lambda z: 2 * z[0] + z[1] * z[2]
x = [1, 2, 3]
baseline = [0, 0, 0]
phi = shapley_values(f, x, baseline)
print(phi) # [2.0, 3.0, 3.0]
print(sum(phi), f(x) - f(baseline)) # 8.0 8.0Read the result as follows:
- Feature 0 enters only through the term 2 * z[0], so it gets 2.0.
- The product z[1] * z[2] equals 6 and needs both features. Neither can produce it alone. The method splits it equally, 3.0 each.
- The values sum to 8.0, which is f(x) minus f(baseline).
In the diagram, whichever feature joins second receives the whole gain of 6. Averaging the two orderings gives each feature 3.
Common errors
- Treating a SHAP value as the change in prediction when you delete the feature from the data. It is measured against a baseline, not against a retrained model.
- Forgetting that the values depend on the baseline. A different baseline gives different values.
- Expecting values for different rows to be comparable without noting that each row is explained relative to the same baseline.
Exercise
In the example above, change f to lambda z: 2 * z[0] + z[1] * z[2] with x = [1, 2, 3] and baseline = [0, 0, 3]. Without running the code, what is f(x) - f(baseline), and what do the three Shapley values add up to?
Show answer
f(x) = 2 + 6 = 8 and f(baseline) = 0 + 0 = 0, so the difference is 8. Local accuracy means the three Shapley values also sum to 8. Feature 2 has the same value in x and in the baseline, so replacing it changes nothing and its Shapley value is 0. Feature 0 still gets 2. Feature 1 gets the remaining 6, because with z[2] fixed at 3 its term is 3 * z[1].