Python NumPy 一次性保存一些数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35133317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:01:49  来源:igfitidea点击:

NumPy save some arrays at once

pythonarraysnumpy

提问by Dubon

I working on different shapes of arrays and I want to save them all with numpy.save, so, consider I have

我处理不同形状的数组,我想用 保存它们numpy.save,所以,考虑我有

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]))

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

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

And note that the problem caused by numpy.array([mat1, mat2])and not by numpy.save

并注意问题是由numpy.array([mat1, mat2])而不是由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)

So, all of what I want is to save two arrays as mat1and mat2at once.

因此,所有我想要的是两个数组保存为mat1mat2一次。

采纳答案by Joe Kington

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

如果您想以与 相同的格式保存多个数组np.save,请使用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 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]

Note that the order is not preserved. If you do need to preserve order, you might consider using pickleinstead.

请注意,不会保留顺序。如果确实需要保留顺序,则可以考虑pickle改用。

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, ndarrays more or less pickle to the same format as np.save/np.savez. For example:

如果您使用pickle,请务必指定二进制协议,否则您将使用 ascii pickle 编写内容,这对于 numpy 数组尤其低效。使用二进制协议,ndarrays 或多或少会选择与np.save/相同的格式np.savez。例如:

# 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)

In this case, resultand datawill have identical contents and the order of the input list of arrays will be preserved.

在这种情况下,resultdata将具有相同的内容,并且将保留数组输入列表的顺序。