Sub topic 11
How a feature's effect varies: scatter plots
This chapter assumes you can fit a scikit-learn model, you know basic pandas and NumPy, and you have already built a SHAP Explanation object. The example below rebuilds one so the code runs on its own.
What a scatter plot shows
A bar plot gives one number per feature. It hides how the effect changes across values of that feature. A SHAP scatter plot (also called a dependence plot) shows that change.
Each dot is one row of the data:
- The x-axis is the feature's value in that row.
- The y-axis is that feature's SHAP value in that row.
If the dots form a rising line, higher values push predictions up. A curve means the effect is nonlinear. Vertical spread at a fixed x means the effect also depends on other features.
Drawing the plot
The code fits the model, computes SHAP values, and draws the plot for bmi. The second plot colors each dot by the feature that SHAP finds most strongly related to bmi's effect.
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)
sv = shap.TreeExplainer(model)(X)
shap.plots.scatter(sv[:, "bmi"]) # plain dependence plot
shap.plots.scatter(sv[:, "bmi"], color=sv) # colored by best interaction candidateThe slice sv[:, "bmi"] keeps all rows and one feature. Its .data holds the x values and its .values holds the y values. You can draw the same plot by hand to confirm this:
import matplotlib.pyplot as plt
bmi = sv[:, "bmi"]
plt.scatter(bmi.data, bmi.values, s=10)
plt.xlabel("bmi (standardized)")
plt.ylabel("SHAP value for bmi")
plt.show()The y-axis is in the model's output units. For this regression model that is the disease progression score. A dot at +20 means that in that row, bmi raised the prediction by 20 above the baseline.
Reading the color
Coloring by a second feature helps explain vertical spread. Suppose dots at the same bmi split into a red band above and a blue band below. Then the effect of bmi depends on the colored feature. You can pick the color feature yourself:
shap.plots.scatter(sv[:, "bmi"], color=sv[:, "bp"])With color=sv, the library chooses the feature for you using a correlation heuristic. Treat that choice as a hint to check, not a finding.
Common errors
- Reading the plot as a partial dependence curve. A SHAP scatter plot shows each row's attribution. It is not the average prediction as you vary the feature while holding the others fixed.
- Reading a trend as causal. The plot describes what the model learned, not what happens in the world.
- Trusting sparse regions. Where there are few dots, the pattern is noisy, and the model itself is poorly constrained. The histogram along the x-axis shows where the data is dense.
- Ignoring scale. Here the data is standardized, so the x values are not in original units. Check
sv.databefore labeling axes.
Exercise
In a scatter plot of SHAP values for bmi, the dots at one x position range from -5 to +25 on the y-axis. What does this spread tell you, and how can you investigate it?
Show answer
The same bmi value contributes differently in different rows, so the model's use of bmi depends on other features. Color the dots by another feature, for example color=sv[:, "bp"], and look for bands. Then confirm with SHAP interaction values, because correlated features can create color patterns without a true interaction.