Sub topic 3
Summarizing a Whole Model
Assume you can train a scikit-learn model and understand NumPy arrays, pandas tables, and tree ensembles. A local SHAP explanation describes one prediction. A global explanation summarizes many rows.
Global plots
The bar plot ranks features by their mean absolute SHAP value. A larger value means that the feature usually changes predictions more, regardless of direction.
The beeswarm plot shows the distribution. Each point is one row. Points to the right increase the prediction, while points to the left decrease it. Color often shows whether the feature value is high or low.
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)
shap.plots.beeswarm(sv)
shap.plots.scatter(sv[:, "bmi"], color=sv)How a feature's effect varies: scatter plots
A dependence scatter plot places a feature value on the horizontal axis and its SHAP value on the vertical axis. It shows whether the feature's effect is linear, changes direction, or becomes stronger at some values. Color can reveal interaction with another feature.
Output units
For regression, SHAP values use the model's prediction units. If the model predicts dollars, a SHAP value is dollars.
For binary classification, values may use probability or log-odds, depending on the explainer and its configuration. A log-odds value is not a probability. Check the explainer output before interpreting magnitudes. Multi-class models usually return separate SHAP values for each class.
Feature interactions
An interaction value splits a prediction's contribution between a feature's own effect and its joint effect with another feature. For tree models, TreeExplainer can calculate these values. Large off-diagonal values suggest that two features work together, but they do not prove a causal relationship.
Limitations: correlation, causality and cost
Correlated features can share or compete for attribution. The result depends on the background data and the feature-masking assumptions. SHAP is also not a causal analysis. Finally, model-agnostic explainers can require many predictions, making them expensive for large datasets.
Exercise
A feature has a high mean absolute SHAP value but its beeswarm points appear on both sides of zero. What does this mean?
Show answer
The feature often has a strong effect, but its direction changes across rows.