numpy的廣播(Broadcasting)

一、注意

首先我們一定要注意,執行 broadcast 的前提在於,兩個 ndarray 執行的是 element-wise(按位加,按位減) 的運算,而不是矩陣乘法的運算,矩陣乘法運算時需要維度之間嚴格匹配。

例子如下:(當矩陣乘法的時候

import numpy as npA = np.zeros((2,4))B = np.zeros((3,4))np.dot(A,B)

報的錯誤是aligned:如下、

而提示的錯誤如果是 broadcast的問題的話,一定是兩個 ndarray 執行的是 element-wise(按位加,按位減) 的運算維度不匹配,例子如下:

import numpy as npA = np.zeros((2,4))B = np.zeros((3,4))C = A*B

報的錯誤如下:

二、broadcast的簡單例子

舉一個簡單的例子,實現對一個1-d array的每一個元素乘以2:

>>> a = np.array([1., 2., 3.])>>> b = np.array([2., 2., 2.])>>> a*barray([2., 4., 6.])

broadcast的做法是:

>>> a = np.array([1., 2., 3.])>>> b = 2.>>> a*barray([2., 4., 6.])

三、Broadcast(廣播)的規則

  1. All input arrays with ndim smaller than the input array of largest ndim, have 1』s prepended to their shapes.
  2. The size in each dimension of the output shape is the maximum of all the input sizes in that dimension.
  3. An input can be used in the calculation if its size in a particular dimension either matches the output size in that dimension, or has value exactly 1.
  4. If an input has a dimension size of 1 in its shape, the first data entry in that dimension will be used for all calculations along that dimension. In other words, the stepping machinery of the ufunc will simply not step along that dimension (the stride will be 0 for that dimension).

翻譯如下:

  1. 讓所有輸入數組都向其中shape最長的數組看齊,shape中不足的部分都通過在前面加1補齊
  2. 輸出數組的shape是輸入數組shape的各個軸上的最大值
  3. 如果輸入數組的某個軸和輸出數組的對應軸的長度相同或者其長度為1時,這個數組能夠用來計算,否則出錯
  4. 當輸入數組的某個軸的長度為1時,沿著此軸運算時都用此軸上的第一組值

來看更為一般的broadcasting rules

當操作兩個array時,numpy會逐個比較它們的shape(構成的元組tuple),只有在下述情況下,兩arrays才算兼容:

  1. 相等
  2. 其中一個為1,(進而可進行拷貝拓展已至,shape匹配)

下面通過實際例子來解釋說明上述的四條規則:(下面例子均來自於numpy 中的 broadcasting(廣播)機制)

numpy 中的 broadcasting(廣播)機制?

blog.csdn.net

舉例說明:

Image (3d array): 256 x 256 x 3Scale (1d array): 3Result (3d array): 256 x 256 x 3A (4d array): 8 x 1 x 6 x 1B (3d array): 7 x 1 x 5Result (4d array): 8 x 7 x 6 x 5A (2d array): 5 x 4B (1d array): 1Result (2d array): 5 x 4A (2d array): 15 x 3 x 5B (1d array): 15 x 1 x 5Result (2d array): 15 x 3 x 5

再來看一些不能進行broadcast的例子:

A (1d array): 3B (1d array): 4 # 最後一維(trailing dimension)不匹配A (2d array): 2 x 1B (3d array): 8 x 4 x 3倒數第二維不匹配

我們再來看一些具體的應用:

>>> x = np.arange(4)>> xx = x.reshape(4, 1)>> y = np.ones(5)>> z = np.ones((3, 4))>>> x.shape(4,)>>> y.shape(5,)>>> x+yValueError: operands could not be broadcast together with shapes (4,) (5,) >>> xx.shape(4, 1)>>> y.shape(5,)>>> (xx+y).shape(4, 5)>>> xx + yarray([[ 1., 1., 1., 1., 1.], [ 2., 2., 2., 2., 2.], [ 3., 3., 3., 3., 3.], [ 4., 4., 4., 4., 4.]])

當執行xx+y時,numpy是如何進行copy擴展的呢?

xx (2d array): 4 x 1y (1d array): 5Result (2d array): 4 x 5

也即對xx重複5列,對y重複4行

# 對xx重複5列# 等價於np.dot(xx, np.ones((1, 4)))array([[ 0., 0., 0., 0.], [ 1., 1., 1., 1.], [ 2., 2., 2., 2.], [ 3., 3., 3., 3.]])# 對y重複4行,array([[ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]])

最後來個官網文檔的圖例收尾:

參考:

numpy 中的 broadcasting(廣播)機制?

blog.csdn.net


推薦閱讀:

Python-NumPy模塊的通用函數(3)
Matlab user 轉 Python 筆記(1):初遇Numpy
《利用python進行數據分析》——Chap4:numpy
ImagePy開發文檔 —— 常用匯總

TAG:Python | numpy | Python入門 |