Sub topic 5
Choosing the right explainer
This chapter assumes you can train a scikit-learn style model, you know what background data and the baseline value are, and you know that SHAP values add up to the prediction minus the baseline. Here you decide which SHAP algorithm to run.
Why the choice matters
Every SHAP explainer estimates the same quantity, Shapley values. They differ in what they assume about the model, how fast they run, and how they handle removed features. A fast explainer built for your model type gives exact values in seconds. A generic one gives estimates that can take minutes.
The main options
| Explainer | Works with | Result | Cost |
|---|---|---|---|
| `TreeExplainer` | Gradient boosting, random forests, XGBoost, LightGBM, CatBoost | Exact | Fast |
| `LinearExplainer` | Linear and logistic regression | Exact | Very fast |
| `DeepExplainer`, `GradientExplainer` | Neural networks | Approximate | Moderate |
| `PermutationExplainer` | Any prediction function | Estimate that converges | Slow |
| `ExactExplainer` | Any function, few features | Exact | Grows exponentially with features |
| `KernelExplainer` | Any prediction function | Estimate | Slowest |
PermutationExplainer is usually a better default than KernelExplainer for tabular models. The older Kernel method mainly matters for code that already uses it.
A decision path
Letting shap decide
shap.Explainer is a single entry point that inspects the model and picks an algorithm. Pass a fitted tree model and you get tree-based explanation. Pass a plain function and a background sample and you get a model-agnostic algorithm.
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)
# Dedicated explainer: exact and fast for trees
tree_sv = shap.TreeExplainer(model)(X.iloc[:20])
# Generic path: only needs a prediction function and background data
generic = shap.Explainer(model.predict, X.iloc[:100])
generic_sv = generic(X.iloc[:20])
print(type(generic).__name__)
print(tree_sv.values.shape, generic_sv.values.shape)Both calls return an Explanation with the same shape, so the plotting code works with either. The explainer class chosen for a plain function depends on the shap version and the number of features. Check type(generic).__name__ instead of assuming.
Practical trade-offs
- Use the dedicated explainer when one exists. Wrapping a tree model in
model.predictthrows away the structure that makesTreeExplainerfast and exact. - Background data matters for generic explainers. They need it to define what removing a feature means. A sample of 50 to 200 rows is a common compromise.
- Cost grows with rows and features. Explain a subset of rows, such as
X.iloc[:500], before running everything. - Tree explainers can disagree with generic ones. By default
TreeExplainerfollows the paths the training data took through the trees. A generic explainer replaces features with background values. With correlated features the two can differ, and neither is wrong.
Exercise
You trained a random forest on 40 features and need SHAP values for 100,000 rows. A colleague suggests KernelExplainer because "it works with anything". What do you use instead, and why?
Show answer
Use TreeExplainer. It computes exact values by using the tree structure, and it runs far faster than a generic estimator. KernelExplainer works with any model, but it is the slowest option and only gives estimates. At 100,000 rows and 40 features it would take a very long time.