Skip to content Skip to sidebar Skip to footer

Load Compressed Data (.npz) From File Using Numpy.load

I have an array: >>> data = np.ones((1,3,128)) I save it to file using savez_compressed: >>> with open('afile','w') as f: np.savez_compressed(f,data=data

Solution 1:

When using numpy.load you can pass the file name, and if the extension is .npz, it will first decompress the file:

np.savez_compressed('filename.npz', array1=array1, array2=array2)
b = np.load('filename.npz')

and do b['array1'] and so forth to retrieve the data from each array...

Solution 2:

You can also use the f attribute, which leaves you with a np.ndarray:

images = np.load('images.npz')
images = images.f.arr_0

The name/key of the array inside the .npz-file (e.g. arr_0) can be found through

images.keys()

Note: The f attribute is not documented in the docstring of load. When load reads an npz file, it returns an instance of the class NpzFile. This class is available as numpy.lib.npyio.NpzFile. The docstring of the NpzFile class describes the f attribute. (As of this writing, the source code of the class can be found here.

Solution 3:

Try opening the file as binary:

withopen('afile','rb') as f:

Solution 4:

I do have the same problem(AssertionError) when using numpy 1.7.1/1.8.0 with python 2.7.6 both on MAC OS and Windows. But the problem was automatically fixed after I switch to linux with python 2.7.5. Then I reinstall python 2.7.5 on MACOS and Windows and all the problem was gone. Basically the problem is with python instead of numpy as the compiler is sending alert.So mightly the version matters.

But though npy is the serializable type of numpy, I don't think the file is small enough even with savez_compressed for large matrix.

Hopefully your problem is the same with mine

Post a Comment for "Load Compressed Data (.npz) From File Using Numpy.load"