如何修复分类数据集 MNIST 中的以下错误:-
How to fix the following error in Classification dataset MNIST :-
我将 mnist 指定为:
mnist = fetch_openml('mnist_784', version = 1)
在探索 MNIST 数据集时,分配后:
X, y = mnist["data"], mnist["target"]
我试图抓取一个实例的特征向量,将其重塑为 28×28 数组,
在此之前我分配了:
some_digit = X[0]
我收到错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
c:\users\kanishk\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3079 try:
-> 3080 return self._engine.get_loc(casted_key)
3081 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-8-348a6e96ae02> in <module>
----> 1 some_digit = X[0]
c:\users\kanishk\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
3022 if self.columns.nlevels > 1:
3023 return self._getitem_multilevel(key)
-> 3024 indexer = self.columns.get_loc(key)
3025 if is_integer(indexer):
3026 indexer = [indexer]
c:\users\kanishk\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
-> 3082 raise KeyError(key) from err
3083
3084 if tolerance is not None:
KeyError: 0
我该如何解决?
您的 X
似乎是 pandas.DataFrame
,在 DataFrame
代码中 X[0]
搜索名称为 0
的列,但 X
没有有了。
如果你想获得编号为 0
的行,那么你可能需要 X.iloc[0]
顺便说一句:
当我运行
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version=1)
X, y = mnist["data"], mnist["target"]
print(type(X), X.shape) # <class 'numpy.ndarray'> (70000, 784)
some_digit = X[0]
print(type(some_digit), some_digit.shape) # <class 'numpy.ndarray'> (784,)
然后我得到 X
因为 numpy.array
和 X[0]
给了我期望值。
也许您在某个地方将 X
从 numpy.array
转换为 pandas.DataFrame
- 现在您必须使用不同的方法来访问数据。
我将 mnist 指定为:
mnist = fetch_openml('mnist_784', version = 1)
在探索 MNIST 数据集时,分配后:
X, y = mnist["data"], mnist["target"]
我试图抓取一个实例的特征向量,将其重塑为 28×28 数组, 在此之前我分配了:
some_digit = X[0]
我收到错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
c:\users\kanishk\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3079 try:
-> 3080 return self._engine.get_loc(casted_key)
3081 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-8-348a6e96ae02> in <module>
----> 1 some_digit = X[0]
c:\users\kanishk\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
3022 if self.columns.nlevels > 1:
3023 return self._getitem_multilevel(key)
-> 3024 indexer = self.columns.get_loc(key)
3025 if is_integer(indexer):
3026 indexer = [indexer]
c:\users\kanishk\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
-> 3082 raise KeyError(key) from err
3083
3084 if tolerance is not None:
KeyError: 0
我该如何解决?
您的 X
似乎是 pandas.DataFrame
,在 DataFrame
代码中 X[0]
搜索名称为 0
的列,但 X
没有有了。
如果你想获得编号为 0
的行,那么你可能需要 X.iloc[0]
顺便说一句:
当我运行
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784', version=1)
X, y = mnist["data"], mnist["target"]
print(type(X), X.shape) # <class 'numpy.ndarray'> (70000, 784)
some_digit = X[0]
print(type(some_digit), some_digit.shape) # <class 'numpy.ndarray'> (784,)
然后我得到 X
因为 numpy.array
和 X[0]
给了我期望值。
也许您在某个地方将 X
从 numpy.array
转换为 pandas.DataFrame
- 现在您必须使用不同的方法来访问数据。