前提
MacBook を使用し、anaconda、Jupiter notebookでコードを書いています。
python ライブラリの使い方という書籍の機械学習の章で
minstのデータを使用して画像認識プログラムの学習をする項目があるのですが
書籍どおりのソースコードを入力してもFileNotFoundErrorが出てしまいます
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- このソースコードを動作できるようにしたい
発生している問題・エラーメッセージ
FileNotFoundError Traceback (most recent call last)
Input In [1], in <cell line: 30>()
25 images = np.frombuffer(imgpath.read(), dtype=np.uint8,
26 offset=16).reshape(len(labels), 784)
28 return images, labels
---> 30 x_train, y_train = load_mnist('data', kind='train')
31 x_test, y_test = load_mnist('data', kind='t10k')
33 print('訓練', x_train.shape,y_train.shape)
Input In [1], in load_mnist(path, kind)
13 labels_path = os.path.join(path,
14 '%s-labels-idx1-ubyte.gz'
15 % kind)
16 images_path = os.path.join(path,
17 '%s-images-idx3-ubyte.gz'
18 % kind)
---> 20 with gzip.open(labels_path, 'rb') as lbpath:
21 labels = np.frombuffer(lbpath.read(), dtype=np.uint8,
22 offset=8)
24 with gzip.open(images_path, 'rb') as imgpath:
File ~/opt/anaconda3/lib/python3.9/gzip.py:58, in open(filename, mode, compresslevel, encoding, errors, newline)
56 gz_mode = mode.replace("t", "")
57 if isinstance(filename, (str, bytes, os.PathLike)):
---> 58 binary_file = GzipFile(filename, gz_mode, compresslevel)
59 elif hasattr(filename, "read") or hasattr(filename, "write"):
60 binary_file = GzipFile(None, gz_mode, compresslevel, filename)
File ~/opt/anaconda3/lib/python3.9/gzip.py:173, in GzipFile.init(self, filename, mode, compresslevel, fileobj, mtime)
171 mode += 'b'
172 if fileobj is None:
--> 173 fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
174 if filename is None:
175 filename = getattr(fileobj, 'name', '')
FileNotFoundError: [Errno 2] No such file or directory: 'data/train-labels-idx1-ubyte.gz'

該当のソースコード
python
1import os 2import gzip 3import numpy as np 4import matplotlib.pyplot as plt 5 6 7def load_mnist(path, kind='train'): 8 import os 9 import gzip 10 import numpy as np 11 12 """Load MNIST data from `path`""" 13 labels_path = os.path.join(path, 14 '%s-labels-idx1-ubyte.gz' 15 % kind) 16 images_path = os.path.join(path, 17 '%s-images-idx3-ubyte.gz' 18 % kind) 19 20 with gzip.open(labels_path, 'rb') as lbpath: 21 labels = np.frombuffer(lbpath.read(), dtype=np.uint8, 22 offset=8) 23 24 with gzip.open(images_path, 'rb') as imgpath: 25 images = np.frombuffer(imgpath.read(), dtype=np.uint8, 26 offset=16).reshape(len(labels), 784) 27 28 return images, labels 29 30x_train, y_train = load_mnist('data', kind='train') 31x_test, y_test = load_mnist('data', kind='t10k') 32 33print('訓練', x_train.shape,y_train.shape) 34print('テスト', x_train.shape, y_test.shape) 35print('最初のラベル', y_train[0]) 36plt.matshow(x_train[0], cmap='gray') 37plt.show()
試したこと
同じ階層にデータがあるかを確認。
dataファイルとコードの名前を変更。
書籍に記載されているurl
https://github.com/zalandoresearch/fashion-mnist/blob/master/utils/mnist_reader.py
からコピペを試しました
補足情報(FW/ツールのバージョンなど)

回答1件
あなたの回答
tips
プレビュー