简体   繁体   中英

Transforming a gzip file into npy file

For my my ML model I need to open a gzip file and convert it to an array. My code looks like this:

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) )

At the moment I'm getting an error because I can't reshape the GZ file. Does anyone know how to create arrays or maybe there's another solution to run the code?

My error looks like this

 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' 

This code and error is not about loading a gzip file.

Evidently trainimages is a GzipFile object, but that's not what the load_data function produces. It must be left over from some earlier coding in the script.

Evidently load_data operates successfully on the npz file, which is a zip-archive (possibly with its own non-gzip compression). It returns 4 arrays, with names like x_train , etc. It's those arrays that may need reshaping (I'd check first), not the spurious trainimages .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM