對兩個數組使用廣播機制要遵守下列規則:
- 如果數組的秩不同,使用1來將秩較小的數組進行擴展,直到兩個數組的尺寸的長度都一樣。
- 如果兩個數組在某個維度上的長度是一樣的,或者其中一個數組在該維度上長度為1,那么我們就說這兩個數組在該維度上是相容的。
- 如果兩個數組在所有維度上都是相容的,他們就能使用廣播。
- 如果兩個輸入數組的尺寸不同,那么注意其中較大的那個尺寸。因為廣播之后,兩個數組的尺寸將和那個較大的尺寸一樣。
- 在任何一個維度上,如果一個數組的長度為1,另一個數組長度大於1,那么在該維度上,就好像是對第一個數組進行了復制。
作者:杜客 鏈接:https://zhuanlan.zhihu.com/p/20878530 來源:知乎 著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。 import numpy as np # Compute outer product of vectors v = np.array([1,2,3]) # v has shape (3,) w = np.array([4,5]) # w has shape (2,) # To compute an outer product, we first reshape v to be a column # vector of shape (3, 1); we can then broadcast it against w to yield # an output of shape (3, 2), which is the outer product of v and w: # [[ 4 5] # [ 8 10] # [12 15]] print np.reshape(v, (3, 1)) * w # Add a vector to each row of a matrix x = np.array([[1,2,3], [4,5,6]]) # x has shape (2, 3) and v has shape (3,) so they broadcast to (2, 3), # giving the following matrix: # [[2 4 6] # [5 7 9]] print x + v # Add a vector to each column of a matrix # x has shape (2, 3) and w has shape (2,). # If we transpose x then it has shape (3, 2) and can be broadcast # against w to yield a result of shape (3, 2); transposing this result # yields the final result of shape (2, 3) which is the matrix x with # the vector w added to each column. Gives the following matrix: # [[ 5 6 7] # [ 9 10 11]] print (x.T + w).T # Another solution is to reshape w to be a row vector of shape (2, 1); # we can then broadcast it directly against x to produce the same # output. print x + np.reshape(w, (2, 1)) # Multiply a matrix by a constant: # x has shape (2, 3). Numpy treats scalars as arrays of shape (); # these can be broadcast together to shape (2, 3), producing the # following array: # [[ 2 4 6] # [ 8 10 12]] print x * 2