Python中的矩阵和线性代数计算

您所在的位置:网站首页 python计算矩阵特征值 Python中的矩阵和线性代数计算

Python中的矩阵和线性代数计算

#Python中的矩阵和线性代数计算| 来源: 网络整理| 查看: 265

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 # 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]]) # getting the value of x in an equation inputMatrix * x = inputVector x_value = numpy.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模块来执行矩阵和线性代数操作。我们学习了如何计算矩阵的转置、逆置和行列式。我们还学习了如何在线性代数中进行一些计算,如解方程和确定特征值。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3