Visualizing Shapley Values Over Time

Justin Swansburg
5 min readApr 2, 2023

--

The ML insight you don’t know about

An impressionist oil painting of a robot bound by time (Dalle-2)

Overview

Shapley values are a widely-used concept in machine learning, particularly in the area of explainable artificial intelligence (XAI). They provide a way to fairly distribute the contributions of each feature to the outcome of a prediction made by a machine learning model. The main advantage of Shapley values is that they offer a mathematically rigorous way to understand and explain model predictions.

In the context of machine learning, a Shapley value for a specific feature represents the average contribution of that feature towards the model’s predictions, considering all possible combinations of features. The idea is to evaluate the impact of including or excluding a feature on the model’s output, taking into account its interactions with other features. Let’s take a look at an example for a single prediction:

Shapely Value Force Plot

You can see towards the lefthand side that our model’s prediction was 20.23%. Shapely values allow us to decompose this prediction into marginal effects. In this case, the features highlighted in blue are decreasing are prediction and the features highlighted in red are increasing our prediction.

This enables us to quantify the importance of each feature in a way that is fair and consistent, as the Shapley values always sum up to the total prediction value minus the average prediction for the dataset.

Shapely values are often calculated at prediction time to add additional context to or “explain” a model’s predictions at inference time (hence why they’re often referred to as prediction explanations). Knowing why a score is relatively high or low can be incredibly helpful when making decisions. This got me thinking, are there other helpful ways we can use these values?

Shapely Values Over Time

I’ve already explored how you can group Shapely values by feature to get feature impact charts, how they can enhance lift charts, and how you can visualize them in a table across multiple observations. But what if we aggregated our Shapely values across both time periods and features?

By that, I mean what if we grouped these values across customers for every month, quarter, or year and then displayed them over time? Turns out, it would look something like this:

Prediction explanations over time
Legend

For context, this is the same dataset I used in this post on building customer churn models. There’s a lot going on in this chart so let’s deconstruct it:

  • The solid black line is plotting the average churn prediction across all our customers for every month. This is simply the average prediction grouped by month.
  • The stacked bars are our aggregated Shapely values. Just like row-level Shapely values represent how different features impact a single prediction, these stacked bars represent how different features are impacting our average predictions each month.

You can see that over time our expected churn rate increases from around 32 percent to over 44 percent. Why is this? What changed across our customer base? Typically these are tricky questions to answer. Fortunately, we can look at our aggregated prediction explanations to get to the bottom of them.

Right off the bat, we can see purple bars have gotten larger and the green bars have gotten smaller. These bars are the aggregated Shapely values for total product usage over the past 180 days and the number of unique products purchased over the past five years. This chart is telling us that these two features are disproportionately responsible for driving up our overall churn predictions over the past year.

This makes sense since given that the median customer’s product usage was 9 in the most recent quarter, whereas it was 13 in early 2020. Unsurprisingly, our model has picked up on the fact that lower usage will likely translate to increased churn.

Why stop here though? Since we learned that product usage and total number of unique products purchased were the top two features driving up our expected churn, we can plot them over time to get a better sense of what changed. Let’s start with products purchased:

In the rightmost bar, you can see that fewer customers purchased a business essentials pack and more purchased an enterprise pack. These changes are responsible for part of our spiking churn prediction.

We can also take a look at product usage:

Product usage over time

Overall product usage feel from nearly 2.5 activities per month to roughly 1. This coincides nicely with the increase in Shapely values we saw in our original chart (the purple bars).

Next time you’re working to interpret your machine learning or explain a trend across your predictions, make sure to remember Shapely values!

Go here to see the code and follow me on Medium and LinkedIn for more helpful data science tips. Thanks for reading!

--

--

Justin Swansburg

Data scientist and AI/ML leader | VP, Applied AI @ DataRobot.