Explainable Artificial Intelligence (XAI) is an important subfield of AI that focuses on making the decision-making processes of machine learning models understandable to humans. This is especially important for black-box models, such as ensemble or deep learning algorithms, which are too complex even for experts to understand (see also my article on Explainable Artificial Intelligence). A better understanding of such black-box models leads, among other things, to more trust among users and gives developers hints on how to improve a model.
A popular method of Explainable Artificial Intelligence is SHAP (SHapley Additive ExPlanations). SHAP can be applied to any machine learning model and explains how individual features affect a model’s predictions. This series of articles will give you an easy-to-understand introduction to SHAP and the game-theoretic concept of Shapley values that is central to it. You will also learn how to explain the predictions of black-box models using the Python library SHAP with a practical programming example. The series consists of 3 articles:
- How to better understand black box models with Shapley values?
- The ultimate guide that makes mastering SHAP easy
- A comprehensive Python tutorial for quickly diving into SHAP
This is the third article in the series. In this article, you will learn how to use the Python library SHAP to explain machine learning models. Through a simple programming example, you will learn how to compute and interpret feature attributions using the Python library SHAP. You will also learn about the visualizations provided by the SHAP library and how to use them for local and global explanations. At the end of this article, you will have a solid understanding of the functionality of the Python library SHAP and how it can help you better understand black box models. You can also find the Python code for this article on GitHub or in this Google Colab notebook.
Overview
This blog post is divided into the following sections:
- What is the Python library SHAP?
- Training an XGBoost model on the California Housing dataset
- How to calculate feature attributions using the Python library SHAP?
- How to create local explanations using the Python library SHAP?
- How to create global explanations using the Python library SHAP?
What is the Python library SHAP?
SHAP is a collective term for several methods for estimating Shapley values. Shapley values help to better understand how a machine learning model arrives at its decisions. Shapley values provide information not only about the importance of features, but also about how individual features affect a model’s predictions. The contributions of features to a model’s prediction are also referred to as feature attributions.
Shapley values can be used for both local and global explanations. While local explanations help to understand individual predictions of a model, global explanations help to understand the overall behavior of a model. Furthermore, SHAP is a model-agnostic explanation method, meaning that it can be applied to any machine learning algorithm.
In the following, we use the Python library SHAP to compute Shapley values for a regression model predicting house prices. However, the Python library SHAP can also be used for classification models. In addition to implementing various SHAP methods, the library also provides numerous visualizations. These visualizations can be used to generate both local and global explanations. The main features of the Python library SHAP are shown in Figure 1.

