Note: AI Generated, and augmented by me.
pip install scikit-learn xgboost shap lime matplotlib pandas
import numpy as np
import pandas as pd
import shap
import lime
import lime.lime_tabular
import matplotlib.pyplot as plt
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
# ✅ Load dataset
data = load_diabetes()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = data.target
# ✅ Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# ✅ Train XGBoost regressor
model = XGBRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# ✅ Predict on test set
preds = model.predict(X_test)
# ==============================================
# 🔷 Part A: SHAP Values
# ==============================================
# ✅ Initialize SHAP explainer
explainer = shap.Explainer(model, X_train)
# ✅ Explain predictions on test set
shap_values = explainer(X_test)
# ✅ Visualize feature importance (global)
shap.plots.beeswarm(shap_values)
# ✅ Explain a single prediction (local)
shap.plots.waterfall(shap_values[0])
# ==============================================
# 🔷 Part B: LIME
# ==============================================
# ✅ Initialize LIME explainer
lime_explainer = lime.lime_tabular.LimeTabularExplainer(
X_train.values,
feature_names=X_train.columns.tolist(),
verbose=True,
mode='regression'
)
# ✅ Explain a single instance
i = 0 # index of sample to explain
lime_exp = lime_explainer.explain_instance(
X_test.values[i],
model.predict,
num_features=5
)
# ✅ Visualize LIME explanation
lime_exp.show_in_notebook(show_table=True)
# If running in script, use
# lime_exp.as_pyplot_figure()
# plt.show()
- SHAP provides both global and local explanations with theoretical guarantees based on Shapley values (fair feature attribution). - LIME builds a local surrogate model to approximate the black-box behavior near a specific prediction, giving intuitive explanations quickly. - Limitations: SHAP can be computationally heavy; LIME explanations may vary due to random perturbations.
- Both are widely used for model interpretability in regulated industries like life sciences, where explainability is critical for compliance and stakeholder trust.