Skip to content

Interpretability, attribution, and explanation audits · Interpretability, explanations, and model-mechanism analysis · EXP001 · Algorithm

LIME: local interpretable model-agnostic explanations

Fit an interpretable surrogate around a particular input by querying a fixed predictor on perturbed examples. The explanation describes a chosen local approximation rather than the complete predictor.

Runs in CodeSplash (Experimental) Small complete fit CodeSplash's run and the reference's record

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.

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
Small complete fit
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through explain.lime. 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

Fit an interpretable surrogate around a particular input by querying a fixed predictor on perturbed examples. The explanation describes a chosen local approximation rather than the complete predictor.

Question / hypotheses

Which simple features approximate this predictor near this input under the selected perturbation distribution? This supplies neither a causal effect nor an intrinsic significance test.

Architecture / computational structure

A representation map connects interpretable features, such as token presence or superpixels, to model inputs. Perturbations receive proximity weights. A sparse or otherwise constrained surrogate is fitted to the model responses; a different surrogate can be fitted for each explained case.

Assumptions

The interpretable perturbations must be meaningful for the application and the proximity geometry must define a useful neighborhood. The predictor must return a consistent declared output, such as a class probability or score. Local fidelity must be checked rather than assumed.

Formula / computational definition

g^x=arg⁡min⁡g∈G∑j=1mπx(zj){f(zj)−g(zj′)}2+Ω(g),πx(z)=exp⁡[−D(x,z)2/σ2].\widehat g_x=\arg\min_{g\in\mathcal G}\sum_{j=1}^m\pi_x(z_j)\{f(z_j)-g(z_j^\prime)\}^2+\Omega(g),\qquad \pi_x(z)=\exp[-D(x,z)^2/\sigma^2].

Parameters and notation

The original predictor is fixed. Fitted explanation parameters are surrogate coefficients. Design choices include interpretable representation, distance D, kernel width σ, perturbation distribution/count m, feature-selection penalty Ω, selected output and random seed. Coefficients depend on feature scaling and differ from additive contributions to the original prediction. Here x is the explained input, z_j a perturbed model input, z_j-prime its interpretable encoding, f the fixed scalar prediction, G the surrogate class, and g the fitted surrogate; pi is the locality weight.

Fitting, fine-tuning, and adaptation

Refit explanations after changing the predictor, preprocessing, selected output, or domain. Surrogate fitting is not fine-tuning the predictor. When comparing checkpoints, keep the explanation neighborhood fixed as well as reporting any model-specific adjustment.

How to use and calibrate

Report a weighted holdout error for fresh perturbations, stability across seeds and kernel widths, and whether perturbed inputs are plausible. Match the surrogate loss to the response being approximated. Save sampled neighborhoods when a result needs to be reproducible.

Example application

Explain a local nonlinear regression prediction with a weighted linear approximation, then demonstrate that a broader neighborhood changes the fitted slope and approximation quality.

Interpretation / pitfalls

Watch out

An attractive coefficient bar chart can summarize a poor surrogate. Correlated features and unrealistic masked inputs can change the explanation substantially. A coefficient can describe a local model artifact instead of a real-world intervention effect.

Python and R code examples

Scope: Complete weighted local linear fit on an explicitly supplied neighborhood; no sparse feature-selection search. 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.

Python

Small complete fit — Complete weighted local linear fit on an explicitly supplied neighborhood; no sparse feature-selection search. · needs numpy

import numpy as np
Z=np.array([[-.2,-.2],[-.2,.2],[0.,0.],[.2,-.2],[.2,.2],[.4,0.]])
y=Z[:,0]+2*Z[:,1]+Z[:,0]*Z[:,1]; w=np.exp(-(Z**2).sum(1)/.3**2)
A=np.column_stack([np.ones(len(Z)),Z]); beta=np.linalg.solve(A.T@(w[:,None]*A),A.T@(w*y))
print(beta, np.sum(w*(y-A@beta)**2)/w.sum())
R

Small complete fit — Complete weighted local linear fit on an explicitly supplied neighborhood; no sparse feature-selection search.

Z <- rbind(c(-.2,-.2),c(-.2,.2),c(0,0),c(.2,-.2),c(.2,.2),c(.4,0))
y <- Z[,1]+2*Z[,2]+Z[,1]*Z[,2]; w <- exp(-rowSums(Z^2)/.3^2)
A <- cbind(1,Z); beta <- solve(crossprod(A,A*w),crossprod(A,w*y))
print(beta);print(sum(w*(y-drop(A%*%beta))^2)/sum(w))

Sources

From the CodeSplash reference, entry EXP001.