Push the limits of explainability — an ultimate guide to SHAP library
This article is a guide to the advanced and lesser-known features of the python SHAP library. It is based on an example of tabular data classification.
But first, let’s talk about the motivation and interest in explainability at Saegus that motivated and financed my explorations.
Explainability — the theory
The explainability of algorithms is taking more and more place in the discussions about Data Science. We know that algorithms are powerful, we know that they can assist us in many tasks: price prediction, document classification, video recommendation.
From now on, more and more questions are being asked about this prediction: - Is it ethical? - Is it affected by bias? - Is it used for the right reasons?
In many domains such as medicine, banking or insurance, algorithms can be used if, and only if, it is possible to trace and explain (or better, interpret) the decisions of these algorithms.
Vocabulary parenthesis
In this article we would like to distinguish the terms :
Explainability: possibility to explain from a technical point of view the prediction of an algorithm.
Interpretability: the ability to explain or provide meaning in terms that are understandable by a human being.
Transparency: a model is considered transparent if it is understandable on its own.
Why do we care about interpretability ?
Interpretability helps to ensure impartiality in decision-making, i.e. to detect and therefore correct biases in the training data set. In addition, it facilitates robustness by highlighting potential adverse disturbances that could change the prediction. It can also act as an assurance that only significant features infer the outcome.
Sometimes, it would be more advisable to abandon the machine learning approach, and use deterministic algorithms based on rules justified by industry knowledge or legislation [1].
Nevertheless, it is too tempting to access the capabilities of machine learning algorithms that can offer high accuracy. We can talk about the trade-off between accuracy and explainability. This trade-off consists in discarding more complex models such as neural networks for simpler algorithms that can be explained.

To achieve these goals, a new field has emerged: XAI (Explainable Artificial Intelligence), which aims to produce algorithms that are both powerful and explainable.
Many frameworks have been proposed to help explain non-transparent algorithms. A very good presentation of these methods can be found in the Cloudera white paper [3].
In this article we will deal with one of the most used frameworks: SHAP.
XAI, who’s it for?
Different profiles interested in expainability or interpretability have been identified:
- Business expert/model user — in order to trust the model, understand the causality of the prediction
- Regulatory bodies to certify compliance with the legislation, auditing
- Managers and executive board to assess regulatory compliance, understand enterprise AI applications
- Users impacted by model decisions in order to understand the situation, verify decisions
- Data scientist, developer, PO to ensure/improve product performance, find new features, explain functioning/predictions to superiors
In order to make explainability accessible to people with low technical skills, first of all, the creator: a data scientist/developer must be comfortable with the tools of explainability.
The data scientist will use them above all to understand and improve his model and then to communicate with his superiors and regulatory bodies. Recently, explainability tools have become more and more accessible.
For example, Dataiku — ML’s platform — has added in its latest version 7.0 published on March 2, 2020 explainability tools: Shapley values and “The Individual Conditional Expectation” (ICE).

Azure ML proposes its own version of Shap and alternative tools adding interactive dashboards.

There are also open-source webapps such as this one described in the medium article [4] that facilitate the exploration of the SHAP library.
These tools, very interesting to get a quick overview of interpretation, do not necessarily give an understanding of the full potential of the SHAP library. Few allow to explore interaction values or to use different background or display sets.
I investigated the SHAP framework and I present you my remarks and the usage of less known features, available in the official version of the library in open source. I also propose some interactive visualizations easy to integrate in your projects.
Explainability — the practice
Most data scientists have already heard of the SHAP framework. In this post, we won’t explain in detail how the calculations behind the library are done. Many resources are available online such as the SHAP documentation [5], publications by authors of the library [6,7], the great book “Interpretable Machine Learning” [8] and multiple medium articles [9,10,11].
In summary, Shapley’s values calculate the importance of a feature by comparing what a model predicts with and without this feature. However, since the order in which a model sees the features can affect its predictions, this is done in all possible ways, so that the features are compared fairly. This approach is inspired by game theory.
Having worked with many clients, for example in the banking and insurance sectors, one can see that their data scientists are struggling to exploit the full potential of SHAP. They don’t know how this tool could really be useful for understanding a model and how to use it to go beyond simply extracting the importance of features.
The devil is in the detail
SHAP comes with a set of visualizations that are quite complex and not always intuitive, even for a data scientist.
On top of that, there are several technical nuances to be able to use SHAP with your data. Francesco Porchetti’s blog article [12] expresses some of these frustrations by exploring the SHAP, LIME, PDPbox (PDP and ICE) and ELI5 libraries.
At Saegus, I worked on a course which aims to give more clarity to the SHAP framework and to facilitate the use of this tool.
In this post I would like to share with you some observations collected during that process.
SHAP is used to explain an existing model. Taking a binary classification case built with a sklearn model. We train, tune and test our model. Then we can use our data and the model to create an additional SHAP model that explains our classification model.

