+3 votes
in Programming Languages by (17.0k points)
edited by
I want to save a NumPy array to a file on the disk so that I can load it later. The file should be compressed.

What functions should I use to save and load?

1 Answer

0 votes
by (28.9k points)

You can use the savez_compressed() function to save a Numpy array into a ".npz" file in compressed format. You can use the load() function to load the array from the compressed ".npz" file.

Here is an example:

>>> import numpy as np
>>> a=np.array([[1,2],[3,4],[5,6]])
>>> a
array([[1, 2],
       [3, 4],
       [5, 6]])

>>> np.savez_compressed("testfile.npz", a=a)
>>> v=np.load("testfile.npz")
>>> v['a']
array([[1, 2],
       [3, 4],
       [5, 6]])