Python 中的矩阵和线性代数计算
在本文中,我们将学习 Python 中的矩阵和线性代数计算,例如矩阵乘法、查找行列式、求解线性方程等。
可以使用 NumPy 库中的 矩阵对象 来实现这一点。在计算方面,矩阵与数组对象相对可比
线性代数是一个很大的话题,不在本文讨论范围内。
但是,如果您需要操作矩阵和向量,NumPy 是一个很好的入门库。
使用的方法
使用 Numpy 查找矩阵的转置
使用 Numpy 查找矩阵的逆
将矩阵与向量相乘
使用 numpy.linalg 子包获取矩阵的行列式
使用 numpy.linalg 查找特征值
使用numpy.linalg
方法 1:使用 Numpy 查找矩阵的转置
numpy.matrix.T 属性 - 返回给定矩阵的转置。
示例
以下程序使用 numpy.matrix.T 属性返回矩阵的转置 -
# 导入 NumPy 模块 import numpy as np # 输入矩阵 inputMatrix = np.matrix([[6, 1, 5], [2, 0, 8], [1, 4, 3]]) # 打印输入矩阵 print("输入矩阵:\n", inputMatrix) # 打印输入矩阵的转置 # 通过应用numpy 模块的 NumPy 矩阵的 .T 属性 print("输入矩阵的转置\n", inputMatrix.T)
输出
执行时,上述程序将生成以下输出 -
输入矩阵: [[6 1 5] [2 0 8] [1 4 3]] 输入矩阵的转置 [[6 2 1] [1 0 4] [5 8 3]]
方法 2:使用 Numpy 查找矩阵的逆
numpy.matrix.I 属性 − 返回给定矩阵的逆。
示例
以下程序使用 numpy.matrix.I 属性返回矩阵的逆 −
# 导入 NumPy 模块 import numpy as np # 输入矩阵 inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]]) # 打印输入矩阵 print("输入矩阵:\n", inputMatrix) # 打印输入矩阵的逆 # 通过应用 NumPy 矩阵的 .I 属性numpy 模块 print("输入矩阵的逆:\n", inputMatrix.I)
输出
执行时,上述程序将生成以下输出 -
输入矩阵: [[6 1 5] [2 0 8] [1 4 3]] 输入矩阵的逆: [[ 0.21333333 -0.11333333 -0.05333333] [-0.01333333 -0.08666667 0.25333333] [-0.05333333 0.15333333 0.01333333]]
方法 3:将矩阵与向量相乘
示例
以下程序使用 * 运算符返回输入矩阵与向量的乘积 -
# 导入 numpy 模块 import numpy as np # 输入矩阵 inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]]) # 打印输入矩阵 print("输入矩阵:\n", inputMatrix) # 使用 numpy.matrix() 函数创建向量 inputVector = np.matrix([[1],[3],[5]]) # 打印输入矩阵与向量的乘积 print("输入矩阵与向量的乘积:\n", inputMatrix*inputVector)
输出
执行时,上述程序将生成以下输出 -
输入矩阵: [[6 1 5] [2 0 8] [1 4 3]] 输入矩阵与向量的乘积: [[34] [42] [28]]
方法 4:使用 numpy.linalg 子包获取矩阵的行列式
numpy.linalg.det() 函数 - 计算方阵的行列式。
示例
以下程序使用 numpy.linalg.det() 函数返回矩阵的行列式 -
# 导入 numpy 模块 import numpy as np # 输入矩阵 inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]]) # 打印输入矩阵 print("Input Matrix:\n", inputMatrix) # 获取输入矩阵的行列式 outputDet = np.linalg.det(inputMatrix) # 打印输入矩阵的行列式 print("输入矩阵的行列式:\n", outputDet)
输出
执行时,上述程序将生成以下输出 -
输入矩阵: [[6 1 5] [2 0 8] [1 4 3]] 打印输入矩阵的行列式: -149.99999999999997
方法 5:使用 numpy.linalg 查找特征值
numpy.linalg.eigvals() 函数 − 计算指定方阵/矩阵的特征值和右特征向量。
示例
以下程序使用 numpy.linalg.eigvals() 函数返回输入矩阵的特征值 −
# 导入 NumPy 模块 import numpy as np # 输入矩阵 inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]]) # 打印输入矩阵 print("Input Matrix:\n", inputMatrix) # 获取输入矩阵的特征值 eigenValues = np.linalg.eigvals(inputMatrix) # 打印输入矩阵的特征值 print("输入矩阵的特征值:\n", eigenValues)
输出
执行时,上述程序将生成以下输出 -
输入矩阵: [[6 1 5] [2 0 8] [1 4 3]] 输入矩阵的特征值: [ 9.55480959 3.69447805 -4.24928765]
方法 6:使用 numpy.linalg 求解方程式
我们可以求解诸如求 A*X = B 中 X 的值之类的问题,
其中 A 是矩阵,B 是向量。
示例
下面是使用solve()函数返回x值的程序−
# 导入 NumPy 模块 import numpy as np # 输入矩阵 inputMatrix = np.matrix([[6, 1, 5],[2, 0, 8],[1, 4, 3]]) # 打印输入矩阵 print("Input Matrix:\n", inputMatrix) # 使用 np.matrix() 函数创建向量 inputVector = np.matrix([[1],[3],[5]]) # 获取方程式中 x 的值 inputMatrix * x = inputVector x_value = np.linalg.solve(inputMatrix, inputVector) # 打印 x 值 print("x 值:\n", x_value) # 将输入矩阵与 x 值相乘 print("将输入矩阵与 x 值相乘:\n", inputMatrix * x_value)
输出
执行时,上述程序将生成以下输出 -
输入矩阵: [[6 1 5] [2 0 8] [1 4 3]] x 值: [[-0.39333333] [ 0.99333333] [ 0.47333333]] 将输入矩阵与 x 值相乘: [[1.] [3.] [5.]]
结论
在本文中,我们学习了如何使用 Python 中的 NumPy 模块执行矩阵和线性代数运算。我们学习了如何计算矩阵的转置、逆和行列式。我们还学习了如何执行线性代数中的一些计算,例如求解方程和确定特征值。