【问题标题】:shap.force_plot() raises Exeption: In v0.20 force_plot now requires the base value as the first parametershap.force_plot() 引发异常:在 v0.20 中 force_plot 现在需要基值作为第一个参数
【发布时间】:2019-11-09 11:29:01
【问题描述】:

我正在使用 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,:])

例外:在 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)

但我得到:TypeError: ufunc 'isnan' not supported for the input types,并且根据强制转换规则 ''safe'' 无法安全地将输入强制转换为任何支持的类型

谁能指出我正确的方向?谢了

【问题讨论】:

    标签: decision-tree xgboost lightgbm catboost


    【解决方案1】:

    试试这个:

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

    【讨论】:

      【解决方案2】:

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

      例外:在 v0.20 中,force_plot 现在需要基值作为 第一个参数!尝试 shap.force_plot(explainer.expected_value, shap_values) 或多输出模型尝试 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' 不支持输入类型,并且 输入无法安全地强制转换为任何支持的类型 强制转换规则“安全”

      检查您的数据是否包含任何 NaN 值或缺失值。
      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 2023-03-26
        • 2019-06-27
        • 1970-01-01
        • 2019-12-07
        • 1970-01-01
        • 2015-08-31
        • 2022-01-12
        • 1970-01-01
        相关资源
        最近更新 更多