Interpretability, attribution, and explanation audits · Interpretability, explanations, and model-mechanism analysis · EXP002 · Algorithm
KernelSHAP
Estimate Shapley feature attributions by fitting a weighted additive explanation over feature coalitions, using the SHAP kernel and constraints that fix the empty and full coalition values.
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.
- SHAP values for a saved model Experimental
Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.
- Availability
- Runs in CodeSplash (Experimental)
- Code example
- Complete calculation
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- Verified in Python and R through explain.shap. The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Estimate Shapley feature attributions by fitting a weighted additive explanation over feature coalitions, using the SHAP kernel and constraints that fix the empty and full coalition values.
Question / hypotheses
How should the difference between a prediction and a chosen reference value be allocated among features for this specified coalition game?
Architecture / computational structure
A masker or background distribution defines a value v(S) for observing subset S. Model evaluations on masked coalitions feed a constrained weighted regression. Sampling coalitions approximates a calculation that can instead be enumerated for very few features.
Assumptions
The coalition value must be specified: fixed-reference replacement, empirical-background replacement, and conditional expectations define different games. Correlated features make these differences consequential. The regression needs sufficient informative coalitions and numerical care at the constrained endpoints.
Formula / computational definition
Parameters and notation
Fitted explanation coefficients are φ; the original model is fixed. Hyperparameters/design choices include background/masker, output scale or link function, coalition budget, feature grouping, approximation seed and any regularization. Empty/full coalitions are constraints, not finite ordinary kernel weights. Here F is the full feature set, d its size, S a coalition, i a feature index, v the declared coalition-value function, and w the regression kernel weight for an interior coalition.
Fitting, fine-tuning, and adaptation
Recompute explanations after adapting the predictor or reference population. Preserve the background definition when comparing versions, or explicitly report both reference choices. Do not tune the background to manufacture a preferred feature ranking.
How to use and calibrate
Check local additivity on the stated output scale, stability against a larger coalition/background budget, and sensitivity to plausible reference choices. Use exact enumeration as a unit check for small d. Budget cost includes every masked prediction.
Example application
Explain a two-feature interaction model at x=(1,1), allocating the interaction according to the full coalition game rather than treating coefficients as independent main effects.
Interpretation / pitfalls
Watch out
Additivity is a bookkeeping property for a game; it does not establish causality, calibration, or fairness. Feature dependence and preprocessing grouping change the meaning of absent features. Approximate regression can fail before the Shapley definition does.
Python and R code examples
Scope: Exact two-feature Shapley solution, equivalent to the fully constrained KernelSHAP solution for this tiny game; not a sampled high-dimensional implementation. Python: NumPy; R: base R.
Validation: Python executed successfully on the supplied fixture; R reviewed but unexecuted. Full implementations and supported operation details are provided in the linked primary/official sources.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Complete calculation — Exact two-feature Shapley solution, equivalent to the fully constrained KernelSHAP solution for this tiny game; not a sampled high-dimensional implementation. Python: NumPy; R: base R. · needs numpy
import numpy as np
v00,v10,v01,v11=0.,1.,2.,7.
phi=np.array([.5*((v10-v00)+(v11-v01)),.5*((v01-v00)+(v11-v10))])
assert np.isclose(phi.sum(),v11-v00)
print(phi)
Complete calculation — Exact two-feature Shapley solution, equivalent to the fully constrained KernelSHAP solution for this tiny game; not a sampled high-dimensional implementation. Python: NumPy; R: base R.
v00 <- 0; v10 <- 1; v01 <- 2; v11 <- 7
phi <- c(.5*((v10-v00)+(v11-v01)),.5*((v01-v00)+(v11-v10)))
stopifnot(abs(sum(phi)-(v11-v00))<1e-12);print(phi)
Sources
- Source 1arxiv.org
- Source 2shap.readthedocs.io
From the CodeSplash reference, entry EXP002.