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 [ ]:
 

发表评论

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

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