Search code examples
pythontensorflowkerasneural-networkshap

During handling of the above exception, another exception occurred when using SHAP to interpret keras neural network model


The x_train looks like this (22 features):


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

The label is 0 and 1, it's a binary classification problem, here's the code for building the model, and I was following this page to implement 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)

This gave me error:

KeyError: 0

During handling of the above exception, another exception occurred

I have no idea what this message is complaining, I tried to use SHAP with a XGBoost and Logistic Regression model and they both work fine, I'm new to keras and SHAP, can someone have a look for me and how I can solved it? Many thanks.


Solution

  • I think SHAP (whatever it is) is expecting a Numpy array and so indexing x_train like a Numpy array, it yields an error. Try:

    shap_values = deep_explainer.shap_values(x_train.values)