Explain Your Model with LIME

(Revised on June 16, 2022)
Why Is Model Interpretability so Important?
In this article, I will introduce the LIME approach. I will start with the questions that the inventors of LIME were concerned with, then walk you through their solutions. You may be interested in knowing their thought process or even adopting their problem-solving approach.
People typically agree that a linear model is more interpretable than a complicated machine learning model. Is it true? Do you think the following linear model is easily interpretable? Probably not. It has too many variables.

Besides the above concern, the second concern is that this model has many variables. For an individual prediction, only a few variables play significant roles in the prediction. The rest variables are almost irrelevant to an individual prediction.
These two questions are what Marco Tulio Ribeiro, Sameer Singh, and Carlos Guestrin, the authors of the paper “Why Should I Trust You?” Explaining the Predictions of Any Classifier (KDD2016), concerned about. Let’s see their augments.
(A) “Why Should I Trust You?”
The authors of LIME argue that we should build two types of trusts for a user to adopt a model:
- Trusting a prediction: a user will trust an individual prediction to act upon. No user wants to accept a model prediction on blind faith, especially if the consequences can be catastrophic.
- Trusting a model: the user gains enough trust that the model will behave in reasonable ways when deployed. Although in the modeling stage accuracy metrics (such as AUC — Area under the curve) is used on multiple validation datasets to mimic the real-world data, there often exist significant differences in the real-world data. Besides using the accuracy metrics, we need to test the individual prediction explanations.
(B) “Easily Interpretable” and “Local Fidelity”
They argue that a model should be easily interpretable. The linear equation in Figure (A) is probably not easily interpretable.
Second, for an individual prediction, there may be only a few variables influencing its predicted value. The interpretation should make sense from an individual prediction’s view. The authors of LIME call this local fidelity. Globally important features may not be important in the local context, and vice versa. Because of this, it could be the case that only a handful of variables directly relate to a local (individual) prediction, even if a model has hundreds of variables globally.
In summary, the authors of LIME have two criteria for model explainability:
- Easy to interpret: A linear model can have hundreds or thousands of variables. Is it more interpretable than a complex gradient boosting or deep learning model?
- Local fidelity: the explanation for individual predictions should at least be locally faithful, i.e. it must correspond to how the model behaves in the vicinity of the individual observation being predicted. The authors address that local fidelity does not imply global fidelity: globally important features may not be important in the local context, and vice versa. Because of this, it could be the case that only a handful of variables directly relate to a local (individual) prediction, even if a model has hundreds of variables globally.
That’s why they named their technique Local Interpretable Model-Agnostic Explanations (LIME) — It should be locally interpretable and able to explain any model.
(C) How Is LIME Different from SHAP?
In “Explain Your Model with the SHAP Values” I describe extensively how the SHAP (SHapley Additive exPlanations) is distinctly built on the Shapley value. The Shapley value is the average of the marginal contributions across all permutations. The Shapley values consider all possible permutations, thus SHAP is a united approach that provides global and local consistency and interpretability. However, the disadvantage of the SHAP algorithm is the long computational time — it has to compute all permutations to give the results. In contrast, LIME (Local Interpretable Model-agnostic Explanations) builds sparse linear models around an individual prediction in its local vicinity. In short, the LIME algorithm is a subset of the SHAP algorithm. This interesting finding is documented in Lundberg and Lee (2016).
(D) The Advantage of LIME over SHAP — SPEED
Readers may ask: “If SHAP is already a united solution, why should we consider LIME?” Remember, the two methods emerge very differently. The advantage of LIME is speed. LIME perturbs data around an individual prediction to build a model, while SHAP has to compute all permutations globally to get local accuracy.
(E) How Does LIME Work?

The authors of LIME have an intuitive graph as shown in Figure (E). The original complex model is represented by the blue/pink background. It is not linear. The bold red cross is the individual prediction to be explained. The algorithm of LIME does the following steps:
- Generating new samples then gets their predictions using the original model, and
- Weighing these new samples by the proximity to the instance being explained (represented in Figure (E) by size).
Then it builds a linear regression for these newly created samples including the red cross. The dashed line is the learned explanation that is locally (but not globally) faithful.
(F) How to Use LIME in Python?
To let you compare SHAP and LIME, I use the red wine quality data used in “Explain Your Model with the SHAP Values” and “Explain Any Models with the SHAP Values — Use the KernelExplainer”. There are 1,599 wine samples. The column of the target is the quality rating from low to high (0–10). The input variables are the content of each wine sample including fixed acidity, volatile acidity, citric acid, residual sugar, chlorides, free sulfur dioxide, total sulfur dioxide, density, pH, sulphates, and alcohol. You can get the code via this Github.
The purpose of LIME is to explain a machine learning model. So I will build a random forest model in Section (F.1), then apply LIME in Section (G).
(F.1) Build a model











