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')
參數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)Output#執行時,上述程式將產生以下輸出 -
The input numpy matrix: [[3 5] [4 8]] Resultant flattened matrix: [3 5 4 8]Method 2 − Flattening using reshape() function
#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#執行時,上述程式將產生以下輸出 -
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維矩陣,並傳回結果 -###
# 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中文網其他相關文章!

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

如何解決jieba分詞在景區評論分析中的問題?當我們在進行景區評論分析時,往往會使用jieba分詞工具來處理文�...

如何使用正則表達式匹配到第一個閉合標籤就停止?在處理HTML或其他標記語言時,常常需要使用正則表達式來�...


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器