简体   繁体   English

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

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

I am working my way down through the chapter 3 of book hands on machine learning with scikit learn.我正在学习使用 scikit learn 进行机器学习手册的第 3 章。 I have imported the mnist dataset, but when I am trying to get some image it is showing me an error: screenshot of the error and code!我已经导入了 mnist 数据集,但是当我尝试获取一些图像时,它向我显示一个错误:错误和代码的屏幕截图! I have import every libraries which I need to import, I have also pushed the code to my GitHub where you can see entire juypter notebook.我已经导入了我需要导入的每个库,我还将代码推送到了我的 GitHub 中,您可以在其中看到整个 juypter 笔记本。 link to GitHub notebook can anyone help me with this? 链接到 GitHub 笔记本有人可以帮我吗?

the code which I wrote is:我写的代码是:

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()

the error I am getting我得到的错误

 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

I would suggest a different way of resolving this, something that may be helpful in other machine learning projects when you work with Pandas.我会建议一种不同的方法来解决这个问题,当您使用 Pandas 时,这可能对其他机器学习项目有所帮助。 When slicing data, try to use iloc function instead.切片数据时,尝试使用iloc function 代替。 In your case, I have tried using X.iloc[0] with the same code and it works, no need to reinstall libraries.在您的情况下,我尝试使用具有相同代码的X.iloc[0]并且它可以工作,无需重新安装库。 And because during preprocessing, as well as splitting data into test/training sets, your indices will get mixed up, therefore calling a row by its label may not be wise.并且因为在预处理过程中,以及将数据拆分为测试/训练集,您的索引会混淆,因此通过其 label 调用一行可能是不明智的。

Use X.iloc[0] rather than X[0]使用X.iloc[0]而不是X[0]

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

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