如何使用numpy在Python中展平一個矩陣?
In this article, we will show you how to flatten a matrix using the NumPy library in python.
numpy.ndarray.flatten()函數
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')
登入後複製
參數ndarray.flatten(order='C')
order − 'C', 'F', 'A', 'K' (可選)
- 當我們將排序參數設定為
'C,'時,陣列會按行主序展平。
- When the 'F' is set, the array is flattened in
column-major order.
- #只有當 'a' 在記憶體中是Fortran連續的,且順序參數設定為 'A' 時,陣列才會以列主序展開。最終順序為 'K',以與記憶體中元素出現的順序相同的順序展開數組。此參數預設為 'C'。
Return Value − Returns a flattened 1-D matrix
#Method 1 − Flattening 2x2 Numpy Matrix of np.array() type#Algorithm (Steps)以下是執行所需任務的演算法/步驟:
- 使用import關鍵字,匯入帶有別名(np)的
numpy模組。
- 使用
numpy.array()函數(傳回一個ndarray。ndarray是滿足給定要求的陣列物件),透過將2維數組(2行,2列)作為參數傳遞給它來創建一個numpy數組。
- 列印給定的二維矩陣。
- 在輸入矩陣上套用 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)
The input numpy matrix: [[3 5] [4 8]] Resultant flattened matrix: [3 5 4 8]
#Algorithm (Steps)
以下是執行所需任務的演算法/步驟:
- Use the
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.
- 印出給定的4維矩陣。
- 透過將NumPy陣列的長度與自身相乘來計算矩陣的元素數。這些值表示所需的列數。
- Use the
reshape() function(reshapes an array without affecting its data) to reshape the array and flatten the input matrix(4D) to a one-dimensional matrix.
- #都##列印輸入矩陣的結果扁平化矩陣。
範例下面的程式使用reshape()函數將給定的4維矩陣扁平化為1維矩陣,並傳回結果 -
# 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)
登入後複製
Output#執行時,上述程式將產生以下輸出 -# 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]]
登入後複製
Method 3 − Flattening 4x4 Numpy Matrix of np.matrix() type### 的中文翻譯為:###方法3-將np.matrix()類型的4x4 Numpy矩陣展平##########
###Algorithm (Steps)###
###以下是執行所需任務的演算法/步驟:###
###
######使用###numpy.matrix()###函數(從資料字串或類似陣列的物件傳回矩陣。產生的矩陣是專門的4D陣列),透過將4維數組( 4行,4列)作為參數傳遞給它來建立一個numpy矩陣。 ######
######列印輸入矩陣的結果扁平化矩陣。 ######
###
###範例######以下程式使用flatten()函數將給定的4維矩陣展平為1維矩陣,並傳回結果 -###
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)
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]]
以上是如何使用numpy在Python中展平一個矩陣?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

更新numpy版本方法:1、使用「pip install --upgrade numpy」指令;2、使用的是Python 3.x版本,使用「pip3 install --upgrade numpy」指令,將會下載並安裝,覆蓋目前的NumPy版本;3、若使用的是conda來管理Python環境,使用「conda install --update numpy」指令更新即可。

Numpy是Python中一個重要的數學庫,它提供了高效的數組操作和科學計算函數,被廣泛應用於數據分析、機器學習、深度學習等領域。在使用numpy過程中,我們經常需要查看numpy的版本號,以便確定目前環境所支援的功能。本文將介紹如何快速查看numpy版本,並提供具體的程式碼範例。方法一:使用numpy自帶的__version__屬性numpy模組自帶一個__

推薦使用最新版本的NumPy1.21.2。原因是:目前,NumPy的最新穩定版本是1.21.2。通常情況下,建議使用最新版本的NumPy,因為它包含了最新的功能和效能優化,並且修復了先前版本中的一些問題和錯誤。

一步步教你在PyCharm中安裝NumPy並充分利用其強大功能前言:NumPy是Python中用於科學計算的基礎庫之一,提供了高效能的多維數組物件以及對數組執行基本操作所需的各種函數。它是大多數資料科學和機器學習專案的重要組成部分。本文將向大家介紹如何在PyCharm中安裝NumPy,並透過具體的程式碼範例展示其強大的功能。第一步:安裝PyCharm首先,我們

如何升級numpy版本:簡單易懂的教程,需要具體程式碼範例引言:NumPy是一個重要的Python庫,用於科學計算。它提供了一個強大的多維數組物件和一系列與之相關的函數,可用於進行高效的數值運算。隨著新版本的發布,不斷有更新的特性和Bug修復可供我們使用。本文將介紹如何升級已安裝的NumPy函式庫,以取得最新特性並解決已知問題。步驟1:檢查目前NumPy版本在開始

numpy可以透過使用pip、conda、原始碼和Anaconda來安裝。詳細介紹:1、pip,在命令列中輸入pip install numpy即可;2、conda,在命令列中輸入conda install numpy即可;3、源碼,解碼源碼包或進入源碼目錄,在命令行中輸入python setup.py build python setup.py install即可。

隨著資料科學、機器學習和深度學習等領域的快速發展,Python成為了資料分析和建模的主流語言。在Python中,NumPy(NumericalPython的簡稱)是一個很重要的函式庫,因為它提供了一組高效的多維數組對象,也是許多其他函式庫如pandas、SciPy和scikit-learn的基礎。在使用NumPy過程中,很有可能會遇到不同版本之間的相容性問題,那麼

Numpy安裝攻略:一文解決安裝難題,需要具體程式碼範例引言:Numpy是Python中一款強大的科學計算庫,它提供了高效的多維數組物件和對數組資料進行操作的工具。但是,對於初學者來說,安裝Numpy可能會帶來一些困擾。本文將為大家提供一份Numpy安裝攻略,幫助大家快速解決安裝難題。一、安裝Python環境:在安裝Numpy之前,首先需要確保已經安裝了Py
