【问题标题】:Shap force plot not displaying figure: shap.plots._force.AdditiveForceVisualizer形状力图不显示图:shap.plots._force.AdditiveForceVisualizer
【发布时间】:2021-03-30 00:59:11
【问题描述】:

我正在通过https://towardsdatascience.com/explain-your-model-with-the-shap-values-bc36aac4de3d 尝试让force_plot 打印。

我在 Ubuntu 20.04 上运行 Python 3.8.5

我运行这段代码:

shap.initjs()

# Write in a function

random_picks = np.arange(1,330,50) # Every 50 rows
S = X_test.iloc[random_picks]
def shap_plot(j):
    explainerModel = shap.TreeExplainer(xg_clf)
    shap_values_Model = explainerModel.shap_values(S)
    p = shap.force_plot(explainerModel.expected_value, shap_values_Model[j], S.iloc[[j]])
    return(p)
z = shap_plot(3)

我得到<shap.plots._force.AdditiveForceVisualizer object at 0x7f1568cac070>

返回。

我不是 python 专家,所以我尝试查看这些数据:

display(z)

未定义。

print(z),它只返回对象的名称,并不能帮助我查看绘制的内容。

我也尝试过使用已经加载的matplotlib

def shap_plot(j):
    explainerModel = shap.TreeExplainer(xg_clf)
    shap_values_Model = explainerModel.shap_values(S)
    p = shap.force_plot(explainerModel.expected_value, shap_values_Model[j], S.iloc[[j]])
    plt.savefig('tmp.svg')
    plt.close()
    return(p)
shap_plot(3)

但这只是给出一个空图像。

如果有错误,我看不到。

我怎样才能让这个shap.force_plot 显示图像?

【问题讨论】:

    标签: python-3.x machine-learning shap


    【解决方案1】:

    解决方法在手册中:

    help(shap.force_plot)
    

    显示

    matplotlib : bool
            Whether to use the default Javascript output, or the (less developed) matplotlib output. Using matplotlib can be helpful in scenarios where rendering Javascript/HTML is inconvenient.
    

    确实,运行笔记本对我来说非常不方便。

    所以为了保存图像:

    def shap_plot(j):
        explainerModel = shap.TreeExplainer(xg_clf)
        shap_values_Model = explainerModel.shap_values(S)
        p = shap.force_plot(explainerModel.expected_value, shap_values_Model[j], S.iloc[[j]], matplotlib = True, show = False)
        plt.savefig('tmp.svg')
        plt.close()
        return(p)
    

    【讨论】:

    • 在他们的网站上有一个针对多个样本的教程 (github.com/slundberg/shap -> shap.plots.force(shap_values))。但是当您尝试 matplotlib = True 时,它​​表示不可能使用多个样本。任何想法为什么?
    • @NoobProgrammer 我不知道。夏普很挑剔
    • 因为它是交互式绘图,必须是htm格式:)
    猜你喜欢
    • 1970-01-01
    • 2020-05-20
    • 2014-06-27
    • 2022-01-24
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多