Error in DeepExplainer for MLP Network , which uses Dataset which has only float values

Viewed 108

getting following error. when i use DeepExplainer.

Neural network works fine, here is the code:

        from keras.models import Sequential  
        from keras.layers import Dense  
        from sklearn.preprocessing import scale  
        NNModel = Sequential()  
        # Add an input layer   
        NNModel.add(Dense(24, activation='relu', input_shape=(43,)))  
        # Add one hidden layer   
        NNModel.add(Dense(12, activation='relu'))  
        # Add one hidden layer   
        NNModel.add(Dense(6, activation='relu'))  
        # Add one hidden layer   
        NNModel.add(Dense(3, activation='relu'))     
        # Add an output layer   
        NNModel.add(Dense(1, activation='sigmoid'))  
        NNModel.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])                     
        NNModel.fit(X_train_res,y_train_res,epochs=50, batch_size=1, verbose=1)      
        import tensorflow.keras.backend  
        import numpy as np  
        X_train = X_train_res.sample(frac=100, replace=True)  
        X_test_11 = X_df1_test.sample(frac=10, replace=True)  
        X_train_array = X_train.to_numpy()  
        deepExplainer = shap.DeepExplainer(NNModel,X_train_array)  
        X = X_test_11   
        shap_values = deepExplainer.shap_values(X)  

How ever, when i use deepExplainer it throws following error:

 KeyError Traceback (most recent call last)
    ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
    2894             try:
    -> 2895                 return self._engine.get_loc(casted_key)
    2896             except KeyError as err:
    pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
    pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
    pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
    pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
    KeyError: 0
    The above exception was the direct cause of the following exception:
    KeyError                                  Traceback (most recent call last)
    <ipython-input-620-fe7c03d72907> in <module>
    1 X = X_Train_1
    ----> 2 shap_values = deepExplainer.shap_values(X)
    ~\anaconda3\lib\site-packages\shap\explainers\_deep\__init__.py in shap_values(self, X, ranked_outputs, output_rank_order, check_additivity)
    122             were chosen as "top".
    123        
    --> 124         return self.explainer.shap_values(X, ranked_outputs, output_rank_order, check_additivity=check_additivity)
    ~\anaconda3\lib\site-packages\shap\explainers\_deep\deep_tf.py in shap_values(self, X, ranked_outputs, output_rank_order, check_additivity)
    310                 # assign the attributions to the right part of the output arrays
    311                 for l in range(len(X)):
    --> 312                     phis[l][j] = (sample_phis[l][bg_data[l].shape[0]:] * (X[l][j] - bg_data[l])).mean(0)
    313 
    314             output_phis.append(phis[0] if not self.multi_input else phis)
    ~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
    2900             if self.columns.nlevels > 1:
    2901                 return self._getitem_multilevel(key)
    -> 2902             indexer = self.columns.get_loc(key)
    2903             if is_integer(indexer):
    2904                 indexer = [indexer]
    
    ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
    2895                 return self._engine.get_loc(casted_key)
    2896             except KeyError as err:
    -> 2897                 raise KeyError(key) from err
    2898 
    2899         if tolerance is not None:
    
    KeyError: 0
0 Answers
Related