SHAP explainability

Explainable AI in Python

Fouad Trad

Machine Learning Engineer

Introduction to SHAP

  • SHAPSHapley Additive exPlanations
  • Model-agnostic technique
  • Uses shapely values from game theory
  • SHAP values → quantify feature contributions to predictions

SHAP logo

Explainable AI in Python

Splitting profit among band members

Image for a musical band.

Explainable AI in Python

SHAP explainers

 

  • Compute SHAP values
  • Tree explainers → tree-based models

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

Admissions dataset

GRE Score TOEFL Score University Rating SOP LOR CGPA Chance of Admit Accept
337 118 4 4.5 4.5 9.65 0.92 1
324 107 4 4 4.5 8.87 0.76 1
316 104 3 3 3.5 8 0.72 1
322 110 3 3.5 2.5 8.67 0.8 1
314 103 2 2 3 8.21 0.45 0

 

  • rf_reg: predicts chance of admit
  • rf_class: predicts acceptance
  • X: dataframe containing features
Explainable AI in Python

Deriving SHAP values

import shap
Regression
explainer_reg = shap.TreeExplainer(rf_reg)

shap_values_reg = explainer_reg.shap_values(X)
Classification
explainer_class = shap.TreeExplainer(rf_class)

shap_values_class = explainer_class.shap_values(X)
Explainable AI in Python

Understanding SHAP output

Regression
print(shap_values_reg.shape)  
(400, 6)

Image showing SHAP values as a table having the same dimensions of the X dataframe (number of samples, number of features)

Explainable AI in Python

Understanding SHAP output

Regression
print(shap_values_reg.shape)  
(400, 6)

Image showing SHAP values as a table having the same dimensions of the X dataframe (number of samples, number of features)

Classification
print(shap_values_class.shape)
(400, 6, 2)

Select values of positive class

positive_values = shap_values_class[:,:,1]
Explainable AI in Python

Feature importance

mean_shap_values_class = np.abs(shap_values_class[:, :, 1]).mean(axis=0)
mean_shap_values_reg = np.abs(shap_values_reg).mean(axis=0)

plt.bar(X_train.columns, mean_shap_values_class) plt.bar(X_train.columns, mean_shap_values_reg)

Image showing feature importances for regression and classification, highlighting that CGPA and GRE scores are the most influential factors in predicting admissions.

Explainable AI in Python

Let's practice!

Explainable AI in Python

Preparing Video For Download...