Show Menu
Cheatography

Explainable AI Cheat Sheet (DRAFT) by

All XAI visualizastion gathered in one place! examples and code taken from https://github.com/slundberg/shap

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Before you start - SHAP

import xgboost
import shap

X, y = shap.datasets.boston()
model = xgboost.XGBRegressor().fit(X, y)

explainer = shap.Explainer(model)
shap_values = explainer(X)

Waterfall Chart

Used to see contri­butions of different atributes for the predic­tion. These SHAP values are valid for this observ­ation only. With other data points the SHAP values will change.

shap.p­lot­s.f­orc­e(s­hap­_va­lue­s[0])

Force Plot

Exactly the same purpose as the waterfall chart but much more compact

shap.p­lot­s.f­orc­e(s­hap­_va­lue­s[0])

SHAP Summaries

If you take force plots for all observ­ations, rotate them by 90 degrees and then put next to each other you obtain a SHAP summary plot. This is very useful if you want te see explan­ations for the entire dataset.

shap.p­lot­s.f­orc­e(s­hap­_va­lues)

SHAP Beeswarm

Useful to see which attributes are the most important. For every feature and every sample we plot a dot. We denote value of the feature with color: big (red) or small (blue). On the X-axis we see the import­ance. From this plot we see that LSTAT is probably the most important attribute. Also, high value of RM increases the model prediction

shap.p­lot­s.b­ees­war­m(s­hap­_va­lues)

Feature Intera­ction

This one is helpful to capture feature intera­ction and how they influence SHAP value for given feature. On X and Y axis we have inform­ation about attribute we are interested in. Color represents value of another feature that is intera­cting with consid­ered. From here we see that if RAD is small then RM have quite big impact on the prediction whereas when RAD is big then this impact is much smaller.

shap.p­lot­s.s­cat­ter­(sh­ap_­val­ues­[:,­"­RM"], color=­sha­p_v­alues)

SHAP for text

We can extend this idea to text and see how particular words influence the predic­tion.

SHAP for images

This can be also used for images to see the influence of individual pixels.
 

Before you start - OTHER XAI PLOTS

from sklearn.linear_model import LinearRegression

import plotly.express as px
import dalex as dx

linarModel = LinearRegression().fit(scale(X), y)

boston_rf_exp = dx.Explainer(model, X, y, label="Boston houses RF Pipeline")

Break down plot

This plot shows the decomp­osition of the model's prediction into contri­butions of different attributes

bd = boston­_rf­_ex­p.p­red­ict­_pa­rts­(house, type='­bre­ak_­down')

bd.plot()

Permut­ation importance

This functions function calculates the feature importance of estimators for a given dataset for given evaluation metrics. Can be visualized on bar chart.

r = permut­ati­on_­imp­ort­anc­e(m­odel, X, y)

Tree models feature importance

Tree algorithms offer importance scores based on the reduction in the evaluation criterion, like Gini or entropy. Can be used either in regression or classi­fic­ation problems in decision trees, random forests or boosting methods.

px.bar­(x=­X.c­olumns, y=mode­l.f­eat­ure­_im­por­tan­ces_)

Ceteris paribus profiles (partial dependence plot)

This figure shows how different attributes in a new instance can change a prediction of the model.
In a nutshell, we held all explan­atory variables but one (can increase this but comput­ational const increases by much) constant. Then we change the values of one selected and see how the response changes.

cp = titani­­c_­r­f­_e­­xp.p­­r­e­d­ic­­t_p­­ro­f­i­le­­(house)

cp.plo­­t(­v­a­ri­­abl­­es­=­[­'NOX', 'RM', 'DIS', 'LSTAT'])

Linear model feature importance

After scaling features we can measure how each attribute is important for the model

px.bar­­(y­=­a­bs­­(l­­ina­­rM­o­d­el.c­o­­ef_), x=X.co­­lumns)

Ceteris paribus profiles (partial dependence plot)

This figure shows how different attributes in a new instance can change a prediction of the model.
In a nutshell, we held all explan­­atory variables but one (can increase this but comput­­at­ional const increases by much) constant. Then we change the values of one selected and see how the response changes.

cp = titani­­­c­_­r­­f­­_e­­­xp.p­­­r­­e­d­­ic­­­t­_p­­­ro­­f­­i­l­e­­­(house)

cp.plo­­­t­(­v­­a­­ri­­­ab­l­­­es­­=­[­­'NOX', 'RM', 'DIS', 'LSTAT'])