Training an XGBoost model on the California Housing dataset
The machine learning algorithm whose decision-making process we will explain in this tutorial is an XGBoost (Extreme Gradient Boosting) model. XGBoost is a fast and powerful ensemble method that uses decision trees as weak learners and combines their predictions into an accurate prediction model through gradient boosting. The XGBoost algorithm is a very popular machine learning method suitable for both regression and classification tasks.
Its popularity stems from the fact that XGBoost outperforms other machine learning methods on many benchmark datasets. For example, XGBoost has won more Kaggle competitions than any other machine learning algorithm. This makes XGBoost the state-of-the-art algorithm for structured data problems. It also makes the XGBoost algorithm perfectly suited for the dataset we will be working with in this tutorial.
Install the required Python libraries
To use SHAP in Python, you must first install the Python library SHAP. You can do this using either pip or conda:
pip install shap
or
conda install -c conda-forge shap
The next step is to import all the libraries you need:
# We use the SHAP library to estimate and visualize Shapley values
import shap
# We use the XGBoost implementation from the xgboost library
import xgboost
# We use this function from the Scikit-learn library to split our dataset into a training dataset and a test dataset
from sklearn.model_selection import train_test_split
# We use this function from the Scikit-learn library to compute the mean squared error of the model's predictions
from sklearn.metrics import mean_squared_error
PythonWe need the SHAP library to compute the Shapley values. We also need the xgboost library for our XGBoost model. To split our data set into training and test data, we use the corresponding function from the sklearn library. Finally, we need another function from sklearn to compute the mean squared error (MSE) for our model.
The California Housing dataset
The dataset we use is the California Housing dataset. This dataset contains information on 20,640 block groups throughout California from 1990. Our goal is to train a regression model to estimate the natural logarithm of the median house price for different block groups. The eight features available to us for this purpose are listed in Table 1.
Feature name | Description |
---|---|
MedInc | Median income in the block group |
HouseAge | Median age of houses in the block group |
AveRooms | Average number of rooms per household |
AveBedrms | Average number of bedrooms per household |
Population | Number of residents in the block group |
AveOccup | Average number of household members |
Latitude | Latitude of the block group |
Longitude | Longitude of the block group |
The California Housing dataset is included directly in the Python library SHAP, making it easy to import:
# Load the California housing dataset
# X is the features and y is the targets
X, y = shap.datasets.california()
PythonX is the feature matrix and the vector y is the target variable containing the average house prices of the different block groups.
Training of the XGBoost model
To train our XGBoost model on this dataset, we first divide the dataset into a training dataset and a test dataset. While the training dataset is used to optimally calibrate the parameters of the XGBoost model, the test dataset is only used to evaluate the model. To split the dataset into a training and a test dataset, we use the “train_test_split” function from the sklearn library:
# Split the dataset into a training dataset and a test dataset, using 80% of the instances for the training dataset and 20% of the instances for the test dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
PythonBy choosing the parameter “test_size=0.2”, the dataset was partitioned so that the test dataset contains 20% of the instances of the original dataset and the training dataset contains the remaining 80% of the instances. By choosing a fixed value for the “random_state” parameter, the partitioning of the data set remains reproducible.
In the next step, we can use our training data set to train the XGBoost model. To do this, we first need to initialize an XGBoost model. Since this is a regression problem, we will use the XGBRegressor:
# Initialize an XGBoost model for regression tasks
model = xgboost.XGBRegressor()
PythonAfter initialization, we can easily train the model on our training data set using the fit function:
# Train the XGBoost model on the training dataset
model.fit(X_train, y_train)
PythonTo ensure that the model is not overfitting, we should evaluate the performance of the model on the test data. To do this, we first predict the average house prices for all block groups in the test dataset:
# Make predictions on the test dataset using the XGBoost model
predictions = model.predict(X_test)
PythonTo assess the deviation of the model predictions from the actual target variable, we calculate the mean square error (MSE):
# Calculate the prediction error for the test dataset using the mean squared error
mse = mean_squared_error(y_test, predictions)
print(f"MSE: {mse}")
PythonGreat, our model achieves an MSE of 0.21 on the test data!
How to calculate feature attributions using the Python library SHAP?
Now we can use the Python library SHAP to generate explanations for the trained XGBoost model to help us better understand the behavior of the model. There are several SHAP methods implemented in the SHAP library. The three most important ones are the Tree, Deep, and Kernel explainers.
The Tree Explainer class is an implementation of Tree SHAP and allows for fast and accurate computation of Shapley values for tree-based machine learning algorithms such as Decision Trees, Random Forests, and XGBoost. On the other hand, the Explainer class “Deep” corresponds to the Deep SHAP algorithm and is thus specialized for fast computation of Shapley values for deep learning models.
The Kernel SHAP algorithm is implemented in the Explainer class “Kernel” and allows the approximation of Shapley values for arbitrary machine learning algorithms. For example, it can be used to compute Shapley values for algorithms such as K-Nearest Neighbor (KNN). However, the kernel SHAP method is significantly slower than the other two model-specific Explainer classes.
In addition, there is the “Exact” explainer, which can be used to compute the exact Shapley values for a model. However, this is only useful for small data sets with less than 15 features. Otherwise, the calculation of the exact Shapley values is associated with very long computation times.
Choosing the right Explainer class
In general, you don’t have to worry much about the choice of SHAP algorithm. You can simply use the general Explainer class of the Python library SHAP to compute Shapley values for your model. This class will then, by default, choose the most appropriate SHAP method to approximate the Shapley values, depending on your model and the size of your data. The great thing about the SHAP library is that it is compatible with several machine learning frameworks, including sklearn, Tensorflow, and PyTorch.
To compute the Shapley values for our XGBoost model, we first need to initialize the SHAP explainer. For the model, we specify our XGBoost model (parameter “model”). For the “masker” parameter, we specify the data set from which the base value for the Shapley values will be estimated. As you may recall from my article on Shapley values, Shapley values are always relative to a base value. The base value is the average of all model predictions for the instances of the data set that we specify as the “masker” parameter when initializing the Explainer object. We use the training data set for this purpose.
# Initialize an explainer that estimates Shapley values using SHAP
# Here we use the training dataset X_train to compute the base value
explainer = shap.Explainer(model=model, masker=X_train)
PythonThe Explainer automatically selects an appropriate SHAP method for our model and data. You can view the selected method via the Explainer’s “__class__” attribute:
explainer.__class__
PythonAs you can see, the Tree SHAP algorithm was automatically selected. This makes sense because XGBoost is an ensemble of decision trees. For such models, Tree SHAP can approximate the Shapley values very quickly and with high accuracy.
Calculate Shapley values with SHAP-Explainer
The Explainer can be used to compute Shapley values either for individual instances or for entire data sets. In our case, we compute Shapley values for all instances of the test data set. To do this, we simply apply the Explainer to the test data set:
# Estimate the Shapley values for the test dataset
shap_values = explainer(X_test)
PythonAfter calculating the Shapley values, the Explainer returns an object we call “shap_values”. This object has the following attributes:
- shap_values.values: This is a matrix containing the Shapley values for each instance of the input data, i.e. in our case the Shapley values for all instances of the test data set. The matrix has the same dimensions as the test data set. The number of columns is equal to the number of features (8 in our case) and the number of rows is equal to the number of instances in our test data set. Each row of the matrix contains the Shapley values for exactly one instance of the test data set and its feature values.
- shap_values.base_values: This is a vector of base values. The dimension of the vector is equal to the number of instances in the input data. Since the base value is the same for all instances, all entries of this vector correspond to the same value. This value is the average of all predictions of the model for the data set specified in the “masker” parameter when the Explainer object was created (in our case, the training data set).
- shap_values.data: This is simply a copy of the input data, in our case the test data set.
We now want to use the calculated Shapley values to better understand the model behavior. We can use the Shapley values for both local and global explanations. Local explanations help us better understand the influence of different features on individual model predictions. Global explanations help us understand the overall behavior of the model. In the following, we will look at different visualizations of local and global explanations provided by the Python library SHAP.
How to create local explanations using the Python library SHAP?
Local explanations attempt to explain the predictions for individual instances of a data set. The Shapley values computed by SHAP are in themselves local explanations because they are computed separately for each prediction. They explain how a prediction for a particular instance is produced by the contributions of each feature. To better understand how the features affect an individual prediction of our XGBoost model, we can use visualizations of the Shapley values, such as waterfall plots and force plots.
Both waterfall and force plots are already implemented in the Python library SHAP. We will now use these visualizations to explain the model’s prediction for the first instance of the test data set (i.e., the first row of the test data set). The approximate Shapley values for this prediction correspond to the first entry in the matrix of Shapley values for the test data set, i.e., shap_values[0].
Waterfall plots
Waterfall plots are one way to visualize the Shapley values of a local explanation. One of the fundamental properties of Shapley values is that the sum of the base value and the Shapley values of each feature always equals the model prediction. Waterfall plots also show this additive nature of Shapley values, in addition to the positive and negative contributions of each feature. Thus, they show how the model prediction is achieved by adding the Shapley values of each feature, starting from the base value.
To create a waterfall plot of the Shapley values for the first instance of the test data set, we simply apply the appropriate plot function from the SHAP library to these Shapley values:
# Visualize the Shapley values for the prediction of the first instance in the test dataset using a waterfall plot
# The waterfall plot shows how we get from shap_values.base_values to model.predict(X_test)[0]
shap.plots.waterfall(shap_values[0])
Python
The waterfall plot in Figure 2 shows the contribution of the different features to get from the base value (E[f(x)] = 2.044) at the bottom of the plot to the model prediction for this instance (f(x) = 1.506) at the top of the plot. The final prediction is obtained by adding the Shapley values of each feature to the base value, i.e., 2.044 + 0.02 -0.03 + 0.05 -0.06 +0.1 -0.39 +1.2 – 1.43 = 1.506 (allowing for a small rounding error).
Interpreting the waterfall plot
The waterfall plot has the following structure:
- The values of the target variable (in our case, house price) are plotted on the x-axis.
- The feature names and associated feature values for the instance under consideration are plotted on the y-axis.
- The Shapley value of each feature for this instance is indicated by the length of the bar.
- The color indicates whether a feature makes a positive contribution (red) or a negative contribution (blue).
- Features are ordered in the waterfall plot by their absolute Shapley value. The absolute Shapley value indicates how much an individual feature influenced the prediction.
In the example above, by far the most important features are the location of the block group, i.e., longitude and latitude. While latitude contributes to an above-average prediction, longitude weakens this effect and contributes to a below-average prediction. The average number of household members also has a negative Shapley value (-0.39) and contributes to a below-average prediction.
The other features have a relatively small effect on this single prediction. It should be noted, however, that the Shapley values and the conclusions drawn from them are only valid for this one instance. Other instances in the data set may have completely different Shapley values.
Force plots
Another way to visualize the Shapley values for individual model predictions is to use force plots. Force plots contain the same information as waterfall plots, but present it in a more condensed form. Force plots can also be easily generated using the plot function of the Python library SHAP:
# For visualizations using force plots, the Javascript library must be loaded
shap.initjs()
# Visualize the Shapley values for the prediction of the first instance in the test dataset using a force plot
shap.plots.force(shap_values[0])
Python
In the force plot, the model prediction for this instance is highlighted and bolded. The plot shows how the influence of the negative and positive Shapley values led to this prediction, with the most important features and their feature values highlighted. On the left, the features with positive Shapley values are shown in red. On the right, the features with negative Shapley values are shown in blue.
The representation in the force plot thus corresponds to a balance of power between features with positive Shapley values and features with negative Shapley values. Features with positive Shapley values correspond to a force pulling to the left, and features with negative Shapley values correspond to a force pulling to the right. The final prediction is made at the point where the two forces are in equilibrium. Of course, this takes into account the base value, which is also shown in the force plot.
Force plots can also be used to display the Shapley values of multiple predictions simultaneously. In this way, force plots can also be used for global explanations, which we will discuss in the next section.
How to create global explanations using the Python library SHAP?
Next, we will use the Shapley values calculated for the test data set to understand the overall behavior of our XGBoost model. Global explanations help us understand how the model behaves as a whole for all predictions in a data set. In the case of the Shapley values, this allows us to identify the most important features and their impact on the predictions of the entire data set.
The computed Shapley values themselves initially represent only local explanations, i.e. they explain only individual predictions. However, global explanations for the entire model can be generated by calculating Shapley values for all instances of a data set and then aggregating or visualizing them in an appropriate way. For example, we can use bar plots, beeswarm plots, and force plots to analyze the global effects of features.
Bar plots
A simple way to get an overview of the overall behavior of a machine learning model is to graphically display the importance of each feature in the form of a bar plot. When the features are ordered by importance, you can quickly see which features have a large impact on the model’s predictions. You can easily create such a bar plot using the Python library SHAP by applying the appropriate plot function to the matrix containing all the Shapley values for our test data set:
# Visualize the Shapley values for the entire test dataset using a bar plot
shap.plots.bar(shap_values)
Python
The bar plot aggregates the Shapley values for each feature by calculating the average of the absolute Shapley values for that feature. The reasoning behind this is that features with high absolute Shapley values are important to the model’s predictions. It does not matter whether a feature has a positive or negative effect on the model’s predictions.
Structure of the bar plot
Since we are interested in the global importance of the features, we average the absolute Shapley values per feature over all instances of the test data set. The average absolute Shapley value for each feature is plotted in the graph to the right of the feature’s corresponding bar. The features are ranked in descending order by the magnitude of the average absolute Shapley value. This means that the features with the greatest influence on the model are plotted at the top of the bar plot, while the features with the least influence are plotted further down the bar plot.
In our example, we see that the latitude and longitude of the block groups have the greatest influence on the model. Latitude contributes an average of ±0.85 to the predicted house price, and longitude contributes ±0.72. In addition, the MedInc, AveOccup, and AveRooms features also have a greater influence on the model’s predictions. The influence of the other three features, on the other hand, is rather small on average.
Bar plots for data sets with many features
If you are working with a data set that contains a large number of features, a bar plot can quickly become long and confusing. In this case, you can use the “max_display” parameter to specify how many bars to display in the bar plot. The contributions of the less important features are then simply added up and displayed in a single bar:
# The max_display parameter can be used to set the maximum number of bars to be displayed in the bar plot
shap.plots.bar(shap_values, max_display=6)
Python
Bar plots provide an initial overview of the overall behavior of a model. However, they only provide information about the importance of individual features. In the following, we will look at beeswarm plots, which present the Shapley values for a data set in a much more meaningful way.
Beeswarm plots
Another way to better understand the overall behavior of a model is to plot the Shapley values as a beeswarm plot. Beeswarm plots are more complex than bar plots. However, they are also much more informative. Instead of aggregating the Shapley values of a data set, they plot the entire distribution of Shapley values for each feature. In this way, beeswarm plots not only provide information about the relative importance of each feature, but also show how different feature values affect model predictions.
Beeswarm plots are already implemented in the Python library SHAP. You can generate them using the following command:
# Visualize the Shapley values for the entire test dataset using a beeswarm plot
shap.plots.beeswarm(shap_values)
Python
Figure 6 shows the beeswarm plot for our example of California house prices.
Structure of the beeswarm plot
Beeswarm plots represent the Shapley values of the different features for all instances of a data set. This means that each point on the graph represents the Shapley value of a feature for a particular instance of the data set. The structure of the beeswarm plot is as follows:
- The features are plotted on the y-axis in descending order of global importance. Thus, the feature with the greatest influence on the model predictions is at the top, and the feature with the least influence is at the bottom.
- The Shapley values are plotted on the x-axis. The horizontal position of the points thus indicates how much influence they have on the model predictions, and whether they influence them positively or negatively.
- When plotting Shapley values, their density is also taken into account. That is, if Shapley values of the same magnitude occur for many instances (i.e., individual points overlap), the points are scattered in the direction of the y-axis. This gives a better understanding of the distribution of Shapley values for the different features.
- The magnitude of the feature values is highlighted in color in the beeswarm plot. Shapley values for instances with a high value for that feature (compared to the other instances) are shown in red. Shapley values for instances with low feature values are shown in blue.
Interpreting the beeswarm plot
Beeswarm plots can be used to identify correlations between the values of individual features and their influence on model predictions. Our example shows that a high average number of rooms per household (i.e., high values for “AveRooms”) is predominantly associated with positive Shapley values and thus generally contributes to above-average house price predictions.
In contrast, cases with low AveRooms values are predominantly associated with negative Shapley values and thus contribute to a below-average prediction. Thus, we can conclude that the AveRooms feature is positively correlated with predicted house prices. The opposite is true for latitude. Here, higher latitudes lead to lower predicted house prices.
In addition, the distribution of the dots provides further insight into the relationship between the feature values and the model predictions. For example, the feature “MedInc” shows a dense concentration of instances with low median income (blue dots), which have slightly negative Shapley values, i.e. a slightly negative impact on the predicted house price.
In contrast, instances with high median income (red dots) have predominantly positive Shapley values and are much more widely distributed on the x-axis. Thus, there are instances with a small positive impact on the predicted house price as well as instances with a very strong positive impact on the model prediction. We can conclude that a high median income has a stronger positive influence on the predicted house price than a low median income has a negative influence.
Force plots
As you already know, force plots can be used to visualize Shapley values for local explanations. However, force plots can also be used to generate global explanations. Global explanations show how the influence of negative and positive Shapley values led to the different predictions for the instances of a data set.
To create a global force plot, a local force plot is first created for each instance of a data set (as shown in Figure 3). All of these local force plots are then rotated 90 degrees and stacked horizontally. This creates a new force plot that combines the force plots of each instance and provides a global explanation for the model.
Force plots for global explanations can also be generated very easily using the corresponding plot function of the Python library SHAP. In the following, however, we will only generate the global force plot for the first 100 instances of the test dataset, since generating the plot for several thousand instances is very slow.
# Visualize the Shapley values for the entire test dataset using a force plot
shap.plots.force(shap_values[0:100])
Python
Figure 7 shows the force plot for our example.
Structure of the force plot
The force plot for global explanations from the Python library SHAP is interactive. The global force plot has the following structure:
- The values of the model predictions are displayed on the y-axis. However, because the plot is interactive, the values plotted on the y-axis can be changed. For example, instead of the values of the model predictions, the effects of each feature can be plotted on the y-axis.
- The x-axis shows the different instances. In our example, the x-axis ranges from 0 to 99, since we visualized the Shapley values for a total of 100 instances. By default, the horizontal position of the instances is ordered by their similarity in terms of their Shapley values. However, the order of the instances can be changed. Instances can also be sorted by their original order in the data set, by the value of their model prediction, or by the values of individual features.
- Different instances can be selected interactively. Once an instance is selected, the model prediction and the values of the most important features for that instance are displayed in addition to the instance index in the selected order. The original instance index can be viewed by clicking on the instance. The color of the instance’s feature values indicates whether the features have a negative Shapley value (blue) or a positive Shapley value (red).
- The Shapley values for the different features are highlighted in color in the global force plot. In the lower portion of the plot, those feature values that have positive Shapley values and thus improve the prediction are shown in red. In the upper portion of the plot, those features with negative Shapley values, which worsen the prediction, are shown in blue.
A force plot for global explanations allows you to interactively explore the relationships between the values of each feature and their influence on the model predictions. In this way, the force plot provides much more information than the bar plot or beeswarm plot. However, the information in the force plot is not readily apparent and takes more time to analyze.
Full Python code for this article
All lines of code in this tutorial are summarized in the code block below. It is also worth taking a look at the following Jupyter notebook on Github or this Google Colab notebook.
# # 1. Import all required libraries
# We use the SHAP library to estimate and visualize Shapley values
import shap
# We use the XGBoost implementation from the xgboost library
import xgboost
# We use this function from the Scikit-learn library to split our dataset into a training dataset and a test dataset
from sklearn.model_selection import train_test_split
# We use this function from the Scikit-learn library to compute the mean squared error of the model's predictions
from sklearn.metrics import mean_squared_error
# # 2. Load the sample dataset
# Load the California housing dataset (https://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.html)
# X is the features and y is the targets
X, y = shap.datasets.california()
# Split the dataset into a training dataset and a test dataset, using 80% of the instances for the training dataset and 20% of the instances for the test dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# # 3. Train an XGBoost model
# Initialize an XGBoost model for regression tasks
model = xgboost.XGBRegressor()
# Train the XGBoost model on the training dataset
model.fit(X_train, y_train)
# Make predictions on the test dataset using the XGBoost model
predictions = model.predict(X_test)
# Calculate the prediction error for the test dataset using the mean squared error
mse = mean_squared_error(y_test, predictions)
print(f"MSE: {mse}")
# # 4. Explain the XGBoost model using the SHAP library
# ## 4.1. Estimate the Shapley values
# Initialize an explainer that estimates Shapley values using SHAP
# Here we use the training dataset X_train to compute the base value
explainer = shap.Explainer(model=model, masker=X_train)
# As you can see below, the Tree SHAP algorithm is used to estimate the Shapley values
# Tree SHAP is a method specifically designed for tree models and tree ensembles that estimates Shapley values quickly and accurately.
explainer.__class__
# Estimate the Shapley values for the test dataset
shap_values = explainer(X_test)
# ## 4.2. Local explanations
# Local explanations are explanations for individual predictions of the model
# ### Waterfall Plot
# Visualize the Shapley values for the prediction of the first instance in the test dataset using a waterfall plot
# The waterfall plot shows how we get from shap_values.base_values to model.predict(X_test)[0]
shap.plots.waterfall(shap_values[0])
# ### Force Plot
# For visualizations using force plots, the Javascript library must be loaded
shap.initjs()
# Visualize the Shapley values for the prediction of the first instance in the test dataset using a force plot
shap.plots.force(shap_values[0])
# ## 4.3. Global explanations
# Global explanations describe the overall behavior of the model
# ### Bar Plot
# Visualize the Shapley values for the entire test dataset using a bar plot
shap.plots.bar(shap_values)
# The max_display parameter can be used to set the maximum number of bars to be displayed in the bar plot
shap.plots.bar(shap_values, max_display=6)
# ### Beeswarm Plot
# Visualize the Shapley values for the entire test dataset using a beeswarm plot
shap.plots.beeswarm(shap_values)
# ### Force Plot
# Visualize the Shapley values for the entire test dataset using a force plot
shap.plots.force(shap_values[0:100])
PythonFurther reading
In this section you will find further reading to help you learn more about SHAP and visualizing Shapley values.
Books
Scientific publications
- Lundberg, Scott M., and Su-In Lee. “A unified approach to interpreting model predictions.” Advances in Neural Information Processing Systems (2017).
- Lundberg, Scott M., et al. “Explainable machine-learning predictions for the prevention of hypoxaemia during surgery.” Nature biomedical engineering 2.10 (2018): 749-760.
- Lundberg, Scott M., et al. “From local explanations to global understanding with explainable AI for trees.” Nature machine intelligence 2.1 (2020): 56-67.
Articles
Summary
In this blog post, you learned how to use the Python library SHAP to explain the decision-making process of machine learning models.
Specifically, you learned:
- To compute feature attributions using the Python library SHAP, you can usually just use the general Explainer, which automatically selects the appropriate SHAP method for your data and model.
- You can create local explanations using waterfall and force plots.
- Global explanations can be created using bar, beeswarm, and force plots.
Do you have any questions?
Feel free to leave them in the comments below and I will do my best to answer them.
P.S.: Of course I also appreciate constructive feedback on this blog post 😊

Hi, my name is René Heinrich. I am a data scientist doing my PhD in the area of trustworthy artificial intelligence. On this blog I share my experiences and everything I have learned on my own knowledge journey.