Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: '(slice(None, None, None), slice(None, None, None))' is an invalid key #293

Closed
haskarb opened this issue Feb 20, 2019 · 8 comments

Comments

@haskarb
Copy link

haskarb commented Feb 20, 2019

I'm facing mentioned issue. I initially thought the issue is due to version of python I was using i.e python3, but I tried to used it with python2, the same error pops up.


clf = RandomForestClassifier(n_estimators=50, max_depth=5, random_state=0, n_jobs=3)
clf.fit(X_train,y_train)
predict_fn_rf = lambda x : clf.predict_proba(X_test).astype(float)
explainer = lime.lime_tabular.LimeTabularExplainer(X_train[:,:])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-c2cd4dda4439> in <module>()
----> 1 explainer = lime.lime_tabular.LimeTabularExplainer(X_train[:,:])

/home/crisp/cnn/local/lib/python2.7/site-packages/pandas/core/frame.pyc in __getitem__(self, key)
   2925             if self.columns.nlevels > 1:
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]

/home/crisp/cnn/local/lib/python2.7/site-packages/pandas/core/indexes/base.pyc in get_loc(self, key, method, tolerance)
   2654                                  'backfill or nearest lookups')
   2655             try:
-> 2656                 return self._engine.get_loc(key)
   2657             except KeyError:
   2658                 return self._engine.get_loc(self._maybe_cast_indexer(key))

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

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

TypeError: '(slice(None, None, None), slice(None, None, None))' is an invalid key](url)
@haskarb haskarb closed this as completed Mar 5, 2019
@naanunaane
Copy link

I'm having the same issue. How did you solve this? @dhariyalbhaskar

@WetzlerkAI
Copy link

I'm also having this issue. Any ideas?

@naanunaane
Copy link

I'm also having this issue. Any ideas?

Hi @WetzlerkAI, I changed my input to a numpy array instead and it worked. I have still not been able to sort this issue with a Pandas dataframe input. If it is urgent in your case, I suggest changing your input to numpy and moving ahead.

@ameyxd
Copy link

ameyxd commented Apr 19, 2019

@WetzlerkAI I had the same problem. I fixed it by converting the dataframe to a numpy array and changing my variable assignments accordingly. Fist bump to @naanunaane!

@andorend
Copy link

X_train is a Pandas DataFrame. In Pandas you can use X_train.iloc[:,:] and that fixes the issue, but I think it is the same as X_train without any indexing. If you would like to get the Numpy array from a Pandas DataFrame you can use X_train.values . In this case you again can use your type of indexing like X_train.values[:,:]

I think it does not depend on Python version.

@shashwatgithub
Copy link

shashwatgithub commented Sep 1, 2019

Use like data_frame_name.iloc[:,1:4]

for all rows and 1,2 and 3 columns

@doperogue
Copy link

You have to always use "iloc" to slice a pandas array
eg:
X[:-1] #not gonna work
X.iloc[:-1] #gonna work

@ghbacct
Copy link

ghbacct commented Feb 4, 2020

Current best practice would be to use df.to_numpy() instead of df.values[:.:].

Source:
1.https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_numpy.html#pandas.DataFrame.to_numpy
2. https://stackoverflow.com/questions/31789160/convert-select-columns-in-pandas-dataframe-to-numpy-array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants