Python:加载MNIST d时出错

2024-10-01 13:25:38 发布

您现在位置:Python中文网/ 问答频道 /正文

使用以下代码加载MNIST数据时出错。(anaconda已经在在线Jupyter笔记本上安装并编码。)

from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('MNIST original')

时间错误出现了,我不知道我在哪里犯了错误。我已经关闭了我的vpn代理,但它没有工作。救命啊!在

^{pr2}$

我下载了MNIST数据集并尝试自己加载数据。我复制了用于加载MNIST的代码,但再次加载数据失败。我认为我需要修改一些代码,而不是完全从互联网上复制代码,但我不知道我应该在哪里做更改(只是一个Python初学者) 我用来加载下载的MNIST的代码数据。是是因为我把数据放错了文件?在

def loadmnist(imagefile, labelfile):

    # Open the images with gzip in read binary mode
    images = open(imagefile, 'rb')
    labels = open(labelfile, 'rb')

    # Get metadata for images
    images.read(4)  # skip the magic_number
    number_of_images = images.read(4)
    number_of_images = unpack('>I', number_of_images)[0]
    rows = images.read(4)
    rows = unpack('>I', rows)[0]
    cols = images.read(4)
    cols = unpack('>I', cols)[0]

    # Get metadata for labels
    labels.read(4)
    N = labels.read(4)
    N = unpack('>I', N)[0]

    # Get data
    x = np.zeros((N, rows*cols), dtype=np.uint8)  # Initialize numpy array
    y = np.zeros(N, dtype=np.uint8)  # Initialize numpy array
    for i in range(N):
        for j in range(rows*cols):
            tmp_pixel = images.read(1)  # Just a single byte
            tmp_pixel = unpack('>B', tmp_pixel)[0]
            x[i][j] = tmp_pixel
        tmp_label = labels.read(1)
        y[i] = unpack('>B', tmp_label)[0]

    images.close()
    labels.close()
    return (x, y)

以上部分可以。在

train_img, train_lbl = loadmnist('data/train-images-idx3-ubyte'
                                 , 'data/train-labels-idx1-ubyte')
test_img, test_lbl = loadmnist('data/t10k-images-idx3-ubyte'
                               , 'data/t10k-labels-idx1-ubyte')

错误是这样的。在

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-5-b23a5078b5bb> in <module>()
      1 train_img, train_lbl = loadmnist('data/train-images-idx3-ubyte'
----> 2                                  , 'data/train-labels-idx1-ubyte')
      3 test_img, test_lbl = loadmnist('data/t10k-images-idx3-ubyte'
      4                                , 'data/t10k-labels-idx1-ubyte')

<ipython-input-4-967098b85f28> in loadmnist(imagefile, labelfile)
      2 
      3     # Open the images with gzip in read binary mode
----> 4     images = open(imagefile, 'rb')
      5     labels = open(labelfile, 'rb')
      6 

FileNotFoundError: [Errno 2] No such file or directory: 'data/train-images-idx3-ubyte'

我下载的数据放在我刚做的文件夹里。 enter image description here


Tags: 数据代码inreaddatalabelstraintmp
3条回答

您可以直接从sklearn数据集加载它。在

from sklearn import datasets
digits = datasets.load_digits()

或者你可以用Keras加载。在

^{pr2}$

另一个选择是只使用download the dataset并将其加载到类似pandas的内容中。在

df = pd.read_csv('filename.csv')

服务器已关闭一段时间,请参考thisGitHub线程中的一些解决方案,包括从Tensorflow导入或直接从其他来源导入。在

如果要直接从某个库加载数据集,而不是先下载再加载,请从Keras加载它。在

可以这样做

from keras.datasets import mnist

(X_train, y_train), (X_test, y_test) = mnist.load_data()

如果您是机器学习和Python的初学者,希望了解更多,我建议您看看{a1}优秀的博客文章。在

另外,在将文件传递给函数时,还需要文件的扩展名。你必须像这样调用函数。在

^{pr2}$

在用于从本地磁盘加载数据的代码中,它会抛出一个错误,因为该文件不在给定的位置。确保文件夹mnist位于您的笔记本所在的文件夹中。在

相关问题 更多 >