
在本文中,我们将学习如何使用Python进行矩阵和线性代数计算,例如矩阵乘法、求行列式、解线性方程等。
从NumPy库中可以使用一个矩阵对象来实现。在进行计算时,矩阵与数组对象相对可比。
线性代数是一个庞大的主题,超出了本文的范围。
然而,如果你需要操作矩阵和向量,NumPy是一个很好的起点。
立即学习“Python免费学习笔记(深入)”;
使用Numpy找到矩阵的转置
使用Numpy找到矩阵的逆
矩阵与向量相乘
使用numpy.linalg子包获取矩阵的行列式
使用numpy.linalg找到特征值
使用numpy.linalg解决方程
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]]
numpy.matrix.I属性 - 返回给定矩阵的逆矩阵。
以下程序使用 numpy.matrix.I 属性返回矩阵的逆矩阵 −
启科网络商城系统由启科网络技术开发团队完全自主开发,使用国内最流行高效的PHP程序语言,并用小巧的MySql作为数据库服务器,并且使用Smarty引擎来分离网站程序与前端设计代码,让建立的网站可以自由制作个性化的页面。 系统使用标签作为数据调用格式,网站前台开发人员只要简单学习系统标签功能和使用方法,将标签设置在制作的HTML模板中进行对网站数据、内容、信息等的调用,即可建设出美观、个性的网站。
0
# 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]]
以下程序使用*运算符返回输入矩阵和向量的乘积 -
# 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]]
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
numpy.linalg.eigvals() 函数 − 计算指定方阵/矩阵的特征值和右特征向量。
The following program returns the Eigenvalues of an input matrix using the numpy.linalg.eigvals() function −
# 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]
我们可以解决类似于找到 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模块执行矩阵和线性代数操作。我们学会了如何计算矩阵的转置、逆和行列式。我们还学习了如何在线性代数中进行一些计算,例如解方程和确定特征值。
以上就是在Python中进行矩阵和线性代数计算的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号