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)
CodePudding user response:
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))