Sub topic 10
Summarizing a Whole Model
This chapter assumes you can compute SHAP values and hold them in an Explanation object, as covered in the earlier chapters. A local plot explains one row. A global plot combines the SHAP values of many rows to show how the model behaves overall.
From local values to a global summary
The Explanation object holds one SHAP value per row and per feature. Global plots aggregate that matrix over rows. The two plots below use different aggregations:
- The bar plot averages the absolute SHAP values of each feature.
- The beeswarm plot shows every SHAP value without averaging.
Bar plot: how much does each feature matter?
The bar plot ranks features by mean absolute SHAP value. A feature with a large value moves predictions far from the baseline, on average, in either direction.
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)
sv = shap.TreeExplainer(model)(X)
shap.plots.bar(sv)
importance = np.abs(sv.values).mean(axis=0)
for name, value in sorted(zip(X.columns, importance), key=lambda p: -p[1]):
print(f"{name:>4} {value:.2f}")The printed table matches the bar lengths. The numbers are in the units of the model output, here the diabetes progression score. That makes them easier to interpret than a unitless importance score.
Beeswarm plot: direction and spread
The beeswarm plot keeps the information the bar plot throws away.
shap.plots.beeswarm(sv)Read it as follows:
- Each dot is one row.
- The horizontal position is that row's SHAP value for the feature. Dots to the right push the prediction up. Dots to the left push it down.
- The color is the feature's own value, from low (blue) to high (red).
- Rows are ordered by mean absolute SHAP value, so the top row matches the longest bar.
- Dots pile up vertically where many rows share a similar SHAP value.
If red dots sit on the right and blue dots on the left, higher feature values tend to raise the prediction. A feature with a long, wide spread of dots matters a lot for some rows. A feature with a mixed color pattern has an effect that depends on other features or on the value range.
Choosing between them
| Question | Plot |
|---|---|
| Which features matter most overall? | Bar |
| Does a high value push predictions up or down? | Beeswarm |
| Is the effect concentrated in a few rows? | Beeswarm |
| Do I need a single number per feature for a report? | Bar |
A feature can have a small mean absolute SHAP value because it matters a lot for a few rows and not at all for the rest. The beeswarm plot shows this as a long tail. The bar plot shows only a short bar.
Common mistakes
- Reading bar length as a percentage of the prediction. It is an average shift in output units.
- Treating color as causal. A red dot means a high feature value, not that raising the feature would raise the outcome.
- Computing SHAP values on the training set only and assuming the ranking holds on new data.
Exercise
A feature has a short bar in the bar plot but a few dots far from zero in the beeswarm plot. What does this say about the feature?
Show answer
Most rows have SHAP values near zero, so the mean absolute value is small. A few rows have a large effect. The feature matters a lot for those rows even though it matters little on average.