简体   繁体   English

在numpy中广播数组

[英]broadcasting arrays in numpy

I got an array and reshaped it to the following dimentions: (-1,1,1,1) and (-1,1): 我得到一个数组并将其重塑为以下尺寸:(-1,1,1,1)和(-1,1):

 Array A:
[-0.888788523827  0.11842529285   0.319928774626  0.319928774626  0.378755429421  1.225877519716  3.830653798838]

A.reshape(-1,1,1,1):
[[[[-0.888788523827]]]


 [[[ 0.11842529285 ]]]


 [[[ 0.319928774626]]]


 [[[ 0.319928774626]]]


 [[[ 0.378755429421]]]


 [[[ 1.225877519716]]]


 [[[ 3.830653798838]]]]

A.reshape(-1,1):
[[-0.888788523827]
 [ 0.11842529285 ]
 [ 0.319928774626]
 [ 0.319928774626]
 [ 0.378755429421]
 [ 1.225877519716]
 [ 3.830653798838]]

Then I have done substractig and broadcasting came in, so my resulting matrix is 7x1x7x1. 然后我完成了减法运算,然后广播开始了,所以我得到的矩阵是7x1x7x1。

I have a hard time to visualize the intermediate step what broadcasting does. 我很难想象广播的中间步骤。 I mean I cannot imagine what elements of arrays are repeated and what they look like while broadcasting. 我的意思是我无法想象重复播放数组的哪些元素以及它们在广播时的外观。 Could somebody shed some light on this problem,please? 请问有人可以阐明这个问题吗?

In [5]: arr = np.arange(4)
In [6]: A = arr.reshape(-1,1,1,1)
In [7]: B = arr.reshape(-1,1)
In [8]: C = A + B
In [9]: C.shape
Out[9]: (4, 1, 4, 1)
In [10]: A.shape
Out[10]: (4, 1, 1, 1)
In [11]: B.shape
Out[11]: (4, 1)

There are 2 basic broadcasting rules: 有2条基本广播规则:

  • expand the dimensions to match - by adding size 1 dimensions at the start 扩大尺寸以匹配-在开始时添加尺寸1的尺寸
  • adjust all size 1 dimensions to match 调整所有尺寸1的尺寸以匹配

So in this example: 因此,在此示例中:

 (4,1,1,1) + (4,1)
 (4,1,1,1) + (1,1,4,1)    # add 2 size 1's to B
 (4,1,4,1) + (4,1,4,1)    # adjust 2 of the 1's to 4
 (4,1,4,1)

The first step is, perhaps, the most confusing. 第一步也许是最令人困惑的。 The (4,1) is expanded to (1,1,4,1), not (4,1,1,1). (4,1)扩展为(1,1,4,1),而不是(4,1,1,1)。 The rule is intended to avoid ambiguity - by expanding in a consistent manner, not necessarily what a human might intuitively want. 该规则旨在避免歧义-通过以一致的方式扩展,不一定是人类可能会直观地想要的。

Imagine the case where both arrays need expansion to match, and it could add a dimension in either direction: 想象一下两个数组都需要扩展才能匹配的情况,并且可以在任一方向上添加维:

 (4,) and (3,)
 (1,4) and (3,1)  or (4,1) and (1,3)
 (3,4)            or (4,3)
 confusion

The rule requires that the programmer choose which one expands to the right (4,1) or (3,1). 该规则要求程序员选择哪个扩展到右边的(4,1)或(3,1)。 numpy can then unambiguously add the other. numpy然后可以明确地添加另一个。


For a simpler example: 举一个简单的例子:

In [22]: A=np.arange(3).reshape(-1,1)
In [23]: B=np.arange(3)
In [24]: C = A+B   (3,1)+(3,) => (3,1)+(1,3) => (3,3)
In [25]: C
Out[25]: 
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4]])
In [26]: C.shape
Out[26]: (3, 3)

The [0,2,4] are present, but on the diagonal of C . [0,2,4]存在,但在C的对角线上。

When broadcasting like this, the result is a kind of outer sum: 像这样广播时,结果是一种outer和:

In [27]: np.add.outer(B,B)
Out[27]: 
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4]])

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

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