LIME (Local Interpretable Model-agnostic Explanations) › Implementation using the Official LIME Package

Michael BrenndoerferJuly 27, 20255 min read
Machine Learning from ScratchAll chapters
Part 6 of 9Explainability
Chapter 52LIME (Local Interpretable Model-agnostic Explanations) › Implementation using the Official LIME Package
Reading section 6 of 9 · Implementation using the Official LIME Package · 5 minSection 6 of 9 · 5 min

Book contents

Section 6 of 9 · 5 min read

LIME (Local Interpretable Model-agnostic Explanations) › Implementation using the Official LIME PackageOne section · Sections in this chapter6 / 9 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The official LIME package (https://github.com/marcotcr/lime) provides a production-ready implementation for various data types. For tabular data, we use LimeTabularExplainer, which automatically generates and weights samples before fitting the local model.

Now let's demonstrate the official LIME implementation with a practical example:

In[17]:
Code
from lime.lime_tabular import LimeTabularExplainer

# Create a more complex dataset
X, y = make_classification(
    n_samples=1000,
    n_features=5,
    n_informative=3,
    n_redundant=1,
    n_clusters_per_class=1,
    random_state=42,
)

# Split and train
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42
)
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)

# Initialize LIME explainer
# The explainer needs the training data to understand feature distributions
feature_names = [f"Feature_{i + 1}" for i in range(X.shape[1])]
lime_explainer = LimeTabularExplainer(
    X_train, feature_names=feature_names, mode="classification", random_state=42
)

# Explain a specific instance
instance_to_explain = X_test[0]
explanation = lime_explainer.explain_instance(
    instance_to_explain, rf_model.predict_proba, num_features=5
)
In[18]:
Code
# Extract feature importance from explanation
importance_list = explanation.as_list()
importance_df = pd.DataFrame(
    importance_list, columns=["Feature", "Importance"]
).sort_values("Importance", key=abs, ascending=False)

# Get predictions for comparison
original_prediction = rf_model.predict_proba([instance_to_explain])[0, 1]
Out[19]:
Console
LIME Feature Importance:
                    Feature  Importance
0          Feature_3 > 0.98    0.443949
1         Feature_5 > -0.61    0.176028
2  0.05 < Feature_1 <= 0.38   -0.065185
3        Feature_4 <= -0.68   -0.011901
4         Feature_2 <= 0.47    0.010636

Complex model prediction: 0.990

Local model prediction: 0.460

The feature importance list shows each feature and its contribution. Positive values mean the feature increases the probability of the predicted class. Negative values decrease it. The local model prediction should be close to the complex model prediction. When they match well, it means the linear approximation captures the model's behavior accurately.

Let's also visualize the explanation:

In[20]:
Code
# Convert explanation to a pandas DataFrame for easier manipulation
explanation_df = pd.DataFrame(
    importance_list, columns=["Feature", "Importance"]
).sort_values("Importance", key=abs, ascending=False)

# Extract just the absolute importance values for visualization
feature_names_vis = explanation_df["Feature"].values
importance_values = explanation_df["Importance"].values
Out[21]:
Visualization
Horizontal diverging bar chart of five LIME feature conditions, sorted by absolute importance, with blue positive contributions, coral negative contributions, a zero reference line, and numeric labels on every bar.
LIME feature importance showing the contribution of each feature to the prediction, sorted by absolute importance. Positive values (blue) increase the probability of the predicted class, while negative values (coral) decrease it.

Key Parameters

Below are the main parameters that affect how LIME works and performs.

For LimeTabularExplainer:

  • training_data (required): The training dataset. The explainer uses this to understand feature distributions and generate meaningful perturbations.
  • feature_names (optional): List of feature names. Improves readability of explanations.
  • mode (default: 'classification'): Either 'classification' or 'regression'. Determines how predictions are interpreted.
  • kernel_width (default: 0.75): Width of the exponential kernel for weighting instances. Smaller values focus on very close instances. Larger values include more distant ones.
  • discretize_continuous (default: True): Whether to discretize continuous features. Can improve explanations for some models.
  • random_state (optional): Seed for reproducibility. Set to an integer for consistent results across runs.

For explain_instance():

  • num_features (default: 10): Number of features to include in the explanation. More features give a more complete picture but may include less important ones.
  • num_samples (default: 5000): Number of perturbed samples to generate. More samples improve quality but increase computation time. Values between 1000-5000 typically work well.

Key Methods

The following are the most commonly used methods for working with LIME.

  • LimeTabularExplainer(training_data, ...): Initializes a LIME explainer for tabular data. It requires the training data. Optional parameters configure features and the explanation mode; kernel_width controls the kernel width.
  • explain_instance(data_row, predict_fn, num_features=10, ...): Generates a LIME explanation for a single instance. Takes the instance to explain and the prediction function. Returns an Explanation object.
  • explanation.as_list(): Returns the explanation as a list of (feature, importance) tuples, sorted by absolute importance. Useful for programmatic access.
  • explanation.show_in_notebook(): Displays the explanation in interactive HTML format within Jupyter notebooks.
  • explanation.as_pyplot_figure(): Creates a matplotlib visualization. Useful for static visualizations or non-notebook environments.