2

I am trying to list feature importance of a Keras neural network regression model using Lime.

I have tried a number of different variations of the code and keep getting some version of KeyError: 4 where the number is different. I have tried changing the target label and num of features, as well as converting the dataframes to arrays or not.

My model:

model = Sequential()
model.add(Dense(units=1, input_dim=6, activation='relu'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(256, activation='relu'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(units=1, input_dim=3, activation='relu'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(64, activation='relu'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(32, activation='relu'))
model.add(LeakyReLU(alpha=.001))
model.add(Dense(units=1, input_dim=1, activation='relu'))
model.summary()

And my Lime code:

import lime
import lime.lime_tabular
# declare lime explainer
explainer = lime.lime_tabular.LimeTabularExplainer(x_train.values, feature_names=list(x_train.columns),
                                                   verbose=True)
# declare explainer and run
exp = explainer.explain_instance(y_train['absorb_pct'], model.predict(x_train), 
                                 num_features=len(list(x_train.columns)))
exp.show_in_notebook(show_table=True)

x_train includes all features but absorb pct, and absorb pct is my target variable

I want to get some tabular data like shown on this page: https://pythondata.com/local-interpretable-model-agnostic-explanations-lime-python/

But keep getting:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-32-8d39e1aec03b> in <module>
      6 # declare explainer and run
      7 exp = explainer.explain_instance(y_train['absorb_pct'], model.predict(x_train), 
----> 8                                  num_features=len(list(x_train.columns)))
      9 exp.show_in_notebook(show_table=True)

/anaconda3/lib/python3.6/site-packages/lime/lime_tabular.py in explain_instance(self, data_row, predict_fn, labels, top_labels, num_features, num_samples, distance_metric, model_regressor)
    309             explanations.
    310         """
--> 311         data, inverse = self.__data_inverse(data_row, num_samples)
    312         scaled_data = (data - self.scaler.mean_) / self.scaler.scale_
    313 

/anaconda3/lib/python3.6/site-packages/lime/lime_tabular.py in __data_inverse(self, data_row, num_samples)
    462             first_row = data_row
    463         else:
--> 464             first_row = self.discretizer.discretize(data_row)
    465         data[0] = data_row.copy()
    466         inverse = data.copy()

/anaconda3/lib/python3.6/site-packages/lime/discretize.py in discretize(self, data)
    107         for feature in self.lambdas:
    108             if len(data.shape) == 1:
--> 109                 ret[feature] = int(self.lambdas[feature](ret[feature]))
    110             else:
    111                 ret[:, feature] = self.lambdas[feature](

/anaconda3/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
    866         key = com.apply_if_callable(key, self)
    867         try:
--> 868             result = self.index.get_value(self, key)
    869 
    870             if not is_scalar(result):

/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4373         try:
   4374             return self._engine.get_value(s, k,
-> 4375                                           tz=getattr(series.dtype, 'tz', None))
   4376         except KeyError as e1:
   4377             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 4

1 Answers1

2

You should use lime_tabular.RecurrentTabularExplainer instead of lime_tabular.LimeTabularExplainer. As it is an explainer for keras-style recurrent neural networks. Check the tutorials provided in LIME package.

atinjanki
  • 483
  • 3
  • 13
  • Thanks @atinjanki, The explain_instance is requesting 2D array in RecurrentTabularExplainer , even though if I pass the test data as (1,27) array, I am getting ValueError: cannot reshape array of size 27 into shape (270000,), I have 27 features. – hanzgs Aug 21 '19 at 06:44
  • pls discard the above Error, actual Error is : ValueError: Error when checking input: expected dense_474_input to have 2 dimensions, but got array with shape (5000, 1, 27) – hanzgs Aug 21 '19 at 07:07