如何使用 Python 和動態陣列執行 NumPy 廣播?


“廣播”指的是 NumPy 在算術運算期間如何處理不同維度的陣列。較小的陣列會在遵守一定限制的情況下“廣播”到較大的陣列中,以確保它們的形狀一致。廣播允許您將陣列運算向量化,使您能夠在 C 而不是 Python 中迴圈。

這無需進行不必要的的資料複製,從而實現高效的演算法實現。在某些情況下,廣播是一個不好的主意,因為它會導致記憶體浪費,從而減慢計算速度。

在本文中,我們將向您展示如何使用 Python 對 NumPy 陣列執行廣播。

在給定陣列上執行廣播的步驟:

  • 步驟 1. 建立兩個維度相容的陣列

  • 步驟 2. 列印給定陣列

  • 步驟 3. 對兩個陣列執行算術運算

  • 步驟 4. 列印結果陣列

新增兩個不同維度的陣列

使用 arange() 函式建立一個包含從 0 到 n-1 的數字的 NumPy 陣列(arange() 函式返回在給定區間內均勻分佈的值。在半開區間 [start, stop] 內生成值),並向其中新增一個常數。

示例

import numpy as np # Getting list of numbers from 0 to 7 givenArray = np.arange(8) # Adding a number to the numpy array result_array = givenArray + 9 print("The input array",givenArray) print("Result array after adding 9 to the input array",result_array)

輸出

The input array [0 1 2 3 4 5 6 7]
Result array after adding 9 to the input array [ 9 10 11 12 13 14 15 16] 

給定陣列具有一個長度為 8 的維度(軸),而 9 是一個沒有維度的簡單整數。由於它們的維度不同,NumPy 會嘗試沿某個軸廣播(只是拉伸)較小的陣列,使其適合數學運算。

將兩個維度相容的陣列求和

使用 arange() 函式建立兩個從 0 到 n-1 的 NumPy 陣列,並使用 reshape() 函式對其進行重塑(重塑陣列而不影響其資料)。這兩個陣列具有相容的維度 (3,4) 和 (3,1),並新增兩個陣列的對應元素。

示例

import numpy as np # Getting the list of numbers from 0 to 11 and reshaping it to 3 rows and 4 columns givenArray_1 = np.arange(12).reshape(3, 4) # Printing the shape(rowsize, columnsize) of array print("The shape of Array_1 = ", givenArray_1.shape) # Getting list of numbers from 0 to 2 and reshaping it to 3 rows and 1 columns givenArray_2 = np.arange(3).reshape(3, 1) print("The shape of Array_2 = ", givenArray_2.shape) # Summing both the arrays print("Input array 1 \n",givenArray_1) print("Input array 2 \n",givenArray_2) print("Summing both the arrays:") print(givenArray_1 + givenArray_2)

輸出

The shape of Array_1 =  (3, 4)
The shape of Array_2 =  (3, 1)
Input array 1 
 [[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11] ]
Input array 2 
 [[0]
  [1]
  [2]]
Summing both the arrays:
[[ 0  1  2  3]
 [ 5  6  7  8]
 [10 11 12 13]]

givenArray_2 沿第二維展開以匹配 givenArray_1 的維度。由於兩個陣列的維度相容,這可以實現。

將兩個維度不相容的陣列求和

建立兩個維度不相容的 NumPy 陣列 (6, 4) 和 (6, 1)。當我們嘗試新增兩個陣列的對應元素時,它會引發如下所示的錯誤。

示例

import numpy as np # Getting a list of numbers from 0 to 11 and reshaping it to 3 rows and 4 columns givenArray_1 = np.arange(20).reshape(6, 4) # Printing the shape(rowsize, columnsize) of array print("The shape of Array_1 = ", givenArray_1.shape) # Getting list of numbers from 0 to 5 and reshaping it to 3 rows and 1 columns givenArray_2 = np.arange(6).reshape(6, 1) print("The shape of Array_2 = ", givenArray_2.shape) # Summing both the arrays print("Summing both the arrays:") print(givenArray_1 + givenArray_2)

輸出

Traceback (most recent call last):
  File "main.py", line 3, in 
    givenArray_1 = np.arange(20).reshape(6, 4)
ValueError: cannot reshape array of size 20 into shape (6,4)

行數為 6,列數為 4。

它無法插入大小為 20 的矩陣中(它需要大小為 6*4 = 24 的矩陣)。

將 NumPy 多維陣列和線性陣列求和

使用 arange() 函式建立一個多維陣列,並使用 reshape() 函式將其重塑為任意數量的行和列。使用 arange() 函式建立另一個線性陣列,並將這兩個陣列相加。

示例 1

import numpy as np # Getting list of numbers from 0 to 14 and reshaping it to 5 rows and 3 columns givenArray_1 = np.arange(15).reshape(5, 3) # Printing the shape(rowsize, columnsize) of array print("The shape of Array_1 = ", givenArray_1.shape) # Getting list of numbers from 0 to 2 givenArray_2 = np.arange(3) print("The shape of Array_2 = ", givenArray_2.shape) # Summing both the arrays print("Array 1 \n",givenArray_1) print("Array 2 \n",givenArray_2) print("Summing both the arrays: \n",givenArray_1 + givenArray_2)

輸出

The shape of Array_1 =  (5, 3)
The shape of Array_2 =  (3,)
Array 1 
 [[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]
  [ 9 10 11]
  [12 13 14]]
Array 2 
 [0 1 2]
Summing both the arrays: 
 [[ 0  2  4]
  [ 3  5  7]
  [ 6  8 10]
  [ 9 11 13]
  [12 14 16]]
 

給定的線性陣列被展開以匹配給定陣列 1(多維陣列)的維度。由於兩個陣列的維度相容,這可以實現。

示例 2

import numpy as np givenArray_1 = np.arange(240).reshape(6, 5, 4, 2) print("The shape of Array_1: ", givenArray_1.shape) givenArray_2 = np.arange(20).reshape(5, 4, 1) print("The shape of Array_2: ", givenArray_2.shape) # Summing both the arrays and printing the shape of it print("Summing both the arrays and printing the shape of it:") print((givenArray_1 + givenArray_2).shape)

輸出

The shape of Array_1:  (6, 5, 4, 2)
The shape of Array_2:  (5, 4, 1)
Summing both the arrays and printing the shape of it:
(6, 5, 4, 2)

務必理解,多個數組可以沿多個維度進行傳播。Array1 的維度為 (6, 5, 4, 2),而 array2 的維度為 (5, 4, 1)。維度陣列是透過沿第三維拉伸 array1 和沿第一和第二維拉伸 array2 形成的 (6, 5, 4, 2)。

結論

NumPy 廣播比遍歷陣列要快。從第一個示例開始。使用者可以使用迴圈遍歷陣列,向陣列中的每個元素新增相同的數字,而不是使用廣播方法。這有兩個原因很慢:迴圈需要與 Python 迴圈互動,這會減慢 C 實現的速度。其次,NumPy 使用步幅而不是迴圈。將步幅設定為 0 允許您無限迴圈遍歷元件,而不會產生記憶體開銷。

更新於:2022年8月17日

248 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.