SHAP kernel explainer

Explainable AI in Python

Fouad Trad

Machine Learning Engineer

SHAP kernel explainer

 

  • Derives SHAP values for any model

    • K-nearest neighbors
    • Neural networks
    • Tree-based models
  • Slower than type-specific explainers

Image showing that SHAP explainers are divided between general explainers that can be applied to any model and type-specific explainers which are optimized for specific model types.

Explainable AI in Python

Heart disease

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

Explainable AI in Python

Insurance charges

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

Explainable AI in Python

Creating kernel explainers

MLPRegressor
import shap


explainer = shap.KernelExplainer( # Model's prediction function, # Representative summary of dataset )
MLPClassifier
import shap


explainer = shap.KernelExplainer( # Model's prediction function, # Representative summary of dataset )
Explainable AI in Python

Creating kernel explainers

MLPRegressor
import shap

explainer = shap.KernelExplainer(
  mlp_reg.predict, 
  # Representative summary of dataset
)


MLPClassifier
import shap

explainer = shap.KernelExplainer(
  mlp_clf.predict_proba, 
  # Representative summary of dataset
)


Explainable AI in Python

Creating kernel explainers

MLPRegressor
import shap

explainer = shap.KernelExplainer(
  mlp_reg.predict, 
  shap.kmeans(X, 10)
)


shap_values_reg = explainer.shap_values(X)
MLPClassifier
import shap

explainer = shap.KernelExplainer(
  mlp_clf.predict_proba, 
  shap.kmeans(X, 10)
)


shap_values_cls = explainer.shap_values(X)
Explainable AI in Python

Feature importance

MLPRegressor
mean_reg = np.abs(shap_values_reg).mean(axis=0)

plt.bar(X.columns, mean_reg)

Image showing a bar plot for feature importances in the regression task, highlighting that smoking and age are the most influential factors in predicting charges.

MLPClassifier
mean_cls = np.abs(shap_values_cls[:,:,1]).mean(axis=0)

plt.bar(X.columns, mean_cls)

Image showing a bar plot for feature importances in the classification task, highlighting that chest pain type and thalassemia are the most influential factors in predicting charges.

Explainable AI in Python

Comparing with model-specific approaches

Linear regression
plt.bar(X.columns, np.abs(lin_reg.coef_))

Image showing a bar plot for feature importances in the using a linear regression model, highlighting that smoking and age are the most influential factors in predicting charges.

Logistic regression
plt.bar(X.columns, np.abs(log_reg.coef_[0]))

Image showing a bar plot for feature importances in the classification task, highlighting that chest pain type is the most influential factors in predicting charges.

Explainable AI in Python

Let's practice!

Explainable AI in Python

Preparing Video For Download...