Loading arrays stored in NPY binary format

Independent of which function was used to generate a file containing NumPy arrays in NPY format, the load() function is used to retrieve the data from disk. In the following examples, we assume that the files array_x.npy, arrays_xyz.npz, and arrays_xyz_c.npz were generated, as in the previous examples. 

The single array contained in the array_x.npy file can be loaded with the following command:

x = np.load('array_x.npy')

This will open the file, read the array into the variable x, and close the file.

Loading arrays saved with savez() and savez_compressed() is slightly more complicated. The following code shows how to load the arrays stored in the arrays_xyz.npz file:

arrays = np.load('arrays_xyz.npz')
x, y, z = [arrays[name] for name in ['xfile', 'yfile', 'zfile']]

For an npz file, the load() function returns a dictionary-like object. The keys to the dictionary are the file names in the archive, and the values are in the corresponding NumPy array. If the key names are not known, they can be recovered with the keys() method, shown as follows:

arrays.keys()

In our example, this returns the following:

['xfile', 'yfile', 'zfile']
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset