SHAP and LIME assumptions
Quick answer
SHAP uses Shapley-style allocation of a prediction relative to a baseline. LIME fits a simple surrogate to model predictions in a chosen local neighborhood. Both depend on design assumptions. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Calculate two-feature Shapley values
v0,v1,v2,v12=0,1,2,7a=((v1-v0)+(v12-v2))/2b=((v2-v0)+(v12-v1))/2print(a,b,a+b==v12-v0)Observed local output:
3.0 4.0 TrueThe interaction is shared equally under this specified coalition game.
How it works
For two features with coalition values v(empty),v({1}),v({2}),v({1,2}), Shapley contributions are phi1=0.5*((v1-v0)+(v12-v2)) and phi2=0.5*((v2-v0)+(v12-v1)). Their sum is v12-v0. More features require averaging marginal contributions over feature orderings; exact enumeration is exponential. The coalition value depends on how absent features are integrated: marginal/interventional masking can break correlations; conditional expectations preserve statistical dependence and may allocate credit to unused correlated features. A reference dataset and output scale must be documented. Even a method called interventional is not automatically causal. SHAP is a family of explainers with model-specific assumptions and approximations. LIME uses perturbed inputs, model predictions, proximity weights and an interpretable surrogate, often sparse linear regression. Local fidelity should be evaluated on fresh neighborhood samples, and repeat seeds, kernel widths, feature representations and sampling choices to assess stability. The examples below implement exact two-feature arithmetic and a simplified weighted local regression, not the SHAP or LIME packages. For a nonlinear model f(x)=x², the slope near x=2 differs from the slope near zero.
Fit a simplified weighted local surrogate
import numpy as npfrom sklearn.linear_model import LinearRegressionx=np.array([0.,1.,2.,3.,4.]); y=x*xw=np.exp(-((x-2)/.7)**2)m=LinearRegression().fit(x[:,None],y,sample_weight=w)print(round(float(m.coef_[0]),3),round(float(m.predict([[2]])[0]),3))Observed local output:
4.0 4.208The slope captures local behavior near 2; the surrogate is approximate and is not the LIME library.
Common mistakes
Do not claim that a Shapley allocation reveals causality, or compare explanations with different baselines and output scales as if identical. Do not present a local surrogate as a globally faithful model. Poor neighborhood fidelity or unstable signs should be reported.
Practice and next steps
Compute Shapley values for v(empty)=0,v1=1,v2=2,v12=7. Change the background and explain why the allocation can change. Try two neighborhood widths for a curved model. Complete the knowledge check and staged exercise. Next: Correlated features and counterfactual limits.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SHAP and LIME assumptions — knowledge check
Apply shap and lime assumptions to new scenarios.
What does Shapley efficiency say here?
What can change conditional SHAP values?
What does a LIME surrogate fit?
How should local surrogate quality be checked?
Do these arithmetic examples execute native SHAP/LIME?