LIME: Local Surrogate Explanations
Approximating the model's behaviour around a single example with a simple linear model, and the reasons for its instability.
The algorithm
- Choose the example \(x\).
- Generate perturbed samples \(z_1, \dots, z_N\) around it.
- Get \(f(z_i)\) from the original model.
- Give each sample a weight \(\pi_x(z_i)\) that decreases with distance from \(x\).
- Train a linear model \(g\) on the interpretable features with the weights.
$$\xi(x) = \arg\min_{g \in G} \; \mathcal{L}(f, g, \pi_x) + \Omega(g)$$
where \(\Omega\) is the complexity of the explanation, such as the number of features.
A tabular example
# pip install lime
from lime.lime_tabular import LimeTabularExplainer
explainer = LimeTabularExplainer(X_tr.values, feature_names=list(X.columns),
class_names=["malignant", "benign"], discretize_continuous=True,
random_state=0)
exp = explainer.explain_instance(X_te.values[0], rf.predict_proba, num_features=6, num_samples=5000)
print(exp.as_list())(The example assumes rf andX_tr andX_te from the feature importance lesson.)
Images and texts
- Text: removing words at random; the explanation highlights the influential words.
- Images: splitting the image into superpixels and hiding some of them.
Limits
| The problem | Detail |
|---|---|
| Instability | Different results between runs |
| Defining the neighbourhood | The choice of kernel width changes the explanation |
| Unrealistic samples | Perturbing features independently produces out-of-distribution examples |
| Can be fooled | A model can be built that behaves differently on perturbed samples and so hides its bias |
Use LIME as an initial exploration tool, and do not rely on it alone for an important decision.
Lesson terms
- Superpixels
- Homogeneous regions of an image used as units of explanation instead of individual pixels.
- Local surrogate model
- A simple model trained to imitate the behaviour of the complex model in the neighbourhood of a specific example.
Abstract
LIME generates perturbed samples around the example to be explained, gets the model's predictions for them, then trains a simple linear model weighted by closeness to the example. The coefficients of this model are the explanation. It works with tabular data, texts and images (through superpixel regions). Its drawbacks: the results depend on the perturbation method, the kernel width and the number of samples, explanations may differ between two runs, and it has been shown that it can be fooled by models designed to do so.
Review questions
-
What determines the "neighbourhood" in LIME?
The method for generating perturbed samples, and the kernel function and its width, which give more weight to nearby samples.
-
How do we check the stability of a LIME explanation?
We repeat the explanation with different seeds and more samples and compare the top features.
Sources and references
Complete the lesson
You have completed all the lessons in the track! You can now request the track completion certificate.
Request the certificate