LIME (Local Interpretable Model-agnostic Explanations) › Implementation using the Official LIME Package
Machine Learning from ScratchAll chapters 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:
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
)# 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]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.460The 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:
# 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
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_widthcontrols 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 anExplanationobject.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.