Vocabulary
It is important to understand all the bricks that make up a SHAP explanation.
Often, by using default values for parameters, the complexity of the choices we make remains obscure.
global explanations explanations of how the model works from a general point of view
local explanations explanations of the model for a sample (a data point)
explainer (shap.explainer_type(params)) type of explainability algorithm to be chosen according to the model used.
The parameters are different for each type of model. Usually, the model and training data must be provided, at a minimum.
base value (explainer.expected_value) E(y_hat) is “the value that would be predicted if we didn’t know any features of the current output” is the mean(y_hat) prediction for the training data set or the background set. We can call it “reference value”, it’s a scalar (n).
It’s important to choose your background set carefully — if we have the unbalanced training set this will result in a base value placed among the majority of samples. This can also be a desired effect: for example if for a bank loan we want to answer the question: “how is the customer in question different from customers who have been approved for the loan” or “how is my false positive different from the true positives”.
# equilibrated casebackground = X.sample(1000) #X is equilibrated# background used in explainer defines base value
explainer = shap.TreeExplainer(xgb_model,background,model_output="raw" )shap_values = explainer.shap_values(X)# background used in the plot, the points that are visible on the plotshap.summary_plot(shap_values,background, feature_names=background.columns)…
# base value shiftedclass1 = X.loc[class1,:] #X is equilibrated# background from class 1 is used in explainer defines base value
explainer = shap.TreeExplainer(xgb_model,class1,model_output="raw" )shap_values = explainer.shap_values(X)# points from class 0 is used in the plot, the points that are visible on the plotshap.summary_plot(shap_values,X.loc[class0,:], feature_names=X.columns)
SHAPley values (explainer.shap_values(x)) the average contribution of each feature to each prediction for each sample based on all possible features. It is a (n,m) n — samples, m — features matrix that represents the contribution of each feature to each sample.
output value (for a sample) the value predicted by the algorithm (the probability, logit or raw output values of the model)
display features (n x m) a matrix of original values — before transformation/encoding/engineering of features etc. — that can be provided to some graphs to improve interpretation. Often overlooked and essential for interpretation.
____
SHAPley values
Shapley values remain the central element. Once we realize that this is simply a matrix with the same dimensions as our input data and that we can analyze it in different ways to explain the model and not only. We can reduce its dimensions, we can cluster it, we can use it to create new features. An interesting exploration described in the article [12] aims at improving anomaly detection using auto encoders and SHAP. The SHAP library proposes a rich but not exchaustive exploration through visualizations.
Visualizations
The SHAP library offers different visualizations. A good explanation on how to read the colors of the summary plot can be found in this medium article [14].

The summary plot shows the most important features and the magnitude of their impact on the model. It can take several graphical forms and for the models explained by TreeExplainer we can also observe the interaction values using the “compact dot” with shap_interaction_values in input.
The dependency plot allows to analyze the features two by two by suggesting a possibility to observe the interactions. The scatter plot represents a dependency between a feature(x) and the shapley values (y) colored by a second feature(hue).
On a personal note, I find that an observation of a three-factor relationship at the same time is not intuitive for the human brain (at least mine). I also doubt that an observation of dependency by observing colours can be scientifically accurate. Shap can give us an interaction relationship that is calculated as a correlation between the shapley values of the first feature and the values of the second feature. If possible (for TreeExplainer) it makes more sense to use the shapley interaction values to observe interactions.


























