LIME (Local Interpretable Model-agnostic Explanations) › Visualizing LIME
Machine Learning from ScratchAll chapters 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.
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)# 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)

Now let's visualize the feature importance weights that LIME would extract:
# Extract feature importance
feature_importance = np.abs(local_model.coef_)
feature_names = ["Feature 1", "Feature 2"]
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.