How to get the contribution of each features with a datashape (35087, 15, 103)?

Viewed 1068

I discovered recently a very nice github repository called SHAP written in Python and JavaScript. It can be used to explain the output of any machine learning model. You can see a good video here which explain well the library.

Inside a classification problem written in python, I discover I can have a little problem to use this library.

EXPLANATION:

I use exactly 103 features to modelize three labels described by -1, 0 and 1. All my features are very well defined and all of them have a title associated. The problem I have is I am not passing 103 features at each unity. I am passing 103*15 features at each unity of time, i.e 103 features for the current time t_0 and the same features for the times t_-1, ..., t_-14. The shape of my train dataset is (35087, 15, 103) where the first argument represents 35087 seconds.

Here is an obvious example how I might use it :

import shap

# we use the first 100 training examples as our background dataset to integrate over
explainer = shap.DeepExplainer(model, x_train[:100])

# explain the first 10 predictions
# explaining each prediction requires 2 * background dataset size runs
shap_values = explainer.shap_values(x_test[:10])

# init the JS visualization code
shap.initjs()

# transform the indexes to words
import numpy as np
words = imdb.get_word_index()
num2word = {}
for w in words.keys():
    num2word[words[w]] = w
x_test_words = np.stack([np.array(list(map(lambda x: num2word.get(x, "NONE"), x_test[i]))) for i in range(10)])

# plot the explanation of the first prediction
# Note the model is "multi-output" because it is rank-2 but only has one column
shap.force_plot(explainer.expected_value[0], shap_values[0][0], x_test_words[0])

Something I would like to get is simply

enter image description here

How to deal with the features at time t_-1, ..., t_-14 to get the contribution of each feature?

UPDATE

Here is an update of what I tried :

So initially, I have 103 features and 3 labels. X_train.shape is (35087, 15, 103) and X_test.shape is (11696, 15, 103).

explainer = shap.DeepExplainer(Model.model, X_train[:100])
shap_values = explainer.shap_values(X_test[:10])

Here shap_values seems to be a list of three arrays of shape (10, 15, 103).

X_test_flatten = X_test.flatten()
shap.summary_plot(shap_values, X_test_flatten, features_names=FEATURES)

From there I got the error *** IndexError: index 40 is out of bounds for axis 0 with size 15

Could anyone help me to get summary_plot with the above information?

0 Answers
Related