NumPy saves multiple arrays at once

I am working on various forms of arrays and I want to save them all with numpy.save , therefore, I think that I have

 mat1 = numpy.arange(8).reshape(4, 2) mat2 = numpy.arange(9).reshape(2, 3) numpy.save('mat.npy', numpy.array([mat1, mat2])) 

It works. But when I have two matrices with one size of the same size, it does not work.

 mat1 = numpy.arange(8).reshape(2, 4) mat2 = numpy.arange(10).reshape(2, 5) numpy.save('mat.npy', numpy.array([mat1, mat2])) 

It calls Traceback (most recent call last): File "<input>", line 1, in <module> ValueError: could not broadcast input array from shape (2,4) into shape (2)

Note that the problem is caused by numpy.array([mat1, mat2]) , not numpy.save

I know that such an array is possible:

>> numpy.array([[[1, 2]], [[1, 2], [3, 4]]]) array([[[1, 2]], [[1, 2], [3, 4]]], dtype=object)

So, all I want is to save two arrays at mat1 as mat1 and mat2 .

+7
python arrays numpy
source share
1 answer

If you want to save multiple arrays in the same format as np.save , use np.savez .

For example:

 import numpy as np arr1 = np.arange(8).reshape(2, 4) arr2 = np.arange(10).reshape(2, 5) np.savez('mat.npz', name1=arr1, name2=arr2) data = np.load('mat.npz') print data['name1'] print data['name2'] 

If you have multiple arrays, you can expand the arguments:

 import numpy as np data = [np.arange(8).reshape(2, 4), np.arange(10).reshape(2, 5)] np.savez('mat.npz', *data) container = np.load('mat.npz') data = [container[key] for key in container] 

Please note that the order is not maintained. If you need to keep order, you can use pickle instead.

If you use pickle , do not forget to specify the binary protocol, otherwise you will write things using abii pickle, which is especially inefficient for numpy arrays. With the binary protocol, ndarray more or less sorted in the same format as np.save / np.savez . For example:

 # Note: This is Python2.x specific. It identical except for the import on 3.x import cPickle as pickle import numpy as np data = [np.arange(8).reshape(2, 4), np.arange(10).reshape(2, 5)] with open('mat.pkl', 'wb') as outfile: pickle.dump(data, outfile, pickle.HIGHEST_PROTOCOL) with open('mat.pkl', 'rb') as infile: result = pickle.load(infile) 

In this case, result and data will have the same content, and the order of the input lists of arrays will be preserved.

+6
source share

All Articles