在瀑布图和蜂群图中绘制 SHAP 值
我正在使用随机森林进行二元分类。然而,我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误回调告诉您问题是什么:
因此,请在列表中在列表中调用
shap_vals.sum()
,以是支持的方式获取总和,例如使用内置sum
函数:The error callback tells you what the problem is:
So instead of calling
shap_vals.sum()
on your list, get the sum in a way that is supported, like using the built-insum
function: