LIME - Local Interpretable Model-agnostic Explanations
Learn how LIME explains individual predictions by building simple local surrogate models for tabular data, text, and images.
How LIME Works
LIME explains a prediction by:
Perturb the input
Generate variations of the input sample by slightly modifying feature values.
Get predictions
Pass the perturbed samples through the black-box model to get predictions.
Weight by proximity
Assign higher weights to perturbed samples that are closer to the original input.
Fit a simple model
Train an interpretable model (e.g., linear regression) on the weighted perturbed data.
Extract explanation
The coefficients of the simple model reveal which features matter most locally.
Installation
pip install lime
LIME for Tabular Data
import lime
import lime.lime_tabular
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# Train a model
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.2
)
model = RandomForestClassifier(n_estimators=100).fit(X_train, y_train)
# Create LIME explainer for tabular data
explainer = lime.lime_tabular.LimeTabularExplainer(
training_data=X_train,
feature_names=iris.feature_names,
class_names=iris.target_names,
mode='classification'
)
# Explain a single prediction
exp = explainer.explain_instance(
X_test[0],
model.predict_proba,
num_features=4,
top_labels=1
)
# Show in notebook
exp.show_in_notebook()
# Or save as HTML
exp.save_to_file('explanation.html')
LIME for Text
from lime.lime_text import LimeTextExplainer
from sklearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
# Train a text classifier
vectorizer = TfidfVectorizer(max_features=5000)
clf = LogisticRegression()
pipeline = make_pipeline(vectorizer, clf)
pipeline.fit(train_texts, train_labels)
# Create text explainer
explainer = LimeTextExplainer(class_names=['negative', 'positive'])
# Explain a prediction
exp = explainer.explain_instance(
"This movie was absolutely fantastic and thrilling",
pipeline.predict_proba,
num_features=10
)
# Words highlighted green push toward positive,
# red push toward negative
exp.show_in_notebook()
LIME for Images
from lime import lime_image
from skimage.segmentation import mark_boundaries
import matplotlib.pyplot as plt
# Create image explainer
explainer = lime_image.LimeImageExplainer()
# Explain a prediction (model is a Keras/PyTorch image classifier)
explanation = explainer.explain_instance(
image, # numpy array (H, W, C)
model.predict, # prediction function
top_labels=3,
hide_color=0,
num_samples=1000 # more samples = better accuracy
)
# Get image with highlighted superpixels
temp, mask = explanation.get_image_and_mask(
explanation.top_labels[0],
positive_only=True,
num_features=5,
hide_rest=False
)
plt.imshow(mark_boundaries(temp / 255.0, mask))
plt.title("Regions supporting the prediction")
plt.show()
SHAP vs LIME
| Aspect | SHAP | LIME |
|---|---|---|
| Theory | Game theory (Shapley values) | Local surrogate models |
| Consistency | Theoretically guaranteed | Can vary between runs |
| Speed | Fast with TreeExplainer | Moderate (depends on num_samples) |
| Global explanations | Yes (aggregate SHAP values) | No (local only) |
| Data types | Tabular, text, images | Tabular, text, images |
| Ease of use | Very easy | Very easy |
random_state parameter) for reproducibility, and consider averaging multiple runs for more stable explanations.Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX