decision-tree - shap.force_plot() 引发异常 : In v0. 20 force_plot 现在需要基值作为第一个参数

标签 decision-tree xgboost lightgbm catboost

我正在使用 Catboost 并希望可视化 shap_values:

from catboost import CatBoostClassifier
model = CatBoostClassifier(iterations=300)
model.fit(X, y,cat_features=cat_features)

pool1 = Pool(data=X, label=y, cat_features=cat_features)
shap_values = model.get_feature_importance(data=pool1, fstr_type='ShapValues', verbose=10000)

shap_values.shape
Output: (32769, 10)
X.shape
Output: (32769, 9)

然后我执行以下操作并引发异常:
shap.initjs()
shap.force_plot(shap_values[0,:-1], X.iloc[0,:])

异常(exception):在 v0.20 force_plot 现在需要基值作为第一个参数!尝试 shap.force_plot(explainer.expected_value, shap_values) 或对于多输出模型尝试 shap.force_plot(explainer.expected_value[0], shap_values[0])。

以下工作,但我想让 force_plot() 工作:
shap.initjs()
shap.summary_plot(shap_values[:,:-1], X)

我阅读了文档,但无法理解解释器。我试过:
explainer = shap.TreeExplainer(model,data=pool1)
#Also tried:
explainer = shap.TreeExplainer(model,data=X)

但我得到:类型错误:输入类型不支持 ufunc 'isnan',并且无法根据转换规则 ''safe'' 将输入安全地强制转换为任何支持的类型

任何人都可以指出我正确的方向吗?谢谢

最佳答案

我遇到了与下面相同的错误-

Exception: In v0.20 force_plot now requires the base value as the first parameter! Try shap.force_plot(explainer.expected_value, shap_values) or for multi-output models try shap.force_plot(explainer.expected_value[0], shap_values[0]).



这帮助我解决了问题-
import shap
explainer = shap.TreeExplainer(model,data=X)
shap.initjs()
shap.force_plot(explainer.expected_value[0],X.iloc[0,:])

同样对于以下问题 -

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''



检查您的数据,如果它包含任何 NaN 或缺失值。
希望这可以帮助!

关于decision-tree - shap.force_plot() 引发异常 : In v0. 20 force_plot 现在需要基值作为第一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56791085/

相关文章:

r - Xgboost 错误 : label must be in [0, num_class), num_class=2

python - AUC 高,但数据不平衡导致预测不佳

python - Pyinstaller exe 转换失败 - 使用 lightgbm 和 sklearn

python - 如何在随机森林中绘制树而不显示 "samples"和 "value"?

c++ - 编写决策树并将其加载到文件 C++

python - 在 32 位 msys 失败的 python 下安装 xgboost

python - LightGBM 是否根据名称或 cat_code 值处理 Pandas 分类?

r - 使用 rpart 生成桑基图的决策树

python - 用 Python 解释决策树

python - 类型错误 : unsupported operand type(s) for -: 'int' and 'StandardScaler