繁体   English   中英

如何解决 juypter notebook 中 pandas 的问题?

[英]How to resolve problems with pandas in juypter notebook?

我正在学习使用 scikit learn 进行机器学习手册的第 3 章。 我已经导入了 mnist 数据集,但是当我尝试获取一些图像时,它向我显示一个错误:错误和代码的屏幕截图! 我已经导入了我需要导入的每个库,我还将代码推送到了我的 GitHub 中,您可以在其中看到整个 juypter 笔记本。 链接到 GitHub 笔记本有人可以帮我吗?

我写的代码是:

some_digit = X[0]
some_digit_image = some_digit.reshape(28, 28)
plt.imshow(some_digit_image, cmap=mpl.cm.binary)
plt.axis("off")

save_fig("some_digit_plot")
plt.show()

我得到的错误

 KeyError Traceback (most recent call last) c:\python3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 3360 try: -> 3361 return self._engine.get_loc(casted_key) 3362 except KeyError as err: c:\python3\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() c:\python3\lib\site-packages\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) ~\AppData\Local\Temp/ipykernel_5048/2618338264.py in <module> ----> 1 some_digit = X[0] 2 some_digit_image = some_digit.reshape(28, 28) 3 plt.imshow(some_digit_image, cmap=mpl.cm.binary) 4 plt.axis("off") 5 c:\python3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key) 3453 if self.columns.nlevels > 1: 3454 return self._getitem_multilevel(key) -> 3455 indexer = self.columns.get_loc(key) 3456 if is_integer(indexer): 3457 indexer = [indexer] c:\python3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 3361 return self._engine.get_loc(casted_key) 3362 except KeyError as err: -> 3363 raise KeyError(key) from err 3364 3365 if is_scalar(key) and isna(key) and not self.hasnans: KeyError: 0

我会建议一种不同的方法来解决这个问题,当您使用 Pandas 时,这可能对其他机器学习项目有所帮助。 切片数据时,尝试使用iloc function 代替。 在您的情况下,我尝试使用具有相同代码的X.iloc[0]并且它可以工作,无需重新安装库。 并且因为在预处理过程中,以及将数据拆分为测试/训练集,您的索引会混淆,因此通过其 label 调用一行可能是不明智的。

使用X.iloc[0]而不是X[0]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM