LIME: Local Interpretable Model-Agnostic Explanations
LIME (Local Interpretable Model-agnostic Explanations) is a Python library designed to explain individual predictions of machine learning classifiers and regressors. It works for tabular, text, and image data by building local, interpretable surrogate models around the instance to be explained. The current version is 0.2.0.1 and the library is actively maintained.
Warnings
- breaking Python 2 support was dropped in version 0.2.0.0. LIME now requires Python 3.5 or newer.
- gotcha For `LimeTabularExplainer`, `training_data` is crucial. It is used to compute statistics (mean, std dev, frequencies) for feature perturbation and discretization. Providing a non-representative or empty `training_data` can lead to inaccurate or misleading explanations.
- gotcha The `predict_fn` passed to `explain_instance` must return probabilities for classification tasks (e.g., `model.predict_proba`) and raw predicted values for regression tasks (e.g., `model.predict`). Mismatching this can cause errors or incorrect explanations.
- gotcha LIME explanations can be sensitive to hyperparameters like `num_samples` (number of perturbed samples) and the choice of distance metric, potentially leading to varied or unstable explanations across runs or slight changes in settings.
Install
-
pip install lime
Imports
- LimeTabularExplainer
from lime.lime_tabular import LimeTabularExplainer
- LimeTextExplainer
from lime.lime_text import LimeTextExplainer
- LimeImageExplainer
from lime.lime_image import LimeImageExplainer
Quickstart
import numpy as np
import sklearn
import sklearn.ensemble
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import lime
import lime.lime_tabular
# 1. Prepare Data and Model
iris = load_iris()
X = iris.data
y = iris.target
feature_names = iris.feature_names
class_names = iris.target_names
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = sklearn.ensemble.RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# 2. Create a LIME Explainer for Tabular Data
explainer = lime.lime_tabular.LimeTabularExplainer(
training_data=X_train,
feature_names=feature_names,
class_names=class_names,
mode='classification'
)
# 3. Choose an instance to explain
i = np.random.randint(0, X_test.shape[0])
instance_to_explain = X_test[i]
# 4. Generate the explanation
explanation = explainer.explain_instance(
data_row=instance_to_explain,
predict_fn=model.predict_proba,
num_features=2
)
# 5. Print the explanation
print(f"Explaining instance: {instance_to_explain}")
print(f"Predicted class: {iris.target_names[model.predict(instance_to_explain.reshape(1, -1))[0]]}")
print("--- Local Explanation ---")
print(explanation.as_list())
# For visualization in a Jupyter Notebook:
# explanation.show_in_notebook(show_table=True)