Sub topic 3
The baseline value
This chapter assumes you can fit a scikit-learn model, read basic NumPy output, and know what a Shapley value is from the previous chapters. Here you will see where the baseline comes from, how the background data sets it, and why it changes what SHAP values mean.
What the baseline is
A SHAP explanation answers one question: why is this prediction different from a reference prediction? The reference is the baseline value. It is the model's expected output when no feature value is known. In the shap library it is stored as expected_value on an explainer and as base_values on an Explanation object.
Every SHAP value is measured against this reference. A SHAP value of +3 for bmi means "this feature pushes the prediction 3 units above the baseline." It does not mean "3 units above zero."
How the background data sets it
To "remove" a feature, SHAP replaces its value with values drawn from the background data, a reference sample of rows. The baseline is the average model output over that sample. A different background gives a different baseline, and therefore different SHAP values for the same row.
The brute-force function below makes this concrete. It uses a single baseline row as the background. Run it with two baselines.
from itertools import combinations
from math import factorial
def shapley_values(f, x, baseline):
n = len(x)
def value(subset):
z = [x[i] if i in subset else baseline[i] for i in range(n)]
return f(z)
phi = []
for i in range(n):
others = [j for j in range(n) if j != i]
total = 0.0
for size in range(n):
for subset in combinations(others, size):
s = set(subset)
weight = factorial(len(s)) * factorial(n - len(s) - 1) / factorial(n)
total += weight * (value(s | {i}) - value(s))
phi.append(total)
return phi
f = lambda z: 2 * z[0] + z[1] * z[2]
x = [1, 2, 3]
for baseline in ([0, 0, 0], [1, 1, 1]):
phi = shapley_values(f, x, baseline)
print(baseline, phi, sum(phi), f(x) - f(baseline))
# [0, 0, 0] [2.0, 3.0, 3.0] 8.0 8.0
# [1, 1, 1] [0.0, 2.0, 3.0] 5.0 5.0The prediction f(x) is 8 in both runs. With the second baseline, feature 0 gets a value of 0 because x[0] already equals the baseline's x[0]. Replacing it changes nothing. The SHAP values describe a contrast with the reference, not an absolute effect.
The baseline in the shap library
For tree models, TreeExplainer uses the training-data statistics stored in the trees when you give it no background. For other models you pass a background sample yourself. Here is the tree case.
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)
explainer = shap.TreeExplainer(model)
sv = explainer(X)
print(np.ravel(explainer.expected_value)[0]) # baseline
print(model.predict(X).mean()) # average prediction, close to the baseline
print(np.isclose(sv.base_values[0] + sv.values[0].sum(),
model.predict(X.iloc[[0]])[0], atol=1e-4))The baseline is close to the average prediction over the data. The last line checks the additive property: baseline plus the row's SHAP values reproduces that row's prediction.
Choosing a background
The right background depends on the question you ask.
| Background | Baseline means | Use when |
|---|---|---|
| Whole training set (or a sample) | Average prediction for a typical row | You want general explanations |
| A subgroup, such as one customer segment | Average prediction for that group | You want to compare against peers |
| A single "neutral" row | Prediction for that specific row | A meaningful reference point exists |
Larger backgrounds give a more stable baseline but cost more time for model-agnostic explainers. A sample of 100 to 1,000 rows is a common compromise.
Exercise
A regression model has a baseline of 5.0. For one row, the SHAP values are +1.5, -0.5 and +0.25. What does the model predict for that row, and what would change if you recomputed with a background whose average output is 6.0?
Show answer
The prediction is 5.0 + 1.5 - 0.5 + 0.25 = 6.25. With a background averaging 6.0, the baseline would be 6.0 and the SHAP values would shift so that they still sum to 6.25 - 6.0 = 0.25. The prediction stays at 6.25. Only the reference and the split between features change.