在瀑布图和蜂群图中绘制 SHAP 值

发布于 2025-01-18 00:20:35 字数 2464 浏览 1 评论 0原文

我正在使用随机森林进行二元分类。然而,我尝试使用 SHAP 来解释模型预测。但是,我不断收到以下错误。我正在遵循教程 这里

import shap
explainer = shap.Explainer(rf_boruta)  #pass my model
shap_values = explainer(ord_test_t) #pass my test dataset
sample_idx = 15
shap_vals = explainer.shap_values(ord_test_t.iloc[sample_idx:sample_idx+1])
print("Base Value : ", explainer.expected_value)
print()
print("Shap Values for Sample %d : "%sample_idx, shap_vals)
print("\n")
print("Prediction From Model                            : ", rf_boruta.predict(ord_test_t.iloc[15:16]))
print("Prediction From Adding SHAP Values to Base Value : ", explainer.expected_value + shap_vals.sum())

我收到如下所示的错误

>       8 print("\n")
>       9 print("Prediction From Model                            : ", rf_boruta.predict(ord_test_t.iloc[sample_idx:sample_idx+1]))
> ---> 10 print("Prediction From Adding SHAP Values to Base Value : ", explainer.expected_value + shap_vals.sum())
> 
> AttributeError: 'list' object has no attribute 'sum'

当我尝试另一个教程时, 这里,我又遇到了一个错误

explainer = shap.TreeExplainer(rf_boruta,ord_test_t)
shap_values = explainer.shap_values(ord_test_t)
sample_ind = 0
shap.waterfall_plot(explainer.expected_value, shap_values[sample_ind],ord_test_t.iloc[sample_ind])


TypeError: waterfall() got multiple values for argument 'max_display'

后来,当我将其更改为默认值,我收到另一个错误,如下所示

> ---> 46     base_values = shap_values.base_values
>      47 
>      48     features = shap_values.data
> 
> AttributeError: 'numpy.ndarray' object has no attribute 'base_values'

更新 - 尝试了另一个代码

row_to_show = 5
data_for_prediction = ord_test_t.iloc[row_to_show]  # use 1 row of data here. Could use multiple rows if desired
data_for_prediction_array = data_for_prediction.values.reshape(1, -1)
rf_boruta.predict_proba(data_for_prediction_array)
explainer = shap.TreeExplainer(rf_boruta)
# Calculate Shap values
shap_values = explainer.shap_values(data_for_prediction)
shap.waterfall_plot(explainer.expected_value,shap_values,data_for_prediction)

I am working on binary classification using random forest. However, am trying to use SHAP to explain the model predictions. However, I keep getting the below error. I am following the tutorial here

import shap
explainer = shap.Explainer(rf_boruta)  #pass my model
shap_values = explainer(ord_test_t) #pass my test dataset
sample_idx = 15
shap_vals = explainer.shap_values(ord_test_t.iloc[sample_idx:sample_idx+1])
print("Base Value : ", explainer.expected_value)
print()
print("Shap Values for Sample %d : "%sample_idx, shap_vals)
print("\n")
print("Prediction From Model                            : ", rf_boruta.predict(ord_test_t.iloc[15:16]))
print("Prediction From Adding SHAP Values to Base Value : ", explainer.expected_value + shap_vals.sum())

I get an error as shown below

>       8 print("\n")
>       9 print("Prediction From Model                            : ", rf_boruta.predict(ord_test_t.iloc[sample_idx:sample_idx+1]))
> ---> 10 print("Prediction From Adding SHAP Values to Base Value : ", explainer.expected_value + shap_vals.sum())
> 
> AttributeError: 'list' object has no attribute 'sum'

When I tried another tutorial here, I got another error

explainer = shap.TreeExplainer(rf_boruta,ord_test_t)
shap_values = explainer.shap_values(ord_test_t)
sample_ind = 0
shap.waterfall_plot(explainer.expected_value, shap_values[sample_ind],ord_test_t.iloc[sample_ind])


TypeError: waterfall() got multiple values for argument 'max_display'

Later, when I change it to default, I get another error as shown below

> ---> 46     base_values = shap_values.base_values
>      47 
>      48     features = shap_values.data
> 
> AttributeError: 'numpy.ndarray' object has no attribute 'base_values'

update - another code tried

row_to_show = 5
data_for_prediction = ord_test_t.iloc[row_to_show]  # use 1 row of data here. Could use multiple rows if desired
data_for_prediction_array = data_for_prediction.values.reshape(1, -1)
rf_boruta.predict_proba(data_for_prediction_array)
explainer = shap.TreeExplainer(rf_boruta)
# Calculate Shap values
shap_values = explainer.shap_values(data_for_prediction)
shap.waterfall_plot(explainer.expected_value,shap_values,data_for_prediction)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2025-01-25 00:20:35

错误回调告诉您问题是什么:

10 print("Prediction From Adding SHAP Values to Base Value : ", explainer.expected_value + shap_vals.sum())

AttributeError: 'list' object has no attribute 'sum'

因此,请在列表中在列表中调用shap_vals.sum(),以支持的方式获取总和,例如使用内置sum函数:

print("Prediction From Adding SHAP Values to Base Value : ", explainer.expected_value + sum(shap_vals))

The error callback tells you what the problem is:

10 print("Prediction From Adding SHAP Values to Base Value : ", explainer.expected_value + shap_vals.sum())

AttributeError: 'list' object has no attribute 'sum'

So instead of calling shap_vals.sum() on your list, get the sum in a way that is supported, like using the built-in sum function:

print("Prediction From Adding SHAP Values to Base Value : ", explainer.expected_value + sum(shap_vals))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文