将gzip文件转换为npy文件

时间:2019-05-23 07:53:01

标签: python numpy

对于我的ML模型,我需要打开一个gzip文件并将其转换为数组。 我的代码如下:

def load_data(path):
    with np.load(path) as f:
        x_train, y_train = f['x_train'], f['y_train']
        x_test, y_test = f['x_test'], f['y_test']
        return (x_train, y_train), (x_test, y_test)

(x_train, y_train), (x_test, y_test) = load_data('../input/mnist-numpy/mnist.npz')

x_train = trainimages.reshape(trainimages.shape[0],784)
y_train = trainimages.reshape(trainimages.shape[0],1)
x_test = testimages.reshape(testimages.shape[0],784)
y_test = testimages.reshape(testimages.shape[0],1)
MNIST_image = np.vstack( (x_train,x_test) )
MNIST_label = np.vstack( (y_train,y_test) )

由于无法重塑GZ文件,此刻出现错误。 有谁知道如何创建数组,或者还有另一种解决方案来运行代码?

我的错误看起来像这样

 Traceback (most recent call last): File "<ipython-input-18-c86c75005844>", line 1, in <module> 
    x_train = trainimages.reshape(trainimages.shape[0],784) 
 AttributeError: 'GzipFile' object has no attribute 'reshape' 

1 个答案:

答案 0 :(得分:0)

此代码和错误与加载gzip文件无关。

显然trainimagesGzipFile对象,但这不是load_data函数产生的。必须将其保留在脚本中的某些早期编码中。

显然,load_datanpz这个zip-archive文件上成功运行(可能具有自己的非gzip压缩)。它返回4个数组,名称类似x_train等。这些数组可能需要重塑(首先检查),而不是虚假的trainimages

相关问题