我最近发现了一个非常好的github存储库,名为SHAP,用Python和JavaScript编写。它可以用来解释任何机器学习模型的输出。你可以看到一个很好的视频here,它很好地解释了这个库。在
在python编写的一个分类问题中,我发现在使用这个库时可能会遇到一些小问题。在
说明:
我使用了整整103个特性来建模由-1、0和1描述的三个标签。
我所有的功能都定义得很好,而且都有一个相关的标题。问题是我没有在每个单位传递103个特性。我在每个时间单位传递103*15个特性,即当前时间t_0的103个特性和时代{}的相同特性。我的train数据集的形状是(35087, 15, 103),其中第一个参数代表35087秒。在
这里有一个很明显的example我可以如何使用它: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])
我想得到的只是
如何处理时间t_1,…,t_14的特征,以得到每个特征的贡献?在
更新
以下是我尝试的更新:
所以最初,我有103特性和3标签。X_train.shape是(35087, 15, 103),而{}是{}。在
^{pr2}$
这里shap_values似乎是一个由三个形状(10, 15, 103)组成的数组的列表。在X_test_flatten = X_test.flatten()
shap.summary_plot(shap_values, X_test_flatten, features_names=FEATURES)
从那里我得到了错误*** IndexError: index 40 is out of bounds for axis 0 with size 15
有谁能帮我把上面的信息告诉我吗?在