What is Explainable AI (XAI)?

towards-data-science

This post was originally published by Nicklas Ankarstad at Towards Data Science

Explainability Spectrum

import requests
import pandas as pd
from io import StringIO
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
import shap
import xgboost as xgb
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
shap.initjs()
import lime
import lime.lime_tabular
import matplotlib.pylab as pl
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_auc_score
from sklearn.metrics import accuracy_score## Read in the file
df = pd.read_excel('https://archive.ics.uci.edu/ml/machine-learning-databases/00350/default%20of%20credit%20card%20clients.xls',header = 1)## Store target in y variable and split to train test
y = df['default payment next month'] 
df = df.drop(['default payment next month','ID'], axis =1) 
X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.20, random_state=42)## Train XGBoost Model
xgb_model = XGBClassifier(learning_rate = 0.05, verbosity = 1, n_estimators = 200 ,scale_pos_weight =4, random_state = 2456 )
xgb_model.fit(X_train, y_train)## Predict
y_pred = xgb_model.predict(X_test) roc_auc_score(y_test,y_pred)
## Plot importance (cover)
xgb.plot_importance(xgb_model, importance_type="cover")
pl.title('xgboost.plot_importance(model, importance_type="cover")')
pl.show()## Plot importance (gain)
xgb.plot_importance(xgb_model, importance_type="gain")
pl.title('xgboost.plot_importance(model, importance_type="gain")')
pl.show()

Local Interpretability

xgb_model.fit(X_train.as_matrix(), y_train)
predict_fn_xgb = lambda x: xgb_model.predict_proba(x).astype(float)
explainer = lime.lime_tabular.LimeTabularExplainer(X_train.to_numpy(), feature_names = X_train.columns)
exp = explainer.explain_instance(X_test.to_numpy()[observation_1], predict_fn_xgb, num_features = 9)
exp.show_in_notebook()

Shapley Values (SHAP)

## Calculate SHAP Values
explainer = shap.TreeExplainer(xgb_model)
expected_value = explainer.expected_value## Generate forceplot
shap.force_plot(expected_value,shap_values[observation_1],features_display.iloc[observation_1] ,X_test.columns,link='logit', matplotlib=True)

SHAP Local Importance Plots

shap.summary_plot(shap_values, X_test)

SHAP Variable Importance Plots

from interpret.glassbox import ExplainableBoostingClassifier
from interpret import show## Train Model
ebm = ExplainableBoostingClassifier(scoring = 'auc')
ebm.fit(X_train, y_train)## Calculate Global Explaination
ebm_global = ebm.explain_global()## Plot Global Variable Importance
show(ebm_global)

EBM Overall Feature Importance

## Local explainations
ebm_local = ebm.explain_local(X_test, y_test)
show(ebm_local)

EBM Dashboards

When should you use what?

Spread the word

This post was originally published by Nicklas Ankarstad at Towards Data Science

Related posts