Key error 0 原因がわからない

MNISTのデータをダウンロードし、一つ目のデータを可視化する際に以下のエラーが出ます。

該当のソースコード

python

1from sklearn import datasets 2import matplotlib.pyplot as plt 3import numpy as np 4 5mnist = datasets.fetch_openml('mnist_784', version=1, data_home=".") # data_homeは保存先を指定します6 7X = mnist.data / 255 # 0-255を0-1に正規化8y = mnist.target 9 10y = np.array(y)11y = y.astype(np.int32)12 13 14plt.imshow(X[0].reshape(28, 28), cmap='gray')15print("この画像データのラベルは{:.0f}です".format(y[0]))

発生している問題・エラーメッセージ

Traceback (most recent call last): File "C:\Users\phanton\anaconda3\envs\deep\lib\site-packages\pandas\core\indexes\base.py", line 3803, in get_loc return self._engine.get_loc(casted_key) File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 165, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 0 The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:/Users/phanton/PycharmProjects/pythonProject1/????/MNIST.py", line 36, in <module> plt.imshow(X[0].reshape(28, 28), cmap='gray') File "C:\Users\phanton\anaconda3\envs\deep\lib\site-packages\pandas\core\frame.py", line 3805, in __getitem__ indexer = self.columns.get_loc(key) File "C:\Users\phanton\anaconda3\envs\deep\lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc raise KeyError(key) from err KeyError: 0

モジュールの互換性が原因なのか調べましたが、そのような情報はありませんでした。

原因究明のためにご教授願います。

補足情報(FW/ツールのバージョンなど)

python 3.8
pandas1.5.2
matplotloib 3.6.2

コメントを投稿

0 コメント