This program trains a random forest on the iris dataset. Then it uses SHAP and LIME to explain the prediction for the first test sample. It prints the importance of each feature according to both methods.
import numpy as np
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import shap
from lime import lime_tabular
# Load data
iris = load_iris()
X, y = iris.data, iris.target
feature_names = iris.feature_names
class_names = iris.target_names
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# Train model
model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)
# SHAP explanation
explainer_shap = shap.Explainer(model, X_train)
shap_values = explainer_shap(X_test[0:1])
# Print SHAP values for first test sample
print('SHAP values for first test sample:')
for name, val in zip(feature_names, shap_values.values[0]):
print(f'{name}: {val:.4f}')
# LIME explanation
explainer_lime = lime_tabular.LimeTabularExplainer(X_train, feature_names=feature_names, class_names=class_names, mode='classification')
exp = explainer_lime.explain_instance(X_test[0], model.predict_proba)
print('\nLIME explanation for first test sample:')
for feature, weight in exp.as_list():
print(f'{feature}: {weight:.4f}')