0 0
Read Time:185 Minute, 21 Second
NumPy Array Broadcasting

NumPy Array Broadcasting

Broadcasting is the name given to the method that NumPy uses to allow array arithmetic between arrays with a different shape or size. Broadcasting has the following 1imitations:
  1. at least one array dimension is equal to 1.
  2. The number of columns in the each of arrays must be equal.

Scalar and One-Dimensional Array

After the broadcasting, Scalar b will be array([2,2,2]).
In [26]:
from numpy import array

a = array([1,2,3])
b = 2

c = a + b
print(c)
[3 4 5]

Scalar and Two-Dimensional Array

After the broadcasting, Scalar b will be array([[5,5,5], [5,5,5]]).
In [27]:
from numpy import array

a = array([[1,2,3],
           [4,5,6]]) 

b = 5

c = a + b
print(c)
[[ 6  7  8]
 [ 9 10 11]]

One-Dimensional and Three-Dimensional Arrays

After the broadcasting, array b will be array([[7,8,9], [7,8,9],[7,8,9]) .
In [40]:
from numpy import array 

a = array([[1,2,3],
           [4,5,6],
           [10,11,12]]) 
print(a.shape)

b = array([7, 8, 9])
print(b.shape)

c = a + b
print(c)
(3, 3)
(3,)
[[ 8 10 12]
 [11 13 15]
 [17 19 21]]

Limitations of Broadcasting

The broadcasting fails; array a and array b have the different number of columns.
In [29]:
from numpy import array 

a = array([[1,2],
           [4,5],
           [7,8]]) 
print(a.shape)

b = array([7, 8, 9])
print(b.shape)

c = a + b
print(c)
(3, 2)
(3,)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-3ba81839587f> in <module>
      9 print(b.shape)
     10 
---> 11 c = a + b
     12 print(c)

ValueError: operands could not be broadcast together with shapes (3,2) (3,)
The broadcasting fails; array a and array b have the different number of columns.
In [34]:
from numpy import array 

a = array([[1,2,3],
           [4,5,6],
           [7,8,9]]) 
print(a.shape)

b = array([7,8])
print(b.shape)

c = a + b
print(c)
(3, 3)
(2,)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-34-eb2490e96cdd> in <module>
      9 print(b.shape)
     10 
---> 11 c = a + b
     12 print(c)

ValueError: operands could not be broadcast together with shapes (3,3) (2,)
The broadcasting fails; none of array dimensions are equal to 1, althought, array a and array b have the same number of columns.
In [31]:
from numpy import array 

a = array([[1,2,3],
           [4,5,6],
           [7,8,9]]) 
print(a.shape)

b = array([[10,11,12],
           [13,14,15]])
print(b.shape)

c = a + b
print(c)
(3, 3)
(2, 3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-31-f019a163ba5b> in <module>
     10 print(b.shape)
     11 
---> 12 c = a + b
     13 print(c)

ValueError: operands could not be broadcast together with shapes (3,3) (2,3)
In [ ]:
 

About Post Author

方俊贤; Ken Fang

专利号: 201910652769.4; 一种深度学习的算法, 预测微服务持续发布、持续部署后对产品整体质量的影响, 获得国家知识财产局专利; 符合专利法实施细则第 44 条的规定。
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据