Load Compressed Data using numpy.load

As data arrays get larger and larger, it becomes necessary to compress them if they are to be saved to a file. Data science and machine learning applications using numpy often deal with saving and loading compressed data in the form of files with .npz extensions. This material introduces how to load compressed numpy files.

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.

Source: newbedev, https://newbedev.com/load-compressed-data-npz-from-file-using-numpy-load
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 License.

Last modified: Friday, September 23, 2022, 2:10 PM