我正在使用隨機森林進行二進制分類。但是,我試圖使用 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)
uj5u.com熱心網友回復:
錯誤回呼告訴您問題所在:
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))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/453454.html
上一篇:如何在golangdockertest.resource中使用docker密碼/環境變數而不是硬編碼密碼
下一篇:成對讀取序列并將堿基增加 1
