Sub topic 7
Explaining any model with Permutation and Kernel methods
This chapter assumes you can fit a scikit-learn model, know what a background dataset and baseline are in SHAP, and have used TreeExplainer. Here you will explain a model that has no dedicated explainer.
When you need a model-agnostic explainer
TreeExplainer reads the internal structure of tree ensembles. Many models offer no such structure to read: a support vector machine, a k-nearest-neighbors model, a stacked ensemble, or a function that wraps a model with extra preprocessing. For these you pass only a prediction function and a background sample. The explainer then treats the model as a black box. It builds inputs where some features come from the row you are explaining and the rest come from background rows, and it calls the function on them.
Two algorithms do this work.
- Permutation walks through random orderings of the features. For each ordering it switches features from background values to the row's values one at a time. It records how the prediction changes at each step.
- Kernel samples subsets of features (coalitions) and fits a weighted linear regression. The regression coefficients are the SHAP values.
Running both methods
import numpy as np
import shap
from sklearn.datasets import load_diabetes
from sklearn.svm import SVR
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
X, y = load_diabetes(return_X_y=True, as_frame=True)
model = make_pipeline(StandardScaler(), SVR()).fit(X, y)
background = shap.sample(X, 50, random_state=0)
rows = X.iloc[:5]
# Permutation: returns an Explanation object
perm = shap.explainers.Permutation(model.predict, background)
sv = perm(rows)
print(sv.values.shape) # (5, 10)
print(np.allclose(sv.base_values + sv.values.sum(axis=1),
model.predict(rows), atol=1e-6))
# Kernel: returns a plain array
kern = shap.KernelExplainer(model.predict, background)
kv = kern.shap_values(rows, nsamples=500)
print(kv.shape) # (5, 10)
print(np.allclose(kern.expected_value + kv.sum(axis=1),
model.predict(rows), atol=1e-3))Both checks should print True. Both methods keep the additivity property from earlier chapters: baseline plus the sum of SHAP values reproduces the prediction. The two sets of values will be close but not identical, because both are estimates.
Choosing between them
| Aspect | Permutation | Kernel |
|---|---|---|
| Return type | `Explanation` object | NumPy array |
| Cost | Grows roughly linearly with feature count | Grows quickly with feature count and `nsamples` |
| Accuracy | Exact for main effects and pairwise interactions with enough evaluations | Approximate, depends on `nsamples` |
| Usual choice | Default for tabular models | Older code, or when you need its regression behavior |
Prefer Permutation unless you have a reason not to. Its output plugs straight into shap.plots.waterfall and shap.plots.beeswarm.
Controlling cost
Every explained row needs many model calls, and each call is repeated across background rows. Three settings control the cost:
- Use a small background sample. 50 to 100 rows is common.
shap.sampledraws rows at random.shap.kmeans(X, k)summarizes the data with k weighted centers. - Explain a subset of rows instead of the whole dataset.
- Lower
max_evalsfor Permutation ornsamplesfor Kernel. Fewer evaluations add noise, so compare two runs before trusting small differences.
Exercise
You explain a support vector regressor with Kernel SHAP on 5 rows. For one row, expected_value + shap_values.sum() differs from model.predict by a large amount. Name one likely cause and one fix.
Show answer
The nsamples budget is probably too small for the number of features, so the regression is poorly constrained. Increase nsamples, or switch to the Permutation explainer. Also check that you passed the same function (model.predict, not predict_proba) when you compare outputs.