marcotcr / lime

Lime: Explaining the predictions of any machine learning classifier
BSD 2-Clause "Simplified" License
11.64k stars 1.81k forks source link

LimeTabularExplainer TypeError: unhashable type: 'slice' #168

Closed shashankumar2812 closed 5 years ago

shashankumar2812 commented 6 years ago

I am trying to use LIME package for explaining predictive models. Got following error while running the following code. Can someone explain what is going wrong here?

# Create the LIME Explainer
explainer = lime.lime_tabular.LimeTabularExplainer(X_train_undersample feature_names = feature_names,class_names=['0','1'], kernel_width=3)

Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-68-559ebd127096> in <module>()
      1 # Create the LIME Explainer
----> 2 explainer = lime.lime_tabular.LimeTabularExplainer(X_train_undersample , feature_names = feature_names,class_names=['0','1'], kernel_width=3)

/Applications/anaconda3/lib/python3.6/site-packages/lime/lime_tabular.py in __init__(self, training_data, mode, training_labels, feature_names, categorical_features, categorical_names, kernel_width, verbose, class_names, feature_selection, discretize_continuous, discretizer, random_state)
    162                 self.discretizer = QuartileDiscretizer(
    163                         training_data, self.categorical_features,
--> 164                         self.feature_names, labels=training_labels)
    165             elif discretizer == 'decile':
    166                 self.discretizer = DecileDiscretizer(

/Applications/anaconda3/lib/python3.6/site-packages/lime/discretize.py in __init__(self, data, categorical_features, feature_names, labels, random_state)
    123         BaseDiscretizer.__init__(self, data, categorical_features,
    124                                  feature_names, labels=labels,
--> 125                                  random_state=random_state)
    126 
    127     def bins(self, data, labels):

/Applications/anaconda3/lib/python3.6/site-packages/lime/discretize.py in __init__(self, data, categorical_features, feature_names, labels, random_state)
     44 
     45         # To override when implementing a custom binning
---> 46         bins = self.bins(data, labels)
     47         bins = [np.unique(x) for x in bins]
     48 

/Applications/anaconda3/lib/python3.6/site-packages/lime/discretize.py in bins(self, data, labels)
    128         bins = []
    129         for feature in self.to_discretize:
--> 130             qts = np.array(np.percentile(data[:, feature], [25, 50, 75]))
    131             bins.append(qts)
    132         return bins

/Applications/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2137             return self._getitem_multilevel(key)
   2138         else:
-> 2139             return self._getitem_column(key)
   2140 
   2141     def _getitem_column(self, key):

/Applications/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_column(self, key)
   2144         # get column
   2145         if self.columns.is_unique:
-> 2146             return self._get_item_cache(key)
   2147 
   2148         # duplicate columns & possible reduce dimensionality

/Applications/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
   1838         """Return the cached item, item represents a label indexer."""
   1839         cache = self._item_cache
-> 1840         res = cache.get(item)
   1841         if res is None:
   1842             values = self._data.get(item)

TypeError: unhashable type: 'slice'
marcotcr commented 6 years ago

I'm guessing X_train_undersample is a dataframe instead of a numpy array.

Raghavvraj commented 6 years ago

Hi,

any luck?

paulaceccon commented 6 years ago

@Raghavvraj, if your data is a pandas dataframe, just use my_df.values when creating the explainer object.

tusharu14 commented 5 years ago

The 'X_train' is a dataframe and not an array.to convert from a dataframe to array before making the explainer try the following , it worked for me : X_train=X_train.values This will convert the dataframe into array and then pass it as an argument to the explainer.