如何在 shap 瀑布图中显示特征值?

时间:2021-02-07 17:13:29

标签: python python-3.x shap

查看 shap 库,我遇到了 this question,其中答案展示了瀑布图,整洁!查看一些官方示例 herehere,我注意到这些图还展示了这些功能的价值。

shap 包同时包含 shap.waterfall_plotshap.plots.waterfall,在 Iris 数据集上训练的随机森林上尝试两者给出了相同的结果(参见下面的一个代码和图像示例)

for which_class in y.unique():
display(
    shap.waterfall_plot(shap.Explanation(values=shap_values[int(which_class)][idx], 
                                         base_values=explainer.expected_value[int(which_class)], 
                                         feature_names=X_test.columns.tolist())
                       )
)

其中 idx 表示我试图解释的测试集中的一个样本。该代码为其中一个类生成以下图: enter image description here

如何让绘图同时显示特征值?我没有看到任何可以传递给 plot 方法的额外参数

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

找到了!

shap.Explanation 方法有一个参数,您可以在其中传递数据。请参阅下面的编辑示例

for which_class in y.unique():
display(
    shap.waterfall_plot(shap.Explanation(values=shap_values[int(which_class)][row], 
                                         base_values=explainer.expected_value[int(which_class)], 
                                         data=X_test.iloc[row],  # added this line
                                         feature_names=X_test.columns.tolist())
                       )
)

enter image description here (不要介意与问题中上传的图像相比,特征的贡献略有不同,设置随机种子可能有问题)