30

机器学习: NumPy 102 - Cloud-Native 产品级敏捷

 4 years ago
source link: https://www.deva9.com/ml/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0-numpy-102/?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK