【发布时间】: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。似乎是 pandasDataFrame和X[0]搜索编号为0的列,但它没有。如果您想获得编号为0的行,那么您可能需要X.iloc[0]
标签: python jupyter-notebook data-science