Sub topic 12
Output units: raw margins, log-odds and multi-class outputs
This chapter assumes you can compute an Explanation object and read a waterfall plot. SHAP values add up to a model output, and that output is not always a probability. Reading a value correctly depends on knowing which output it is in.
SHAP values are in the units of the explained output
The additivity rule from earlier still holds: baseline plus the sum of SHAP values equals the model's output for that row. The units are whatever that output is measured in.
| Model | Explained output | Unit of a SHAP value |
|---|---|---|
| Regressor | Predicted value | Same unit as the target (dollars, days, and so on) |
| Gradient boosting classifier, binary | Raw margin | Log-odds |
| Random forest classifier | Averaged class probability | Probability |
| Multi-class classifier | One output per class | Depends on the model, one set of values per class |
A raw margin is the sum of the tree outputs before the final link function. For binary boosted trees the link is the logistic function, so the margin is the log-odds of the positive class.
Binary classification: log-odds
The next example checks additivity against the model's margin, not its probability.
import numpy as np
import shap
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import GradientBoostingClassifier
X, y = load_breast_cancer(return_X_y=True, as_frame=True)
clf = GradientBoostingClassifier(random_state=0).fit(X, y)
sv = shap.TreeExplainer(clf)(X)
margin = sv.base_values[0] + sv.values[0].sum()
print(np.isclose(margin, clf.decision_function(X.iloc[[0]])[0], atol=1e-4))
prob = 1 / (1 + np.exp(-margin))
print(np.isclose(prob, clf.predict_proba(X.iloc[[0]])[0, 1], atol=1e-4))Both checks should print True. The sum matches decision_function, not predict_proba. The logistic function is applied only at the end.
Do not convert each SHAP value to a probability separately. The logistic function is not linear, so the converted pieces no longer add up. A feature with a value of +1.0 log-odds moves the probability by a different amount depending on where the sum already sits.
Multi-class: one set of values per class
For a multi-class model, sv.values has a third axis. Its shape is (n_samples, n_features, n_classes). Slice out one class to get an ordinary Explanation that the plotting functions accept.
import numpy as np
import shap
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
X, y = load_iris(return_X_y=True, as_frame=True)
rf = RandomForestClassifier(n_estimators=100, random_state=0).fit(X, y)
sv = shap.TreeExplainer(rf)(X)
print(sv.values.shape) # (150, 4, 3)
k = 2
total = sv.base_values[0, k] + sv.values[0, :, k].sum()
print(np.isclose(total, rf.predict_proba(X.iloc[[0]])[0, k], atol=1e-4))
shap.plots.waterfall(sv[0, :, k])Here the unit is probability, because a random forest averages the class probabilities of its trees. Each class has its own baseline. Across classes, the SHAP values for one feature sum to zero in a probability model, because the class probabilities must sum to one.
Changing the explained output
TreeExplainer takes a model_output argument. The default, "raw", explains the margin. For tree models, model_output="probability" explains the probability. It needs background data, and you must pass feature_perturbation="interventional". It is slower than the raw default.
bg = X_bg = shap.sample(X, 100, random_state=0)Use it when stakeholders need probability-scale contributions. Otherwise keep the raw margin, where additivity is exact and cheap.
Exercise
A binary boosted model has a baseline of -1.0 log-odds. The SHAP values for one row sum to +2.0. What probability does the model predict for that row?
Show answer
The margin is -1.0 + 2.0 = 1.0 log-odds. The probability is 1 / (1 + exp(-1.0)), which is about 0.73.