Applying a clustering algorithm to feature contribution
By: Ana Preciado, Data Science To Go
Tags: Explainability, Clustering, Shap, Lightgbm, Machine Learning

Applying a clustering algorithm to feature contribution

Background photo created by kjpargeter — www.freepik.com (https://www.freepik.com/photos/background)

INTRODUCTION

If you have deep-dived into the world of machine learning explainability for ensemble algorithms like xgboost and lightgbm, you have probably heard of the prediction contributions, and probably even used the SHAP package that allows us to better understand trained models. This article works from the assumption that you have worked with Shapley values before, as it does not offer a detailed explanation of the topic.

In case you haven’t used prediction contributions before, let’s just say that these are values that summarise how each particular feature contributes to the prediction for each particular observation. To make sense of these values, we can transform them into the actual prediction probability by adding them all up (per observation), add the bias (expected value), and apply a logit function. Therefore, if you are predicting the occurrence of an event (binary classification where 1 indicates the event happened) a positive contribution from a feature means it is signaling the model to predict a higher probability of the event taking place for that particular observation. I invite you to read more on the topic if you are interested, starting with this interesting GitHub page by Christoph Molnar:

CHALLENGE

While through the use of prediction contributions we can see how (on average) a particular feature impacts the prediction of the overall population, we need to remember that features don’t work alone. Especially when we work with ensemble algorithms like xgboost and lightgbm, the contribution a feature has on the observation’s prediction largely depends on the value the other features have for that same observation. As a result, it's often helpful to plot contributions for more than one feature at a time (in order to identify trends of the interaction between them). In this article, I want to go a step further and propose the use of clustering algorithms like KMeans over the prediction contribution data, with the objective of providing a more holistic approach to the explainability of tree ensemble machine learning algorithm decisions.

EXPECTATION

Feeding prediction contribution data to a KMean clustering algorithm will result in a set of identified groups that depict within-group similarities on how all the features work together on signaling the model how it should predict the observations in that group. It's important to remember that these clusters won’t be directly tied to the feature values, but rather tied to the contributions of these feature values. Because KMeans is an unsupervised learning algorithm (it is not predicting the occurrence of an event, but mainly identifying trends within the data), different groups or clusters will have different percentages of their observations that accomplish the target. A useful exercise is to see the per-group average difference between target values and prediction values. Looking at predictions from this light helps identify which are observations that the model has more difficulty predicting.

PRACTICE:

Let’s apply everything we’ve discussed above.

First: We’ll retrieve our data, transform it, and create a base model to work with.

After we do this we should have a table that looks roughly like this:

We’ll be feeding this dataset to a KMeans algorithm in order to identify trends or clusters within our prediction contributions.

Advantages of using prediction contributions

Scale: All prediction contributions are in the same scale and we don't need to preprocess or transform our data before feeding it to KMeans. Null values: While a feature value may be null, a feature contribution value will not be. Therefore we can safely use KMeans without having to drop observations or input null data.

Let’s get started with this second step of our exercise:

This is how our resulting dataset should look like:

To begin understanding our dataset, I think it’s fair to look at the information at each cluster center.

Following the previous code, we would arrive at a table similar to this one:

It becomes clear that the features that define each cluster really are RM, AGE, PTRATIO, and LSTAT. However, let's remember that the centers for the values clusters here are based on the feature contributions, not on the feature values. Let's create a different table with a groupby that will allow us to look at the feature values per cluster as well as the % of the observations from each cluster that accomplish the target (as well as their prediction for it).

If we group the train data in accordance with each cluster, we can see which clusters are easier for the model to predict based on the existing difference between its target value and prediction value. It becomes evident that cluster 18 encompasses the population that our model has the most difficulty predicting.

CONCLUSION

In identifying holistic trends for model explainability, we can look into clustering algorithms for insights. Clustering algorithms will help us create groups of observations that are similar to each other in terms of how the value of their features affects their prediction. By looking at the target and prediction of each of these groups we can start better understanding which types of observations are the ones the model finds easier or more difficult to predict. Thank you for reading. I hope you found this article enlightening on some level.