当我尝试导入两个用于Python培训的文本文件时,我收到以下错误:
Traceback (most recent call last):
File "C:\Users\user\Desktop\test.py", line 31, in <module>
X_train, y_train = load_mnist('mnist', kind='train')
File "C:\Users\user\Desktop\test.py", line 27, in load_mnist
dtype=np.uint8).reshape(len(labels), 784)
ValueError: total size of new array must be unchanged
正在运行的代码部分是:
import os
import struct
import numpy as np
from scipy.special import expit
import sys
import matplotlib.pyplot as plt
def load_mnist(path, kind='train'):
"""Load MNIST data from `path`"""
labels_path = os.path.join(path,'%s-labels-idx1-ubyte' % kind)
images_path = os.path.join(path, '%s-images-idx3-ubyte' % kind)
with open('C:/Users/user/~MNISTnumLabels5000.txt', 'rb') as lbpath:
magic, n = struct.unpack('>II', lbpath.read(8))
labels = np.fromfile(lbpath, dtype=np.uint8)
with open('C:/Users/user/~MNISTnumImages5000.txt', '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
X_train, y_train = load_mnist('mnist', kind='train')
print('Rows: %d, columns: %d' % (X_train.shape[0], X_train.shape[1]))
MNISTnumImages5000.txt文件具有5,000个数字的数据,每个数字是尺寸为28×28像素的灰度图像(即,每个784像素)。数据文件的每一行都有784个值,表示0到9之间一位数的图像强度。
MNISTnumLabels5000.txt文件每行有一个整数,表示 在图像数据文件的相应行中更正图像的标签。因此,第一个条目&#39; 7&#39;表示图像数据文件的第一行具有手写数字7的数据。