Databricks 上的 Shap 值绘图错误但在本地工作

Shap value plotting error on Databricks but works locally

我想做一个简单的形状分析并绘制一个shap.force_plot。我注意到它在 .ipynb 文件中本地运行没有任何问题,但在 Databricks 上失败并显示以下错误消息:

Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must 
also trust this notebook (File -> Trust notebook). If you are viewing  this notebook on 
github the Javascript has been stripped for security. If you are using JupyterLab this 
error is because a JupyterLab extension has not yet been written.

代码:

import xgboost
import shap 

shap.initjs()

X, y = shap.datasets.boston()
bst = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)

explainer = shap.TreeExplainer(bst)
shap_values = explainer.shap_values(X)

shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:])

有什么方法可以在 Databricks 上绘制图像吗?

让我们尝试稍微不同的 (matplotlib=True):

import xgboost
import shap 

X, y = shap.datasets.boston()
bst = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)

explainer = shap.TreeExplainer(bst)
shap_values = explainer.shap_values(X)

shap.force_plot(
    explainer.expected_value, 
    shap_values[0,:], 
    X.iloc[0,:], 
    matplotlib=True # <--
)