我在D盘中解压了这4个文件:
train-images-idx3-ubytetrain-labels-idx1-ubyte t10k-images-idx3-ubytet10k-labels-idx1-ubyte
我遇到了以下错误:
FileNotFoundError: [Errno 2] No such file or directory: 'D:/train-labels-idx1-ubyte'
我的代码如下:
def load_mnist(path, kind='train'): """Load MNIST data from `path`""" path = "D:/" labels_path = os.path.join(path, '%s-labels-idx1-ubyte' % kind) images_path = os.path.join(path, '%s-images-idx3-ubyte' % kind) with open(labels_path, 'rb') as lbpath: magic, n = struct.unpack('>II', lbpath.read(8)) labels = np.fromfile(lbpath, dtype=np.uint8) with open(images_path, 'rb') as imgpath: magic, num, rows, cols = struct.unpack(">IIII", imgpath.read(16)) images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784) return images, labels
完整的错误信息如下:
Traceback (most recent call last): File "C:/Users/PycharmProjects/MachineLearning/NN2.py", line 28, in <module> X_train, y_train = load_mnist('mnist/', kind='train') File "C:/Users/PycharmProjects/MachineLearning/NN2.py", line 14, in load_mnist with open(labels_path, 'rb') as lbpath:FileNotFoundError: [Errno 2] No such file or directory: 'D:/train-labels-idx1-ubyte'
这原本是教科书中的代码:https://github.com/rasbt/python-machine-learning-book/blob/master/code/ch12/ch12.ipynb
回答:
这可能是一个路径问题。你可以尝试通过命令行终端进入你的D:
文件夹,然后在那里打开Python解释器,执行
以显示D:
下的所有文件和文件夹。然后,你可以检查train-labels-idx1-ubyte文件是否确实存在以及它的拼写是否正确。