In this article, we will show you how to flatten a matrix using the NumPy library in python.
The numpy module includes a function called numpy.ndarray.flatten() that returns a one-dimensional copy of the array rather than a two-dimensional or multi-dimensional array.
##簡單來說,我們可以說它將矩陣壓平為1維。文法
ndarray.flatten(order='C')
order − 'C', 'F', 'A', 'K' (可選)
'C,'時,陣列會按行主序展平。
column-major order.
Return Value − Returns a flattened 1-D matrix
#Method 1 − Flattening 2x2 Numpy Matrix of np.array() type#Algorithm (Steps)
numpy模組。
numpy.array()函數(傳回一個ndarray。ndarray是滿足給定要求的陣列物件),透過將2維數組(2行,2列)作為參數傳遞給它來創建一個numpy數組。
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)Output#執行時,上述程式將產生以下輸出 -
The input numpy matrix: [[3 5] [4 8]] Resultant flattened matrix: [3 5 4 8]Method 2 − Flattening using reshape() function
#Algorithm (Steps)
numpy.array() function(returns a ndarray. The ndarray is an array object that satisfies the given requirements), for creating a numpy array by passing the 4-Dimensional array (4rows, 4columns) as an argument to it.
reshape() function(reshapes an array without affecting its data) to reshape the array and flatten the input matrix(4D) to a one-dimensional matrix.
# 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]]
# 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)###Output#### ###執行時,上述程式將產生以下輸出 -###
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]]###Conclusion### ###在這篇文章中,我們學習如何使用三個不同的範例在Python中展平矩陣。我們學習如何使用兩種不同的方法在Numpy中取得矩陣:numpy.array()和NumPy.matrix()。我們也學習如何使用reshape函數展平矩陣。 ###
以上是如何使用numpy在Python中展平一個矩陣?的詳細內容。更多資訊請關注PHP中文網其他相關文章!