+3 votes
in Programming Languages by (74.2k points)

I want to save several NumPy arrays to a file in compressed format. Also, when I load the arrays from the file, I should be able to access all arrays?
 

What functions should I use to save and load?

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

You can use the savez_compressed() function to save several Numpy arrays into a ".npz" file in compressed format. You can use the load() function to load arrays 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]])
>>> b=np.array([11,12,13,14])
>>> b
array([11, 12, 13, 14])
>>> np.savez_compressed("testfile.npz", a=a, b=b)
>>> v=np.load("testfile.npz")
>>> v['a']
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> v['b']
array([11, 12, 13, 14])


...