Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

machine learning - Why does using X[0] in MNIST classifier code give me an error?

I was learning to do classification with the MNIST dataset. And I got an error with I am not able to figure out, I have done a lot of google searches and I am not able to do anything, maybe you are an expert and can help me. Here is the code--

>>> from sklearn.datasets import fetch_openml
>>> mnist = fetch_openml('mnist_784', version=1)
>>> mnist.keys()

output: dict_keys(['data', 'target', 'frame', 'categories', 'feature_names', 'target_names', 'DESCR', 'details', 'url'])

>>> X, y = mnist["data"], mnist["target"]
>>> X.shape

output:(70000, 784)

>>> y.shape

output:(70000)

>>> X[0]

output:KeyError                                  Traceback (most recent call last)
c:userskhushappdatalocalprogramspythonpython39libsite-packagespandascoreindexesase.py in get_loc(self, key, method, tolerance)
   2897             try:
-> 2898                 return self._engine.get_loc(casted_key)
   2899             except KeyError as err:

pandas\_libsindex.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libsindex.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libshashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libshashtable_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-10-19c40ecbd036> in <module>
----> 1 X[0]

c:userskhushappdatalocalprogramspythonpython39libsite-packagespandascoreframe.py in __getitem__(self, key)
   2904             if self.columns.nlevels > 1:
   2905                 return self._getitem_multilevel(key)
-> 2906             indexer = self.columns.get_loc(key)
   2907             if is_integer(indexer):
   2908                 indexer = [indexer]

c:userskhushappdatalocalprogramspythonpython39libsite-packagespandascoreindexesase.py in get_loc(self, key, method, tolerance)
   2898                 return self._engine.get_loc(casted_key)
   2899             except KeyError as err:
-> 2900                 raise KeyError(key) from err
   2901 
   2902         if tolerance is not None:

KeyError: 0

Please answer, there can be a silly mistake because I am a beggineer in ML. It would be really helpful if you gave me some hint also.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The API of fetch_openml changed between versions. Originally, it returns a pandas.DataFrame which is exactly what you have. Since 0.24.0 (December 2020), as_frame argument of fetch_openml is set to False which gives you a numpy.ndarray. You should either convert your DataFrame to numpy.ndarray , see pandas's indexing method or upgrade sklearn.

fetch_openml reference


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...