如何在 Python 中使用 numpy 展平矩陣?


在本文中,我們將向您展示如何在 python 中使用 NumPy 庫展平矩陣。

numpy.ndarray.flatten() 函式

numpy 模組包含一個名為 numpy.ndarray.flatten() 的函式,它返回陣列的一維副本,而不是二維或多維陣列。

簡單來說,我們可以說它將矩陣展平為一維。

語法

ndarray.flatten(order='C')

引數

order − 'C', 'F', 'A', 'K' (可選)

  • 當我們將 order 引數設定為 'C' 時,陣列將以 行主序 展平。

  • 當設定 'F' 時,陣列將以 列主序 展平。

  • 只有當 'a' 在記憶體中是 Fortran 連續的並且 order 引數設定為 'A' 時,陣列才會以列主序展平。最終順序是 'K',它以元素在記憶體中出現的相同順序展平陣列。預設情況下,此引數設定為 'C'。

返回值 − 返回一個展平的一維矩陣

方法 1 - 展平 np.array() 型別的 2x2 NumPy 矩陣

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟 -

  • 使用 import 關鍵字,匯入帶有別名(np)的 numpy 模組。

  • 使用 numpy.array() 函式(返回一個 ndarray。ndarray 是一個滿足給定要求的陣列物件),透過將二維陣列(2 行,2 列)作為引數傳遞給它來建立 NumPy 陣列。

  • 列印給定的輸入二維矩陣。

  • 對輸入矩陣應用 numpy 模組的 flatten() 函式(將矩陣展平為一維),以將輸入二維矩陣展平為一維矩陣。

  • 列印輸入矩陣的最終展平矩陣。

示例

以下程式使用 flatten() 函式將給定的輸入二維矩陣展平為一維矩陣並返回它 -

# importing numpy module with an alias name import numpy as np # creating a 2-Dimensional(2x2) numpy matrix inputMatrix = np.array([[3, 5], [4, 8]]) # printing the input 2D matrix print("The input numpy matrix:") print(inputMatrix) # flattening the 2D matrix to one-dimensional matrix flattenMatrix = inputMatrix.flatten() # printing the resultant flattened matrix print("Resultant flattened matrix:") print(flattenMatrix)

輸出

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

The input numpy matrix:
[[3 5]
[4 8]]
Resultant flattened matrix:
[3 5 4 8]

方法 2 - 使用 reshape() 函式展平

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟 -

  • 使用 numpy.array() 函式(返回一個 ndarray。ndarray 是一個滿足給定要求的陣列物件),透過將四維陣列(4 行,4 列)作為引數傳遞給它來建立 NumPy 陣列。

  • 列印給定的輸入四維矩陣。

  • 透過將 NumPy 陣列的長度乘以自身來計算矩陣的元素數量。此處這些值給出了所需的列數。

  • 使用 reshape() 函式(在不影響其資料的情況下重新整形陣列)來重新整形陣列並將輸入矩陣(4D)展平為一維矩陣。

  • 列印輸入矩陣的最終展平矩陣。

示例

以下程式使用 reshape() 函式將給定的輸入四維矩陣展平為一維矩陣並返回它 -

# importing numpy module with an alias name import numpy as np # creating a 4-Dimensional(4x4) numpy matrix inputMatrix = np.array([[1, 2, 3, 97], [4, 5, 6, 98], [7, 8, 9, 99], [10, 11, 12, 100]]) # Getting the total Number of elements of the matrix matrixSize = len(inputMatrix) * len(inputMatrix) # printing the input 4D matrix print("The input numpy matrix:") print(inputMatrix) # reshaping the array and flattening the 4D matrix to a one-dimensional matrix # here (1,matrixSize(16)) says 1 row and 16 columns(Number of elements) flattenMatrix= np.reshape(inputMatrix, (1, matrixSize)) # printing the resultant flattened matrix print("Resultant flattened matrix:") print(flattenMatrix)

輸出

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

The input numpy matrix:
[[  1   2   3  97]
 [  4   5   6  98]
 [  7   8   9  99]
 [ 10  11  12 100]]
Resultant flattened matrix:
[[  1   2   3  97   4   5   6  98   7   8   9  99  10  11  12 100]]

方法 3 - 展平 np.matrix() 型別的 4x4 NumPy 矩陣

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟 -

  • 使用 numpy.matrix() 函式(從資料字串或類陣列物件返回矩陣。生成的矩陣是一個專門的 4D 陣列),透過將四維陣列(4 行,4 列)作為引數傳遞給它來建立 NumPy 矩陣。

  • 列印輸入矩陣的最終展平矩陣。

示例

以下程式使用 flatten() 函式將給定的輸入四維矩陣展平為一維矩陣並返回它 -

# importing NumPy module with an alias name import numpy as np # creating a NumPy matrix (4x4 matrix) using matrix() method inputMatrix = np.matrix('[11, 1, 8, 2; 11, 3, 9 ,1; 1, 2, 3, 4; 9, 8, 7, 6]') # printing the input 4D matrix print("The input numpy matrix:") print(inputMatrix) # flattening the 4D matrix to one-dimensional matrix flattenMatrix = inputMatrix.flatten() # printing the resultant flattened matrix print("Resultant flattened matrix:") print(flattenMatrix)

輸出

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

The input numpy matrix:
[[11  1  8  2]
 [11  3  9  1]
 [ 1  2  3  4]
 [ 9  8  7  6]]
Resultant flattened matrix:
[[11  1  8  2 11  3  9  1  1  2  3  4  9  8  7  6]]

結論

在本文中,我們學習瞭如何使用三個不同的示例在 python 中展平矩陣。我們學習瞭如何使用兩種不同的方法在 NumPy 中獲取矩陣:numpy.array() 和 NumPy.matrix()。我們還學習瞭如何使用 reshape 函式展平矩陣。

更新於: 2022 年 10 月 31 日

4K+ 次觀看

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.