Sub topic 13
Feature interactions with SHAP interaction values
This chapter assumes you can compute SHAP values for a tree model and know that a prediction equals the baseline plus the sum of its SHAP values. A SHAP value gives one number per feature. When two features act together, that number mixes their separate effects with their joint effect. SHAP interaction values separate the two.
From a vector to a matrix
For one row, ordinary SHAP values form a vector of length p. Interaction values form a p x p matrix:
- The diagonal entry
[i, i]is the main effect of featurei, with all interactions removed. - The off-diagonal entry
[i, j]is half of the interaction between featuresiandj. The matrix is symmetric, so[j, i]holds the other half. - Each row sums to the ordinary SHAP value of that feature.
Recall the brute-force example, where f(z) = 2*z[0] + z[1]*z[2] and x = [1, 2, 3]. The product term contributes 6. Ordinary SHAP gives features 1 and 2 a value of 3 each. Interaction values show where those 3s come from: the main effects of features 1 and 2 are 0, and the entries [1, 2] and [2, 1] are 3 each.
Computing them
Only TreeExplainer provides interaction values. The cost grows roughly with the square of the number of features, so use a subset of rows.
import numpy as np
import shap
from sklearn.datasets import load_diabetes
from sklearn.ensemble import GradientBoostingRegressor
X, y = load_diabetes(return_X_y=True, as_frame=True)
model = GradientBoostingRegressor(random_state=0).fit(X, y)
explainer = shap.TreeExplainer(model)
Xs = X.iloc[:200]
inter = explainer.shap_interaction_values(Xs)
print(inter.shape) # (200, 10, 10)
# Row sums reproduce the ordinary SHAP values.
print(np.allclose(inter.sum(axis=2), explainer.shap_values(Xs), atol=1e-4))
# The whole matrix plus the baseline reproduces the prediction.
pred = explainer.expected_value + inter[0].sum()
print(np.isclose(pred, model.predict(Xs.iloc[[0]])[0], atol=1e-4))Finding the strongest pair
Average the absolute values over rows, then ignore the diagonal.
strength = np.abs(inter).mean(axis=0)
np.fill_diagonal(strength, 0)
i, j = np.unravel_index(strength.argmax(), strength.shape)
print(X.columns[i], X.columns[j], strength[i, j])To see the whole matrix at once, call shap.summary_plot(inter, Xs). It draws a grid in which the diagonal shows main effects and the off-diagonal cells show interactions.
Linking back to scatter plots
A scatter plot colored by a second feature hints at an interaction. The interaction values let you check that hint. Plot the pair you found, using its column names:
sv = explainer(X)
shap.plots.scatter(sv[:, "bmi"], color=sv[:, "s5"])If the vertical spread at a given bmi lines up with the color, the effect of bmi depends on s5. You can then plot inter[:, i, j] against the feature values to see the interaction alone.
Limits
- Computation is slow for many features or deep trees, so subsample rows.
- Interaction values are defined for pairs only. Three-way effects are folded into the pairwise terms.
- The usual cautions still apply. Correlated features can share or hide an interaction, and a large interaction describes the model, not the real world.
Exercise
For a single row k, what do inter[k].sum() and inter[k].sum(axis=1) give you, in terms of the baseline and the model's prediction?
Show answer
inter[k].sum() is the prediction minus the baseline, because the full matrix is additive. inter[k].sum(axis=1) is the ordinary SHAP value of each feature, since each row sums to that feature's SHAP value.