在处理上述异常期间,使用SHAP解释keras神经网络模型时发生了另一个异常

问题描述

x_train如下所示(22个功能):


total_amount    reward  difficulty  duration    discount    bogo    mobile  social  web income  ... male    other_gender    age_under25 age_25_to_35    age_35_to_45    age_45_to_55    age_55_to_65    age_65_to_75    age_75_to_85    age_85_to_105
0   0.006311    0.2 0.50    1.000000    1.0 0.0 1.0 1.0 1.0 0.355556    ... 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
1   0.015595    0.2 0.50    1.000000    1.0 0.0 1.0 1.0 1.0 0.977778    ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0

标签01,这是一个二进制分类问题,这是构建模型的代码,我遵循this page来实现SHAP:


#use SHAG
deep_explainer = shap.DeepExplainer(nn_model_2,x_train[:100])

# explain the first 10 predictions
# explaining each prediction requires 2 * background dataset size runs
shap_values = deep_explainer.shap_values(x_train)

这给了我错误

KeyError: 0

During handling of the above exception,another exception occurred

我不知道此消息在抱怨什么,我尝试将SHAP与XGBoost和Logistic回归模型一起使用,它们都可以正常工作,我对keras和SHAP还是陌生的,有人可以看看我吗,以及我如何能解决吗?非常感谢。

解决方法

我认为SHAP(无论它是什么)都期望有一个Numpy数组,因此像Numpy数组一样索引x_train会产生错误。试试:

shap_values = deep_explainer.shap_values(x_train.values)