简体   繁体   English

numpy中数组的维数

[英]dimensions of array of arrays in numpy

I'd like to operate on "jagged arrays", and I prefer write "A + A" instead of "[x + y for x,y in zipped(A,A)]" 我想对“锯齿状数组”进行操作,并且我更喜欢写“ A + A”,而不是“ [zipped(A,A)中x,y的[x + y]”

For that I'd like to convert list of arrays of different sizes into an overall numpy array, but ran into an error due to seemingly over-zealous broadcasting (notice the first three succeeded, but the last one failed): 为此,我想将不同大小的数组列表转换为整个numpy数组,但是由于看似过度的广播而遇到错误(请注意前三个成功,但最后一个失败):

In[209]: A = array([ones([3,3]), array([1, 2])])
In[210]: A = array([ones([3,3]), array([1, 2])], dtype=object)
In[211]: A = array([ones([3,2]), array([1, 2])], dtype=object)
In[212]: A = array([ones([2,2]), array([1, 2])], dtype=object)
Traceback (most recent call last):
  File "/home/hzhang/.conda/envs/myenv/lib/python3.4/site-
packages/IPython/core/interactiveshell.py", line 2881, in run_code
  exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-212-7297723106f9>", line 1, in <module>
  A = array([ones([2,2]), array([1, 2])], dtype=object)
ValueError: could not broadcast input array from shape (2,2) into shape (2)

Help? 救命?

Your case is a variant on the 3rd case in my answer to 您的情况是我回答的第三种情况的变体

How to keep numpy from broadcasting when creating an object array of different shaped arrays 创建不同形状的数组的对象数组时,如何防止numpy广播

np.array tries to create a multidimensional array of numbers from the input list. np.array尝试从输入列表中创建数字的多维数组。 If the component dimensions are sufficiently different it resorts to keeping the arrays separate, making an object array instead. 如果组件尺寸足够不同,则将阵列分开,而改为对象阵列。 I think of this kind of array as a glorified/debased list. 我认为这种数组是光荣/降级的列表。

How to store multiple numpy 1d arrays with different lengths and print it 如何存储多个具有不同长度的numpy 1d数组并打印

In your problem case, the dimensions are close enough that it 'thinks' it can make a 2d array, but when it starts to fill in those values it finds that it can't broadcast values to do so, and so throws the error. 在您遇到问题的情况下,尺寸足够接近,以至于它“认为”它可以制成2d数组,但是当它开始填充这些值时,会发现它无法广播值,从而引发错误。 One could argue that it should have backtracked and taken the 'object' array route. 有人可能会辩称它应该回溯并采用“对象”阵列路线。 But that decision tree is buried deep in compiled code. 但是该决策树深深地埋在了已编译的代码中。

The problem case in that earlier SO question was 在较早的SO问题中的问题案例是

np.array([np.zeros((2, 2)), np.zeros((2,3))])

The 1st dimensions match, but the 2nd don't. 第一个尺寸匹配,但第二个不匹配。 I'm not entirely sure why your IN[211] works but In[212] does not. 我不确定您的IN[211]为何起作用,但是In[212]无效。 But the error message is the same, right down to the (2,2) => (2) attempt. 但是错误消息是相同的,一直到(2,2)=>(2)尝试。

edit 编辑

oops - I first read your problem example as: 糟糕-我首先将您的问题示例读取为:

np.array([np.ones([2,2]), np.ones([1, 2])], dtype=object)

That is, combining a (2,2) with (1,2), which does produce a (2,) object. 也就是说,将(2,2)与(1,2)组合在一起,这确实会产生(2,)对象。 What you are actually combine is a 你实际上结合的是

 (2,2) with a (2,) 

So it looks like the target is np.empty((2,2),float) (or object ), because out[...]=[ones([2,2]), array([1,2])] produces this error. 所以看起来目标是np.empty((2,2),float) (或object ),因为out[...]=[ones([2,2]), array([1,2])]产生此错误。


In any case the most reliable way of creating an object array is to initialize it, and copy the arrays. 无论如何,创建对象数组的最可靠方法是对其进行初始化,然后复制该数组。

Out[90]: array([None, None], dtype=object)
In [91]: arr[:]=[ones([2,2]), array([1, 2])]
In [92]: arr
Out[92]: 
array([array([[ 1.,  1.],
       [ 1.,  1.]]), array([1, 2])], dtype=object)

Be cautious about doing math on object arrays like this. 对像这样的对象数组进行数学运算时要小心。 What works is hit-or-miss: 起作用的是命中注定:

In [93]: A+A
Out[93]: 
array([array([[ 2.,  2.],
       [ 2.,  2.],
       [ 2.,  2.]]),
       array([2, 4])], dtype=object)

In [96]: np.min(A[1])
Out[96]: 1
In [97]: np.min(A)
....
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [98]: A.sum()
Out[98]: 
array([[ 2.,  3.],
       [ 2.,  3.],
       [ 2.,  3.]])

this works because A[0]+A[1] works. 这是有效的,因为A[0]+A[1]有效。 A[1] is (2,) which broadcasts to (3,2). A[1]是广播到(3,2)的(2,)。

With object arrays numpy resorts to some sort of list comprehension, iterating over the object elements. 使用对象数组时, numpy依靠某种列表理解来遍历对象元素。 So may get the convenience of array notation, but not the same speed as you would with a true 2d array. 这样可能会获得数组表示法的便利,但是速度与真正的2d数组的速度不一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM