LIME (Local Interpretable Model-agnostic Explanations) › Visualizing LIME

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

Book contents

Section 4 of 9 · 5 min read

LIME (Local Interpretable Model-agnostic Explanations) › Visualizing LIMEOne section · Sections in this chapter4 / 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

Let's create visualizations to understand how LIME works in practice. We'll start with a simple example using a 2D dataset to illustrate the core concepts.

In[4]:
Code
import numpy as np
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

# Generate a 2D classification dataset
X, y = make_classification(
    n_samples=1000,
    n_features=2,
    n_redundant=0,
    n_informative=2,
    n_clusters_per_class=1,
    random_state=42,
)

# Split the data
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42
)

# Train a complex model (Random Forest)
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)

# Create a mesh for visualization
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(
    np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100)
)
mesh_points = np.c_[xx.ravel(), yy.ravel()]

# Get predictions for the mesh
Z = rf_model.predict_proba(mesh_points)[:, 1]
Z = Z.reshape(xx.shape)
In[5]:
Code
# Prepare data for visualization
instance_to_explain = X_test[0]

# Generate perturbations around the instance
n_samples = 100
np.random.seed(42)
perturbations = np.random.normal(0, 0.5, (n_samples, 2))
perturbed_instances = instance_to_explain + perturbations

# Get predictions for perturbed instances
perturbed_predictions = rf_model.predict_proba(perturbed_instances)[:, 1]

# Calculate distances and weights
distances = np.linalg.norm(perturbed_instances - instance_to_explain, axis=1)
weights = np.exp(-(distances**2) / 0.5**2)  # Exponential kernel

# Fit local linear model
from sklearn.linear_model import LinearRegression

local_model = LinearRegression()
local_model.fit(
    perturbed_instances, perturbed_predictions, sample_weight=weights
)

# Create local decision boundary
# Clip predictions to [0,1] to match probability range
Z_local = np.clip(local_model.predict(mesh_points), 0, 1).reshape(xx.shape)
Out[6]:
Visualization
Random Forest decision-boundary plot with blue circular class-zero points, coral triangular class-one points, a dashed probability-0.5 boundary, and a coral star for the instance being explained.
Complex model decision boundary showing the classification regions learned by the Random Forest. The dashed line represents the decision boundary at probability 0.5. The coral star marks the specific instance we want to explain.
Local LIME neighborhood with perturbations colored by predicted class-one probability, a probability color scale, the explained instance marked by a coral star, and an annotation that the local 0.5 threshold lies below the displayed neighborhood.
Local linear approximation around the instance of interest. Perturbed instances are colored by predicted class-1 probability. The coral threshold marker indicates that the local model's 0.5 decision boundary lies below this neighborhood, while the coral star marks the explained instance.

Now let's visualize the feature importance weights that LIME would extract:

In[7]:
Code
# Extract feature importance
feature_importance = np.abs(local_model.coef_)
feature_names = ["Feature 1", "Feature 2"]
Out[8]:
Visualization
Vertical bar chart comparing the absolute LIME weights for Feature 1 and Feature 2, with the exact value labeled above each bar.
LIME feature importance weights showing the relative contribution of each feature to the prediction. Higher values indicate stronger influence on the model's decision for this specific instance.
Out[9]:
Console
Local model coefficients: [-0.01646485  0.11526528]
Local model intercept: 0.938
Complex model prediction: 0.980
Local model prediction: 0.979

The local model coefficients show how much each feature contributes to the prediction in this neighborhood. The intercept represents the baseline prediction. We can compare the complex model's prediction with the local model's prediction to see how well our linear approximation captures the model's behavior. If they're close, our explanation is trustworthy for this instance.