为什么在 MNIST 分类器代码中使用 X[0] 会报错?
Why does using X[0] in MNIST classifier code give me an error?
我正在学习使用 MNIST 数据集进行分类。我遇到了一个错误,我无法弄清楚,我已经进行了很多 google 搜索,但我无能为力,也许您是专家可以帮助我。这是代码--
>>> from sklearn.datasets import fetch_openml
>>> mnist = fetch_openml('mnist_784', version=1)
>>> mnist.keys()
输出:
dict_keys(['data', 'target', 'frame', 'categories', 'feature_names', 'target_names', 'DESCR', 'details', 'url'])
>>> X, y = mnist["data"], mnist["target"]
>>> X.shape
输出:(70000, 784)
>>> y.shape
输出:(70000)
>>> X[0]
output:KeyError Traceback (most recent call last)
c:\users\khush\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2897 try:
-> 2898 return self._engine.get_loc(casted_key)
2899 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-10-19c40ecbd036> in <module>
----> 1 X[0]
c:\users\khush\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.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:\users\khush\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexes\base.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
请回答,可能会有一个愚蠢的错误,因为我是 ML 的初学者。如果您也给我一些提示,那将非常有帮助。
fetch_openml
的 API 版本之间发生了变化。在早期版本中,它是 returns 一个 numpy.ndarray
数组。自 0.24.0
(2020 年 12 月)起,fetch_openml
的 as_frame
参数设置为 auto
(而不是之前的默认选项 False
),这为您提供了 pandas.DataFrame
用于 MNIST 数据。您可以通过设置 as_frame = False
强制将数据读取为 numpy.ndarray
。参见 fetch_openml reference。
我也遇到了同样的问题
- scikit-学习:0.24.0
- matplotlib: 3.3.3
- Python: 3.9.1
我以前用下面的代码来解决这个问题。
import matplotlib as mpl
import matplotlib.pyplot as plt
# instead of some_digit = X[0]
some_digit = X.to_numpy()[0]
some_digit_image = some_digit.reshape(28,28)
plt.imshow(some_digit_image,cmap="binary")
plt.axis("off")
plt.show()
如果您遵循以下代码,则无需降级 scikit-learn 库:
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version= 1, as_frame= False)
mnist.keys()
我正在学习使用 MNIST 数据集进行分类。我遇到了一个错误,我无法弄清楚,我已经进行了很多 google 搜索,但我无能为力,也许您是专家可以帮助我。这是代码--
>>> from sklearn.datasets import fetch_openml
>>> mnist = fetch_openml('mnist_784', version=1)
>>> mnist.keys()
输出: dict_keys(['data', 'target', 'frame', 'categories', 'feature_names', 'target_names', 'DESCR', 'details', 'url'])
>>> X, y = mnist["data"], mnist["target"]
>>> X.shape
输出:(70000, 784)
>>> y.shape
输出:(70000)
>>> X[0]
output:KeyError Traceback (most recent call last)
c:\users\khush\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2897 try:
-> 2898 return self._engine.get_loc(casted_key)
2899 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-10-19c40ecbd036> in <module>
----> 1 X[0]
c:\users\khush\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.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:\users\khush\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexes\base.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
请回答,可能会有一个愚蠢的错误,因为我是 ML 的初学者。如果您也给我一些提示,那将非常有帮助。
fetch_openml
的 API 版本之间发生了变化。在早期版本中,它是 returns 一个 numpy.ndarray
数组。自 0.24.0
(2020 年 12 月)起,fetch_openml
的 as_frame
参数设置为 auto
(而不是之前的默认选项 False
),这为您提供了 pandas.DataFrame
用于 MNIST 数据。您可以通过设置 as_frame = False
强制将数据读取为 numpy.ndarray
。参见 fetch_openml reference。
我也遇到了同样的问题
- scikit-学习:0.24.0
- matplotlib: 3.3.3
- Python: 3.9.1
我以前用下面的代码来解决这个问题。
import matplotlib as mpl
import matplotlib.pyplot as plt
# instead of some_digit = X[0]
some_digit = X.to_numpy()[0]
some_digit_image = some_digit.reshape(28,28)
plt.imshow(some_digit_image,cmap="binary")
plt.axis("off")
plt.show()
如果您遵循以下代码,则无需降级 scikit-learn 库:
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version= 1, as_frame= False)
mnist.keys()