【问题标题】:How to fix the following error in Classification dataset MNIST :-如何修复分类数据集 MNIST 中的以下错误:-
【发布时间】:2021-06-13 06:18:31
【问题描述】:

我将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

我该如何解决?

【问题讨论】:

  • 请提供预期的MRE - Minimal, Reproducible Example。显示中间结果与预期结果的偏差。我们应该能够将您的代码块粘贴到文件中,运行它并重现您的问题。这也让我们可以在您的上下文中测试任何建议。
  • 我们还希望您在错误点之前跟踪可疑值。您对他们如何获得这些价值观感到困惑?由于您没有提供足够的代码或预期的跟踪,我们无法为您提供太多帮助。显然,您对 Python 运行时系统有一个争论:您声称 X 是一个具有整数索引的序列; RTS 另有说法。猜猜谁赢了?
  • 先用print()查看X里面有什么——错误显示X里面没有0。似乎是 pandas DataFrameX[0] 搜索编号为 0 的列,但它没有。如果您想获得编号为0 的行,那么您可能需要X.iloc[0]

标签: python jupyter-notebook data-science


【解决方案1】:

您的X 似乎是pandas.DataFrame 并且在DataFrame 代码X[0] 搜索列名称为0X 没有它。

如果您想获得编号为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.arrayX[0] 给我预期的价值。

也许您在某个地方将Xnumpy.array 转换为pandas.DataFrame - 现在您必须使用不同的方法来访问数据。

【讨论】:

    猜你喜欢
    • 2019-12-08
    • 1970-01-01
    • 2021-03-07
    • 2021-04-08
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 2017-04-25
    相关资源
    最近更新 更多