簡體   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