numpy中数组数组的维数

时间:2023-01-21 12:06:10

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”而不是“[x + y代表x,y代表拉链(A,A)]”

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?

1 个解决方案

#1


1  

Your case is a variant on the 3rd case in my answer to

在我的回答中,您的案例是第3个案例的变体

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. 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.

np.array尝试从输入列表中创建一个多维数字数组。如果组件尺寸足够不同,它会使数组保持分离,从而形成对象数组。我认为这种阵列是一个美化/贬值的列表。

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. 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. But the error message is the same, right down to the (2,2) => (2) attempt.

第一维度匹配,但第二维度不匹配。我不完全确定为什么你的IN [211]有效,但在[212]没有。但错误信息是相同的,直到(2,2)=>(2)尝试。

edit

oops - I first read your problem example as:

oops - 我首先将您的问题示例读作:

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. What you are actually combine is a

也就是说,将(2,2)与(1,2)组合,产生(2)对象。你实际上结合的是一个

 (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)(或对象),因为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[1] is (2,) which broadcasts to (3,2).

这是有效的,因为A [0] + A [1]有效。 A [1]是(2,),广播到(3,2)。

With object arrays numpy resorts to some sort of list comprehension, iterating over the object elements. So may get the convenience of array notation, but not the same speed as you would with a true 2d array.

使用对象数组numpy转向某种列表理解,迭代对象元素。因此可能会获得数组表示法的便利,但速度与使用真正的2d数组的速度不同。

#1


1  

Your case is a variant on the 3rd case in my answer to

在我的回答中,您的案例是第3个案例的变体

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. 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.

np.array尝试从输入列表中创建一个多维数字数组。如果组件尺寸足够不同,它会使数组保持分离,从而形成对象数组。我认为这种阵列是一个美化/贬值的列表。

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. 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. But the error message is the same, right down to the (2,2) => (2) attempt.

第一维度匹配,但第二维度不匹配。我不完全确定为什么你的IN [211]有效,但在[212]没有。但错误信息是相同的,直到(2,2)=>(2)尝试。

edit

oops - I first read your problem example as:

oops - 我首先将您的问题示例读作:

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. What you are actually combine is a

也就是说,将(2,2)与(1,2)组合,产生(2)对象。你实际上结合的是一个

 (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)(或对象),因为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[1] is (2,) which broadcasts to (3,2).

这是有效的,因为A [0] + A [1]有效。 A [1]是(2,),广播到(3,2)。

With object arrays numpy resorts to some sort of list comprehension, iterating over the object elements. So may get the convenience of array notation, but not the same speed as you would with a true 2d array.

使用对象数组numpy转向某种列表理解,迭代对象元素。因此可能会获得数组表示法的便利,但速度与使用真正的2d数组的速度不同。