Implementing LIME with the lime Library
You'll learn how to use Python's lime library to generate practical explanations for tabular, text, and image models.
Core Idea
The lime library provides ready-to-use implementations of LIME for different data types. After conceptually understanding perturbation-based explanations and local surrogate models, you now need practical tools to apply LIME to real models. The library handles the complex sampling, weighting, and surrogate fitting steps automatically.
Basic Implementation Pattern
For tabular data, use LimeTabularExplainer:
from lime.lime_tabular import LimeTabularExplainer
explainer = LimeTabularExplainer(
training_data=X_train,
feature_names=feature_names,
mode='classification' # or 'regression'
)
# Explain a single prediction
explanation = explainer.explain_instance(
data_row=X_test[0],
predict_fn=model.predict_proba,
num_features=10
)
explanation.show_in_notebook()
For text classification, use LimeTextExplainer:
from lime.lime_text import LimeTextExplainer
explainer = LimeTextExplainer(class_names=['negative', 'positive'])
explanation = explainer.explain_instance(
text_instance="This movie was fantastic!",
classifier_fn=model.predict_proba
)
For images, use LimeImageExplainer with superpixel segmentation:
from lime.lime_image import LimeImageExplainer
explainer = LimeImageExplainer()
explanation = explainer.explain_instance(
image=img_array,
classifier_fn=model.predict,
top_labels=5,
num_samples=1000
)
Key Parameters
num_features: Controls explanation complexity (how many features to show)num_samples: Number of perturbed instances to generatepredict_fn: Your model's prediction function (must return probabilities for classification)
Key Takeaway: The lime library abstracts LIME's complexity into explainer objects for tabular, text, and image data—you instantiate an explainer with training context, then call explain_instance() with your model's prediction function to generate local explanations.