Explain Iris classification by SHAP
Libraries¶
In [8]:
import sklearn
from sklearn.model_selection import train_test_split
import numpy as np
import shap
import time
shap.initjs()
Load Iris Data¶
In [2]:
X_train, X_test, Y_train, Y_test = train_test_split(
*shap.datasets.iris(), test_size=0.2, random_state=0)
In [3]:
# Predictor
X_train.head()
Out[3]:
In [19]:
# Label
Y_train[:5]
Out[19]:
Train K-nearest neighbors¶
In [4]:
clf = sklearn.neighbors.KNeighborsClassifier()
clf.fit(X_train, Y_train)
Out[4]:
Create an explainer¶
In [10]:
explainer = shap.KernelExplainer(clf.predict_proba, X_train)
Use summarized X by k-measn¶
In [21]:
X_train_summary = shap.kmeans(X_train, 50)
explainer = shap.KernelExplainer(clf.predict_proba, X_train_summary)
Explain one test prediction¶
In [22]:
shap_values = explainer.shap_values(X_test.iloc[0, :])
shap.force_plot(explainer.expected_value[0], shap_values[0], X_test.iloc[0, :])
Out[22]:
Comments
Comments powered by Disqus