Load compressed data (.npz) from file using numpy.load Load compressed data (.npz) from file using numpy.load numpy numpy

Load compressed data (.npz) from file using numpy.load


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


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.


Try opening the file as binary:

with open('afile','rb') as f: