How to interpret machine learning models with SHAP values
Tags: machinelearning, modelexplantion, shapvalues, modeling
Published: 2021-11-23 21:44:23

### Outline

* Introduction
* What are SHAP values?
* Walkthrough example
* How does Mage explain models?
* Conclusion

### Introduction
Machine learning models are usually seen as a “black box.” It takes some features as input and produces some predictions as output. The common questions after model training are:

* How do different features affect the prediction results?
* What are the top features that influence the prediction results?
* The model performance metrics look great, but should I trust the results?

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nakwk4vxzgj23o9kdqq5.gif)_Whatever you say Prince Ali (Source: Giphy)_</center>

Thus, model explainability plays an important role in the machine learning world. Model insights are useful for the following reasons:
* Debugging
* Informing feature engineering
* Directing future data collection
* Informing human decision-making
* Building trust

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tnzmo0aiz414986okf2.gif)_Model insights doing its part! (Source: Giphy)_</center>

We talked about the metrics used for model evaluation in a previous article “[The definitive guide to Accuracy, Precision, and Recall for product developers.](https://m.mage.ai/the-definitive-guide-to-accuracy-precision-and-recall-for-product-developers-71e4887d0da7)” Those metrics help you understand the overall performance of a model. However, we need more insights into how different features impact the model’s prediction.

There are multiple techniques to explain the models. In this article, we’ll introduce SHAP values, which is one of the most popular model explanation techniques. We’ll also walk through an example to show how to use SHAP values to get insights.

## What are SHAP values?
SHAP stands for “SHapley Additive exPlanations.” [Shapley values](https://en.wikipedia.org/wiki/Shapley_value) are a widely used approach from cooperative game theory. The essence of Shapley value is to measure the contributions to the final outcome from each player separately among the coalition, while preserving the sum of contributions being equal to the final outcome.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4u62df7y4ecc2cs212x6.gif)_Oh SHAP! (Source: Giphy)_</center>

When using SHAP values in model explanation, we can measure the input features’ contribution to individual predictions. We won’t be covering the complex formulas to calculate SHAP values in this article, but we’ll show how to use the [SHAP](https://github.com/slundberg/shap) Python library to easily calculate SHAP values.

There are some other techniques used to explain models like [permutation importance](https://scikit-learn.org/stable/modules/permutation_importance.html) and [partial dependence plots](https://scikit-learn.org/stable/modules/partial_dependence.html). Here are some benefits of using SHAP values over other techniques:

1. Global interpretability: SHAP values not only show feature importance but also show whether the feature has a positive or negative impact on predictions.
2. Local interpretability: We can calculate SHAP values for each individual prediction and know how the features contribute to that single prediction. Other techniques only show aggregated results over the whole dataset.
3. SHAP values can be used to explain a large variety of models including [linear models](https://en.wikipedia.org/wiki/Linear_model) (e.g. [linear regression](https://en.wikipedia.org/wiki/Linear_regression)), [tree-based models](https://sdcastillo.github.io/PA-R-Study-Manual/tree-based-models.html) (e.g. [XGBoost](https://en.wikipedia.org/wiki/XGBoost)) and [neural networks](https://en.wikipedia.org/wiki/Neural_network), while other techniques can only be used to explain limited model types.

## Walkthrough example
We’ll walk through an example to explain how SHAP values work in practice. If you’ve followed Mage’s blog articles, you might’ve already read “[Build your first machine learning model](https://m.mage.ai/how-to-get-started-with-ai-ml-8630fecfd776).” We’ll use the same dataset [titanic_survival](https://app.box.com/s/zrpzj8zw35zbs8njcij40gy45gcgwsyl) to demonstrate how SHAP values work.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0nae8qf81w7pbdde0gk.gif)_The SHAP has sailed (Source: Giphy)_</center>

We use XGBoost to train the model to predict survival. “Sex”, “Pclass”, “Fare”, and “Age” features are used in the model training. “Survived” is the label feature with values 0 and 1.

We use this [SHAP](https://github.com/slundberg/shap) Python library to calculate SHAP values and plot charts. We select TreeExplainer here since XGBoost is a tree-based model.
```
import shap
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)
```
The `shap_values` is a 2D array. Each row belongs to a single prediction made by the model. Each column represents a feature used in the model. Each SHAP value represents how much this feature contributes to the output of this row’s prediction.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dsg9mtlq3czbhvs7epz.png)

Positive SHAP value means positive impact on prediction, leading the model to predict 1(e.g. Passenger survived the Titanic). Negative SHAP value means negative impact, leading the model to predict 0 (e.g. passenger didn’t survive the Titanic).

### Feature importance
We can use the **summary_plot** method with plot_type “bar” to plot the feature importance.
```
shap.summary_plot(shap_values, X, plot_type='bar')
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g96ki40y9eqihiyulj2w.png)

The features are ordered by how much they influenced the model’s prediction. The x-axis stands for the average of the absolute SHAP value of each feature. For this example, “Sex” is the most important feature, followed by “Pclass”, “Fare”, and “Age”.
<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmegnbfkey7lm7l3t92v.gif)_(Source: Giphy)_</center>

### Directionality impact
With the same **summary_plot** method, we can plot dot charts to visualize the directionality impact of the features.
```
shap.summary_plot(shap_values, X)
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hgqh3jvm3a29q4kgmla.png)
In this chart, the x-axis stands for SHAP value, and the y-axis has all the features. Each point on the chart is one SHAP value for a prediction and feature. Red color means higher value of a feature. Blue means lower value of a feature. We can get the general sense of features’ directionality impact based on the distribution of the red and blue dots.

In the chart above, we can conclude the following insights:
* Higher value of “Sex” (male) leads to lower chance of survive.  * Lower value of “Sex” (female) leads to higher chance of survival.
* Higher value of “Pclass” leads to lower chance to survive as well. 
Lower value of “Pclass” leads to higher chance of survival.
* Lower value of “Fare” leads to lower chance to survive.
* Higher value of “Age” leads to lower chance to survive.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ae4ul7l03xtm3folasal.png)_Well, you get the point (Source: Titanic)_</center>

By comparing these insights with general understanding of the problem, we can trust that the model is intuitive and making the right decisions. For example, the model will more likely predict survival if the passenger had a Sex of female. This is intuitive because women and children were put on lifeboats before men were allowed.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbsxfpvvec6rriv9hiud.gif)_When it sinks in (Source: Giphy)_</center>

### Individual predictions
Besides seeing the overall trend of feature impact, we can call the **force_plot** method to visualize how features contribute to individual predictions.
```
shap.force_plot(
    explainer.expected_value,
    shap_values[idx, :],
    X.iloc[idx, :],
)
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsz6vhl6plkxvtbfp3ag.png)

In this case, feature “Fare” and “Sex” have a positive impact on the prediction, while “Pclass” and “Age” have a negative impact on the prediction.

## How does Mage explain models?
After the model is trained, Mage provides an Analyzer page to show the model performance and insights. In addition to model performance metrics (precision, recall, accuracy, etc), we leverage SHAP values to show features that have the most impact on model output and how those features impact the model output.

“Top Influencers” chart is similar to the summary bar chart from the `shap` library. We calculate the average of the absolute SHAP values for each feature and use it to show which features were the most important when making a prediction.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4f2s4xlkvre0fy00jvc.png)_Mage Analyzer page: top features_</center>

We also translate SHAP values to this “Directionality” chart to show how features impact the output in a more intuitive way. Green plus icon means higher feature value and red minus icon means lower feature value. Horizontally, the right side means positive impact on the prediction and the left side means negative impact. We also summarize all the insights into text to make it easier to understand.

For the Titanic example, we can easily see when sex is female, pclass is smaller, fare is higher, or younger age, there is a higher chance to survive. These insights align with our understanding of what actually happened during the Titanic incident in real life.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61cfnpp6t2phore84ojk.png)_Mage Analyzer page: SHAP values_</center>

## Conclusion
Model explainability is an important topic in machine learning. SHAP values help you understand the model at row and feature level. The [SHAP](https://github.com/slundberg/shap) Python package is a useful library to calculate SHAP values, visualize the feature importance, and directionality impact using multiple charts.

<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpe611ir1oom877ubuyq.png)_(Source: Titanic)_</center>

### Resources
* Original paper for SHAP: [https://arxiv.org/pdf/1705.07874.pdf](https://arxiv.org/pdf/1705.07874.pdf)
* Documentation for 'shap' library : [https://shap.readthedocs.io/en/latest/index.html](https://shap.readthedocs.io/en/latest/index.html)