Q&A
[作りながら学ぶ!深層強化学習{PyTorchによる実践プログラミング}]
にあるコードを入力しています。
MNISTダウンロードする必要があり
python
1 2#手書き数字の画像データMNISTをダウンロード 3from sklearn.datasets import fetch_openml 4mnist = fetch_openml('mnist_784', version=1) 5 6#1. データの前処理(画像データとラベルに分割し。正規化) 7X = mnist.data / 255 #0-255を0-1に正規化 8y = mnist.target 9 10#MNISTのデータの一つ目を可視化する 11import matplotlib.pyplot as plt 12%matplotlib inline 13 14plt.imshow(X[0].reshape(28, 28), cmap='gray') 15print("この画像データのラベルは{:.0f}です".format(y[0]))
と入力したところ
KeyErorr
1KeyError Traceback (most recent call last) 2File ~\anaconda3\envs\rl_env\lib\site-packages\pandas\core\indexes\base.py:3621, in Index.get_loc(self, key, method, tolerance) 3 3620 try: 4-> 3621 return self._engine.get_loc(casted_key) 5 3622 except KeyError as err: 6 7File ~\anaconda3\envs\rl_env\lib\site-packages\pandas\_libs\index.pyx:136, in pandas._libs.index.IndexEngine.get_loc() 8 9File ~\anaconda3\envs\rl_env\lib\site-packages\pandas\_libs\index.pyx:163, in pandas._libs.index.IndexEngine.get_loc() 10 11File pandas\_libs\hashtable_class_helper.pxi:5198, in pandas._libs.hashtable.PyObjectHashTable.get_item() 12 13File pandas\_libs\hashtable_class_helper.pxi:5206, in pandas._libs.hashtable.PyObjectHashTable.get_item() 14 15KeyError: 0 16 17The above exception was the direct cause of the following exception: 18 19KeyError Traceback (most recent call last) 20Input In [3], in <cell line: 5>() 21 2 import matplotlib.pyplot as plt 22 3 get_ipython().run_line_magic('matplotlib', 'inline') 23----> 5 plt.imshow(X[0].reshape(28, 28), cmap='gray') 24 6 print("この画像データのラベルは{:.0f}です".format(y[0])) 25 26File ~\anaconda3\envs\rl_env\lib\site-packages\pandas\core\frame.py:3505, in DataFrame.__getitem__(self, key) 27 3503 if self.columns.nlevels > 1: 28 3504 return self._getitem_multilevel(key) 29-> 3505 indexer = self.columns.get_loc(key) 30 3506 if is_integer(indexer): 31 3507 indexer = [indexer] 32 33File ~\anaconda3\envs\rl_env\lib\site-packages\pandas\core\indexes\base.py:3623, in Index.get_loc(self, key, method, tolerance) 34 3621 return self._engine.get_loc(casted_key) 35 3622 except KeyError as err: 36-> 3623 raise KeyError(key) from err 37 3624 except TypeError: 38 3625 # If we have a listlike key, _check_indexing_error will raise 39 3626 # InvalidIndexError. Otherwise we fall through and re-raise 40 3627 # the TypeError. 41 3628 self._check_indexing_error(key) 42 43KeyError: 0
と出ました
簡単に言えば、MNISTのダウンロードがうまくいってないってことでしょうか。
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/06/11 07:29
2022/06/11 07:47
2022/06/11 08:20 編集