Create visualizations of ML models with Shap

Nowadays we like to look at graphics or recordings. They draw our attention much more than a wall of text. The situation is similar when creating algorithms and machine learning models. Today I will introduce you to the Python library which is used for that purpose. Create visualizations of ML models with Shap.

Shap

Initially, it is worth explaining what this library is based on and what is behind its name. SHAP (Shapley Additive explanations) is a game theoretic approach to explaining the results of any machine learning model. It combines optimal credit allocation with local explanations using classical Shapley values from game theory and related extensions.

Shap library

Now let’s move on to write a few words of introduction about the library itself. It is a library that uses SHAP values to explain the output of any machine learning model. It is also notable for being actively used by thousands of data scientists representing a wide variety of organisations. Moreover, it has fast integration of algorithms from scikit-learn, TensorFlow and PyTorch libraries.

Example usage

First of all, you need to install that package:

pip install shap

If you haven’t installed such packages as pandas, scipy or slicer yet, then they will be installed at that moment.

Let’s try to use this lovely library. We will apply the Adaptive Boosting Classifier (I introduced this classifier some time ago) and the well-known dataset Breast Cancer:

Firstly, we need to add our dataset and then prepare the model. The next part is about explaining the model’s predictions using SHAP. In the end, we are ready to visualize the prediction’s explanation (in this case, it will be a waterfall diagram).

Waterfall Diagram

Waterfall Diagram presents the importance of features in our model. You can see that below:

The interpretation of the diagram is very simple – it shows the features that each contributes to pushing the model output from the baseline (the average model output by the provided training data set) to the model output. Features pushing the forecast higher are shown in red, and those pushing the forecast lower are shown in blue.

Summary

Shap is a remarkably useful library for visualising machine learning models. It contains a broad variety of options, which you can check out here. If you haven’t dealt with it yet, I encourage you to do so! Have fun and create visualizations of ML models with Shap.

Scroll to top