Intermediate

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:

  1. Perturb the input

    Generate variations of the input sample by slightly modifying feature values.

  2. Get predictions

    Pass the perturbed samples through the black-box model to get predictions.

  3. Weight by proximity

    Assign higher weights to perturbed samples that are closer to the original input.

  4. Fit a simple model

    Train an interpretable model (e.g., linear regression) on the weighted perturbed data.

  5. Extract explanation

    The coefficients of the simple model reveal which features matter most locally.

Installation

Bash - Install LIME
pip install lime

LIME for Tabular Data

Python - Explaining a classifier
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

Python - Explaining text classification
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

Python - Explaining image classification
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

AspectSHAPLIME
TheoryGame theory (Shapley values)Local surrogate models
ConsistencyTheoretically guaranteedCan vary between runs
SpeedFast with TreeExplainerModerate (depends on num_samples)
Global explanationsYes (aggregate SHAP values)No (local only)
Data typesTabular, text, imagesTabular, text, images
Ease of useVery easyVery easy
When to use LIME over SHAP: LIME is especially useful when you need intuitive visual explanations for text and image models, or when you want to explain models that SHAP's specialized explainers don't support. Its simplicity makes it great for communicating with non-technical stakeholders.
Stability warning: LIME explanations can vary between runs because they use random perturbations. Always set a random seed (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.