Explainable AI in Python
Fouad Trad
Machine Learning Engineer
Derives SHAP values for any model
Slower than type-specific explainers
age | sex | chest_pain_type | blood_pressure | ecg_results | thalassemia | target |
---|---|---|---|---|---|---|
52 | 1 | 0 | 125 | 1 | 3 | 0 |
53 | 1 | 0 | 140 | 0 | 3 | 0 |
70 | 1 | 0 | 145 | 1 | 3 | 0 |
61 | 1 | 0 | 148 | 1 | 3 | 0 |
62 | 0 | 0 | 138 | 1 | 2 | 0 |
mlp_clf
: multilayer perceptron predicting risk of heart disease
age | gender | bmi | children | smoker | charges |
---|---|---|---|---|---|
19 | 0 | 27.900 | 0 | 1 | 16884.92 |
18 | 1 | 33.770 | 1 | 0 | 1725.55 |
28 | 1 | 33.000 | 3 | 0 | 4449.46 |
33 | 1 | 22.705 | 0 | 0 | 21984.47 |
32 | 1 | 28.880 | 0 | 0 | 3866.85 |
mlp_reg
: multilayer perceptron predicting insurance charges
import shap
explainer = shap.KernelExplainer( # Model's prediction function, # Representative summary of dataset )
import shap
explainer = shap.KernelExplainer( # Model's prediction function, # Representative summary of dataset )
import shap
explainer = shap.KernelExplainer(
mlp_reg.predict,
# Representative summary of dataset
)
import shap
explainer = shap.KernelExplainer(
mlp_clf.predict_proba,
# Representative summary of dataset
)
import shap explainer = shap.KernelExplainer( mlp_reg.predict, shap.kmeans(X, 10) )
shap_values_reg = explainer.shap_values(X)
import shap explainer = shap.KernelExplainer( mlp_clf.predict_proba, shap.kmeans(X, 10) )
shap_values_cls = explainer.shap_values(X)
mean_reg = np.abs(shap_values_reg).mean(axis=0)
plt.bar(X.columns, mean_reg)
mean_cls = np.abs(shap_values_cls[:,:,1]).mean(axis=0)
plt.bar(X.columns, mean_cls)
plt.bar(X.columns, np.abs(lin_reg.coef_))
plt.bar(X.columns, np.abs(log_reg.coef_[0]))
Explainable AI in Python