NumPy reshape 中 -1 的含義是什麼?


NumPy 是一個用於數值計算的 Python 庫,它提供高效的陣列操作,而 numpy.reshape() 函式用於更改陣列的形狀,其中 -1 表示推斷的維度。

在處理陣列時,我們經常會遇到需要修改陣列形狀的情況,但這需要先複製資料,然後將其排列成所需的形狀,這很費時。幸運的是,Python 有一個名為 reshape() 的函式可以幫助我們做到這一點。

示例 1:在 NumPy 中查詢未知維度

當我們不知道任何陣列維度時,我們只允許有一個“未知”維度。

這意味著當使用 reshape 方法時,我們不需要為這些維度中的一個提供數字。我們使用 NumPy 透過傳遞 -1 值來計算未知維度。

引數值 (2, 2, -1) 表示輸出陣列的所需形狀為 2 行、2 列,其餘維度將根據輸入陣列的大小自動推斷。

以下程式展示了在 numpy 陣列 reshape() 函式中使用 -1 的方法

# importing numpy module 
import numpy as np
 
# creating a numpy array 
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
 
# reshaping the input array into a 3-dimensional array with 2 rows, 2 columns, and an automatically inferred depth
outputArray = inputArray.reshape(2, 2, -1)
# printing the resultant array
print(outputArray)

輸出

[[[15 16]
  [17 18]]

 [[19 20]
  [21 22]]]

示例 2:將 -1 用作未知列數的值

以下程式展示了將 -1 用作未知列數的值的方法

# importing numpy module 
import numpy as np
 
# creating a numpy array 
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
 
# reshaping the input array into a 3-dimensional array with 2 rows, an automatically inferred middle dimension, and 4 columns
outputArray = inputArray.reshape(2, -1, 4)
# printing the resultant array
print(outputArray)

輸出

[[[15 16 17 18]]

 [[19 20 21 22]]]

示例 3:將 -1 用作未知行數的值

在下面的示例中,形狀指定為 (-1, 4, 2),表示第一個維度將根據輸入陣列的大小自動推斷。輸出是一個具有所需形狀的三維陣列。然後使用 reshape() 函式將 inputArray 重塑成一個三維陣列。它有 2 行,一個自動推斷的中間維度(使用 -1),和 4 列。-1 引數允許 numpy 根據輸入陣列的大小和給定的維度自動計算推斷維度的尺寸。

# importing numpy module 
import numpy as np
 
# creating a numpy array 
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
 
# reshaping the input array into a 3-dimensional array with an automatically inferred first dimension, 4 columns, and 2 depth elements
outputArray = inputArray.reshape(-1, 4, 2)
# printing the resultant array
print(outputArray)

輸出

執行上述程式後,將生成以下輸出

[[[15 16]
  [17 18]
  [19 20]
  [21 22]]]

示例 4:對多個未知維度使用 -1

在這個例子中,我們對多個未知維度使用了 -1。因此它引發了錯誤。

# importing numpy module 
import numpy as np
 
# creating a numpy array 
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
 
# reshaping the input array into a 3-dimensional array with 2 as the first dimension and an automatically inferred second and third dimension
outputArray = inputArray.reshape(2, -1, -1)
# printing the resultant array
print(outputArray)

輸出

Traceback (most recent call last):
  File "/home/cg/root/85901/main.py", line 8, in <module>
    outputArray = inputArray.reshape(2, -1, -1)
ValueError: can only specify one unknown dimension

使用 reshape() 函式展平 NumPy 陣列

使用 reshape() 函式將 3D 陣列展平為 1D 陣列

以下程式透過使用 reshape() 函式將輸入的 3D 陣列展平為 1D 陣列,返回展平的一維陣列。

# importing numpy module 
import numpy as np

# creating a 3D array
inputArray = np.array([[1, 3, 5],
                      [7, 9, 11], 
                      [13, 15, 17]])

# flattening 3D input array to 1-dimension using reshape() function
outputArray = inputArray.reshape(-1)
# printing the resultanat flattened array
print("Output flattened array:\n", outputArray)

輸出

Output flattened array:
 [ 1  3  5  7  9 11 13 15 17]

使用 reshape() 函式將 2D 陣列展平為 1D 陣列

以下程式透過使用 reshape() 函式將輸入的 2D 陣列展平為 1D 陣列,返回展平的一維陣列。

# importing numpy module 
import numpy as np
 
# creating a 2D array
inputArray = np.array([[15, 16, 17], [18, 19, 20]])
 
# flattening 2D input array to 1-dimension using reshape() function
outputArray = inputArray.reshape(-1)
# printing the resultant flattened array
print("Output flattened array:\n", outputArray)

輸出

Output flattened array:
 [15 16 17 18 19 20]

結論

總而言之,NumPy 的 reshape() 函式中的 -1 值用於表示未知維度。它允許 NumPy 根據其他維度和輸入陣列的大小自動計算該維度的尺寸。此功能在重塑具有已知維度的陣列或將多維陣列展平為一維陣列時特別有用。透過利用 -1,我們可以簡化重塑過程,避免手動計算。

更新於:2023年8月17日

356 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告