Sub topic 9
Explaining a single prediction
This chapter covers local explanations: how to read one prediction using SHAP's waterfall and force plots. It assumes you can train a scikit-learn style model, use pandas, and know that SHAP values add up to the prediction minus a baseline. The earlier chapters covered the baseline and the Explanation object.
What a local explanation answers
A global summary says which features matter on average. A local explanation answers a narrower question: why did the model give this row this prediction?
For one row, SHAP splits the gap between the prediction and the baseline into one number per feature. A positive value pushes the prediction above the baseline. A negative value pushes it below. The values add up exactly:
prediction = baseline + sum of the row's SHAP values
Setting up
The code below fits a model and builds the Explanation object. It follows the earlier chapters.
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)
sv = explainer(X)Indexing sv[0] gives an Explanation for a single row. It holds that row's values, the base_values (the baseline), and the data (the feature values).
The waterfall plot
The waterfall plot is the standard local view.
shap.plots.waterfall(sv[0])Read it from the bottom to the top:
- The bottom of the plot shows
E[f(X)], the baseline. - Each row above it is one feature. The label shows the feature's value for this row, such as
bmi = 0.06. - Each bar shows how far that feature moves the running total. Red bars push the total up. Blue bars push it down.
- The top shows
f(x), the model's prediction for this row.
Features are sorted by absolute SHAP value, so the largest contributions come first. When a row has many features, the smallest ones are merged into a single "other features" bar. You can show more rows with max_display.
shap.plots.waterfall(sv[0], max_display=5)The force plot
A force plot shows the same numbers on a single horizontal line. Features that raise the prediction are drawn on one side of the output value, and features that lower it are on the other.
shap.initjs()
shap.plots.force(sv[0])The force plot is interactive and relies on JavaScript. It works in notebooks and can be saved as HTML. It does not render in a plain terminal. The waterfall plot renders in a static figure and shows the size of each bar more clearly, so prefer it for reports and for rows with many features.
Checking the arithmetic
Before you trust a plot, confirm the values add up. The check below is the same one from the additivity chapter, applied to a row of your choice.
import numpy as np
i = 0
reconstructed = sv.base_values[i] + sv.values[i].sum()
print(np.isclose(reconstructed, model.predict(X.iloc[[i]])[0], atol=1e-4))If this prints False, the explainer and the model do not match. A common cause is explaining a different model object than the one you predict with.
Common errors
- Reading bars as changes in the data. A bar says how the model used that feature's value for this row. It does not say what would happen if you changed the feature in the real world.
- Comparing bars across rows. Each row has its own explanation. Sizes in one waterfall are relative to that row's baseline gap only.
- Forgetting the units. For regression, the values are in the units of the target. For classifiers, they are often in log-odds, which a later chapter covers. A bar of 0.5 in log-odds is not a 50% change in probability.
- Ignoring the baseline. A feature can have a large positive value and the prediction can still be below average if other features pull down more. Always read the top and bottom numbers first.
Exercise
For one row, the baseline is 150 and the SHAP values for three features are +40, -15 and +5, with all other features summing to 0. What is the model's prediction, and which feature appears at the top of the waterfall?
Show answer
The prediction is 150 + 40 - 15 + 5 = 180. The waterfall sorts features by absolute SHAP value, so the feature with +40 comes first in the ordering, and it is drawn as the largest bar nearest the top of the feature list.
Exercise
A waterfall plot's final value does not match model.predict for the same row. Name one likely cause and one way to check it.
Show answer
The explainer was probably built from a different model object, or the row used for prediction differs from the one explained. Check by computing base_values[i] + values[i].sum() and comparing it with model.predict on the same row with np.isclose.