NumPy一次保存一些数组
我正在处理不同形状的数组,我想将它们全部保存为numpy.save
,因此,考虑到我拥有
I working on different shapes of arrays and I want to save them all with numpy.save
, so, consider 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 dimension of same size it's not working.
mat1 = numpy.arange(8).reshape(2, 4)
mat2 = numpy.arange(10).reshape(2, 5)
numpy.save('mat.npy', numpy.array([mat1, mat2]))
它导致Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: could not broadcast input array from shape (2,4) into shape (2)
It causesTraceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: could not broadcast input array from shape (2,4) into shape (2)
请注意,此问题是由numpy.array([mat1, mat2])
而不是numpy.save
And note that the problem caused by numpy.array([mat1, mat2])
and not by numpy.save
我知道这样的数组是可能的:
I know that such array is possible:
>> numpy.array([[[1, 2]], [[1, 2], [3, 4]]])
array([[[1, 2]], [[1, 2], [3, 4]]], dtype=object)
>> numpy.array([[[1, 2]], [[1, 2], [3, 4]]])
array([[[1, 2]], [[1, 2], [3, 4]]], dtype=object)
所以,我要做的就是一次将两个数组另存为mat1
和mat2
.
So, all of what I want is to save two arrays as mat1
and mat2
at once.
如果要以与 np.savez
.
If you'd like to save multiple arrays in the same format as np.save
, use np.savez
.
例如:
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 several 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]
请注意,订单不会保留.如果您确实需要保留订单,则可以考虑改用pickle
.
Note that the order is not preserved. If you do need to preserve order, you might consider using pickle
instead.
如果使用pickle
,请确保指定二进制协议,否则,您将使用ascii pickle编写东西,这对于numpy数组尤其无效.使用二进制协议,ndarray
或多或少会出现与[c7>/np.savez
相同的格式的泡菜.例如:
If you use pickle
, be sure to specify the binary protocol, otherwise the you'll write things using ascii pickle, which is particularly inefficient for numpy arrays. With a binary protocol, ndarray
s more or less pickle to the same format as np.save
/np.savez
. For example:
# Note: This is Python2.x specific. It's 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)
在这种情况下,result
和data
将具有相同的内容,并且将保留数组输入列表的顺序.
In this case, result
and data
will have identical contents and the order of the input list of arrays will be preserved.