Python 中的矩陣和線性代數計算
在本文中,我們將學習 Python 中的矩陣和線性代數計算,例如矩陣乘法、求行列式、解線性方程組等。
NumPy 庫中的矩陣物件可用於此目的。在計算方面,矩陣與陣列物件相對可比。
線性代數是一個非常廣泛的主題,本文無法涵蓋。
但是,如果您需要操作矩陣和向量,NumPy 是一個極好的入門庫。
使用的方法
使用 Numpy 求矩陣的轉置
使用 Numpy 求矩陣的逆
矩陣與向量的乘法
使用 numpy.linalg 子包獲取矩陣的行列式
使用 numpy.linalg 求特徵值
使用 numpy.linalg 解方程
方法 1:使用 Numpy 求矩陣的轉置
numpy.matrix.T 屬性 - 返回給定矩陣的轉置。
示例
以下程式使用numpy.matrix.T 屬性返回矩陣的轉置 -
# importing NumPy module
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5], [2, 0, 8], [1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)
# printing the transpose of an input matrix
# by applying the .T attribute of the NumPy matrix of the numpy Module
print("Transpose of an input matrix\n", inputMatrix.T)
輸出
執行上述程式將生成以下輸出 -
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Transpose of an input matrix [[6 2 1] [1 0 4] [5 8 3]]
方法 2:使用 Numpy 求矩陣的逆
numpy.matrix.I 屬性 - 返回給定矩陣的逆。
示例
以下程式使用numpy.matrix.I 屬性返回矩陣的逆 -
# importing NumPy module
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)
# printing the inverse of an input matrix
# by applying the .I attribute of the NumPy matrix of the numpy Module
print("Inverse of an input matrix:\n", inputMatrix.I)
輸出
執行上述程式將生成以下輸出 -
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Inverse of an input matrix: [[ 0.21333333 -0.11333333 -0.05333333] [-0.01333333 -0.08666667 0.25333333] [-0.05333333 0.15333333 0.01333333]]
方法 3:矩陣與向量的乘法
示例
以下程式使用 * 運算子返回輸入矩陣和向量的乘積 -
# importing numpy module
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)
# creating a vector using numpy.matrix() function
inputVector = np.matrix([[1],[3],[5]])
# printing the multiplication of the input matrix and vector
print("Multiplication of input matrix and vector:\n", inputMatrix*inputVector)
輸出
執行上述程式將生成以下輸出 -
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Multiplication of input matrix and vector: [[34] [42] [28]]
方法 4:使用 numpy.linalg 子包獲取矩陣的行列式
numpy.linalg.det() 函式 - 計算方陣的行列式。
示例
以下程式使用numpy.linalg.det() 函式返回矩陣的行列式 -
# importing numpy module
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)
# getting the determinant of an input matrix
outputDet = np.linalg.det(inputMatrix)
# printing the determinant of an input matrix
print("Determinant of an input matrix:\n", outputDet)
輸出
執行上述程式將生成以下輸出 -
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Determinant of an input matrix: -149.99999999999997
方法 5:使用 numpy.linalg 求特徵值
numpy.linalg.eigvals() 函式 - 計算指定方陣/矩陣的特徵值和右特徵向量。
示例
以下程式使用 numpy.linalg.eigvals() 函式返回輸入矩陣的特徵值 -
# importing NumPy module
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)
# getting Eigenvalues of an input matrix
eigenValues = np.linalg.eigvals(inputMatrix)
# printing Eigenvalues of an input matrix
print("Eigenvalues of an input matrix:\n", eigenValues)
輸出
執行上述程式將生成以下輸出 -
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] Eigenvalues of an input matrix: [ 9.55480959 3.69447805 -4.24928765]
方法 6:使用 numpy.linalg 解方程
我們可以解決諸如求解 A*X = B 中 X 的值的問題,
其中 A 是矩陣,B 是向量。
示例
以下是使用 solve() 函式返回 x 值的程式 -
# importing NumPy module
import numpy as np
# input matrix
inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]])
# printing the input matrix
print("Input Matrix:\n", inputMatrix)
# creating a vector using np.matrix() function
inputVector = np.matrix([[1],[3],[5]])
# getting the value of x in an equation inputMatrix * x = inputVector
x_value = np.linalg.solve(inputMatrix, inputVector)
# printing x value
print("x value:\n", x_value)
# multiplying input matrix with x values
print("Multiplication of input matrix with x values:\n", inputMatrix * x_value)
輸出
執行上述程式將生成以下輸出 -
Input Matrix: [[6 1 5] [2 0 8] [1 4 3]] x value: [[-0.39333333] [ 0.99333333] [ 0.47333333]] Multiplication of input matrix with x values: [[1.] [3.] [5.]]
結論
在本文中,我們學習瞭如何在 Python 中使用 NumPy 模組執行矩陣和線性代數運算。我們學習瞭如何計算矩陣的轉置、逆和行列式。我們還學習瞭如何執行一些線性代數計算,例如解方程和求特徵值。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP