查看 shap 库,我遇到了 this question,其中答案展示了瀑布图,整洁!查看一些官方示例 here 和 here,我注意到这些图还展示了这些功能的价值。
shap 包同时包含 shap.waterfall_plot
和 shap.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
表示我试图解释的测试集中的一个样本。该代码为其中一个类生成以下图:
如何让绘图同时显示特征值?我没有看到任何可以传递给 plot 方法的额外参数
非常感谢任何帮助!
答案 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())
)
)