在numpy中创建具有不同维数的数组的数组 [英] Creating array of arrays in numpy with different dimensions
问题描述
我正在尝试创建一个由numpy数组组成的数组,每个数组都有不同的维数. 到目前为止,似乎还不错.例如,如果我运行:
I'm trying to create an array of numpy arrays, each one with a different dimension. So far, it seems to be fine. For example, if I run:
np.array([np.zeros((10,3)), np.zeros((11,8))])
结果是:
array([ array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]]),
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])], dtype=object)
两个矩阵的维数完全不同,并且可以毫无问题地生成数组.但是,如果两个矩阵的一维相同,则将不再起作用:
The dimension of the two matrices are completely different and the array is generated without any problem. However, if the first dimension of the two matrices is the same, it doesn't work anymore:
np.array([np.zeros((10,3)), np.zeros((10,8))])
Traceback (most recent call last):
File "<ipython-input-123-97301e1424ae>", line 1, in <module>
a=np.array([np.zeros((10,3)), np.zeros((10,8))])
ValueError: could not broadcast input array from shape (10,3) into shape (10)
这是怎么回事?
谢谢!
推荐答案
之前已对此进行了哈希处理(长度不等的数组中的numpy数组).基本上np.array
做以下三件事之一:
This has been hashed out before (Why do I get error trying to cast np.array(some_list) ValueError: could not broadcast input array;
Numpy array in array with unequal length). Basically np.array
does one of 3 things:
-
创建基本dtype的n维数组,例如漂浮.
make an n-dimensional array of a basic dtype, e.g. float.
创建对象dtype数组
make an object dtype array
提出一个错误,说前两个不可能.
raise an error, saying the first two are not possible.
后两种选择是后备选项,只有在第一种不可能的情况下才采用.
These second two alternatives are fallback options, taken only if the first is impossible.
不用深入研究编译代码如何工作的细节,显然会发生什么
Without digging into the details of how the compiled code works, apparently what happens with
np.array([np.zeros((10,3)), np.zeros((10,8))])
是因为它首先看到了共同的第一维度,并由此推断出可以采取第一选择.好像它初始化了(10,2)数组(列表中的2个项目),并试图将第一个数组放入第一行,因此尝试将(10,3)数组放入(10, )插槽.
is that it first sees the common first dimension, and deduces from that that it can take the first choice. It looks like it initialed a (10,2) array (2 items in your list), and tried to put the first array into the first row, hence the failed attempt to put a (10,3) array into a (10,) slot.
因此,如果您确实想要对象dtype数组,并且没有遇到第一种或第三种情况,则需要进行某种环回"创建.
So if you really want an object dtype array, and not hit either the 1st or 3rd cases, you need to do some sort of 'round-about' creation.
PaulP和我一直在>强制numpy创建对象数组中探索替代方法
PaulP and I have been exploring alternatives in Force numpy to create array of objects
更早的时间:如何创建列表的numpy数组?
在这个问题上,我建议进行以下迭代:
In this question I suggest this iteration:
A=np.empty((3,),dtype=object)
for i,v in enumerate(A): A[i]=[v,i]
或者您的情况
In [451]: res = np.empty(2, object)
In [452]: alist = [np.zeros((10,3)), np.zeros((10,8))]
In [453]: for i,v in enumerate(alist):
...: res[i] = v
与其在alist
上进行迭代,不如执行以下操作:
Rather than iterate on alist
, it may work to do:
res[:] = alist
在我尝试过的大多数情况下,它似乎都可以工作,但是如果您广播错误,也不要感到惊讶.
It seems to work in most cases that I've tried, but don't be surprised if you broadcasting errors.
这篇关于在numpy中创建具有不同维数的数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!