numpy求矩阵的特征值与特征向量(np.linalg.eig函数用法)

目录
  • 求矩阵的特征值与特征向量(np.linalg.eig)
    • 语法
    • 功能
    • Parameters
    • Returns
    • Raises
    • Ralated Function:
    • Notes
    • Examples
  • 总结

求矩阵的特征值与特征向量(np.linalg.eig)

语法

np.linalg.eig(a)

功能

Compute the eigenvalues and right eigenvectors of a square array.

求方阵(n x n)的特征值与右特征向量

Parameters

a : (…, M, M) array

Matrices for which the eigenvalues and right eigenvectors will be computed

a是一个矩阵Matrix的数组。每个矩阵M都会被计算其特征值与特征向量。

Returns

w : (…, M) array

The eigenvalues, each repeated according to its multiplicity.
The eigenvalues are not necessarily ordered. The resulting array will be of complex type, unless the imaginary part is zero in which case it will be cast to a real type. When a is real the resulting eigenvalues will be real (0 imaginary part) or occur in conjugate pairs

返回的w是其特征值。特征值不会特意进行排序。返回的array一般都是复数形式,除非虚部为0,会被cast为实数。当a是实数类型时,返回的就是实数。

v : (…, M, M) array

The normalized (unit “length”) eigenvectors, such that the column v[:,i] is the eigenvector corresponding to the eigenvalue w[i].

返回的v是归一化后的特征向量(length为1)。特征向量v[:,i]对应特征值w[i]

Raises

LinAlgError

If the eigenvalue computation does not converge.

Ralated Function:

See Also

eigvals : eigenvalues of a non-symmetric array.
eigh : eigenvalues and eigenvectors of a real symmetric or complex Hermitian (conjugate symmetric) array.
eigvalsh : eigenvalues of a real symmetric or complex Hermitian (conjugate symmetric) array.
scipy.linalg.eig : Similar function in SciPy that also solves the generalized eigenvalue problem.
scipy.linalg.schur : Best choice for unitary and other non-Hermitian normal matrices.

相关的函数有:

  • eigvals:计算非对称矩阵的特征值
  • eigh:实对称矩阵或者复共轭对称矩阵(Hermitian)的特征值与特征向量
  • eigvalsh: 实对称矩阵或者复共轭对称矩阵(Hermitian)的特征值与特征向量
  • scipy.linalg.eig
  • scipy.linalg.schur

Notes

… versionadded:: 1.8.0

Broadcasting rules apply, see the numpy.linalg documentation for details.

This is implemented using the _geev LAPACK routines which compute the eigenvalues and eigenvectors of general square arrays.

The number w is an eigenvalue of a if there exists a vector v such that a @ v = w * v. Thus, the arrays a, w, and v satisfy the equations a @ v[:,i] = w[i] * v[:,i] for :math:i \\in \\{0,...,M-1\\}.

The array v of eigenvectors may not be of maximum rank, that is, some of the columns may be linearly dependent, although round-off error may obscure that fact. If the eigenvalues are all different, then theoretically the eigenvectors are linearly independent and a can be diagonalized by a similarity transformation using v, i.e, inv(v) @ a @ v is diagonal.

For non-Hermitian normal matrices the SciPy function scipy.linalg.schur is preferred because the matrix v is guaranteed to be unitary, which is not the case when using eig. The Schur factorization produces an upper triangular matrix rather than a diagonal matrix, but for normal matrices only the diagonal of the upper triangular matrix is needed, the rest is roundoff error.

Finally, it is emphasized that v consists of the right (as in right-hand side) eigenvectors of a. A vector y satisfying y.T @ a = z * y.T for some number z is called a left eigenvector of a, and, in general, the left and right eigenvectors of a matrix are not necessarily the (perhaps conjugate) transposes of each other.

References

G. Strang, Linear Algebra and Its Applications, 2nd Ed., Orlando, FL,
Academic Press, Inc., 1980, Various pp.

需要说明的是,特征向量之间可能存在线性相关关系,即返回的v可能不是满秩的。但如果特征值都不同的话,理论上来说,所有特征向量都是线性无关的。

此时可以利用inv(v)@ a @ v来计算特征值的对角矩阵(对角线上的元素是特征值,其余元素为0),同时可以用v @ diag(w) @ inv(v)来恢复a。
同时需要说明的是,这里得到的特征向量都是右特征向量。

即 Ax=λx

Examples

>>> from numpy import linalg as LA

(Almost) trivial example with real e-values and e-vectors.

>>> w, v = LA.eig(np.diag((1, 2, 3)))
>>> w; v
array([1., 2., 3.])
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

Real matrix possessing complex e-values and e-vectors; note that the
e-values are complex conjugates of each other.

>>> w, v = LA.eig(np.array([[1, -1], [1, 1]]))
>>> w; v
array([1.+1.j, 1.-1.j])
array([[0.70710678+0.j        , 0.70710678-0.j        ],
       [0.        -0.70710678j, 0.        +0.70710678j]])

Complex-valued matrix with real e-values (but complex-valued e-vectors);
note that ``a.conj().T == a``, i.e., `a` is Hermitian.

>>> a = np.array([[1, 1j], [-1j, 1]])
>>> w, v = LA.eig(a)
>>> w; v
array([2.+0.j, 0.+0.j])
array([[ 0.        +0.70710678j,  0.70710678+0.j        ], # may vary
       [ 0.70710678+0.j        , -0.        +0.70710678j]])

Be careful about round-off error!

>>> a = np.array([[1 + 1e-9, 0], [0, 1 - 1e-9]])
>>> # Theor. e-values are 1 +/- 1e-9
>>> w, v = LA.eig(a)
>>> w; v
array([1., 1.])
array([[1., 0.],
       [0., 1.]])

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Python:Numpy 求平均向量的实例

    如下所示: >>> import numpy as np >>> a = np.array([[1, 2, 3], [3, 1, 2]]) >>> b = np.array([[5, 2, 6], [5, 1, 2]]) >>> a array([[1, 2, 3], [3, 1, 2]]) >>> b array([[5, 2, 6], [5, 1, 2]]) >>> c = a + b >

  • 详解Python NumPy中矩阵和通用函数的使用

    目录 一.创建矩阵 二.从已有矩阵创建新矩阵 三.通用函数 四.算术运算 在NumPy中,矩阵是 ndarray 的子类,与数学概念中的矩阵一样,NumPy中的矩阵也是二维的,可以使用 mat . matrix 以及 bmat 函数来创建矩阵. 一.创建矩阵 mat 函数创建矩阵时,若输入已为 matrix 或 ndarray 对象,则不会为它们创建副本. 因此,调用 mat() 函数和调用 matrix(data, copy=False) 等价. 1) 在创建矩阵的专用字符串中,矩阵的行与行之

  • numpy.linalg.eig() 计算矩阵特征向量方式

    在PCA中有遇到,在这里记录一下 计算矩阵的特征值个特征向量,下面给出几个示例代码: 在使用前需要单独import一下 >>> from numpy import linalg as LA >>> w, v = LA.eig(np.diag((1, 2, 3))) >>> w; v array([ 1., 2., 3.]) array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]) >>>

  • numpy求矩阵的特征值与特征向量(np.linalg.eig函数用法)

    目录 求矩阵的特征值与特征向量(np.linalg.eig) 语法 功能 Parameters Returns Raises Ralated Function: Notes Examples 总结 求矩阵的特征值与特征向量(np.linalg.eig) 语法 np.linalg.eig(a) 功能 Compute the eigenvalues and right eigenvectors of a square array. 求方阵(n x n)的特征值与右特征向量 Parameters a

  • python numpy.linalg.norm函数的使用及说明

    目录 numpy.linalg.norm函数的使用 np.linalg.norm()函数用法 总结 numpy.linalg.norm函数的使用 1.linalg = linear(线性)+ algebra(代数),norm则表示范数. 首先需要注意的是范数是对向量(或者矩阵)的度量,是一个标量(scalar): 2.函数参数 x_norm=np.linalg.norm(x, ord=None, axis=None, keepdims=False) x: 表示矩阵(也可以是一维) ord:范数类

  • C++ Eigen库计算矩阵特征值及特征向量

    本文主要讲解利用Eigen库计算矩阵的特征值及特征向量并与Matlab计算结果进行比较. C++Eigen库代码 #include <iostream> #include <Eigen/Dense> #include <Eigen/Eigenvalues> using namespace Eigen; using namespace std; void Eig() { Matrix3d A; A << 1, 2, 3, 4, 5, 6, 7, 8, 9; c

  • python中numpy的矩阵、多维数组的用法

    1. 引言 最近在将一个算法由matlab转成python,初学python,很多地方还不熟悉,总体感觉就是上手容易,实际上很优雅地用python还是蛮难的.目前为止,觉得就算法仿真研究而言,还是matlab用得特别舒服,可能是比较熟悉的缘故吧.matlab直接集成了很多算法工具箱,函数查询.调用.变量查询等非常方便,或许以后用久了python也会感觉很好用.与python相比,最喜欢的莫过于可以直接选中某段代码执行了,操作方便,python也可以实现,就是感觉不是很方便. 言归正传,做算法要用

  • Python numpy中矩阵的基本用法汇总

    Python矩阵的基本用法 mat()函数将目标数据的类型转化成矩阵(matrix) 1,mat()函数和array()函数的区别 Numpy函数库中存在两种不同的数据类型(矩阵matrix和数组array),都可以用于处理行列表示的数字元素,虽然他们看起来很相似,但是在这两个数据类型上执行相同的数学运算可能得到不同的结果,其中Numpy函数库中的matrix与MATLAB中matrices等价. 直接看一个例子: import numpy as np a = np.mat('1 3;5 7')

  • python 的numpy库中的mean()函数用法介绍

    1. mean() 函数定义: numpy.mean(a, axis=None, dtype=None, out=None, keepdims=<class numpy._globals._NoValue at 0x40b6a26c>)[source] Compute the arithmetic mean along the specified axis. Returns the average of the array elements. The average is taken over

  • 详解Numpy扩充矩阵维度(np.expand_dims, np.newaxis)和删除维度(np.squeeze)的方法

    在操作矩阵的时候,不同的接口对于矩阵的输入维度要求不同,输入可能为1-D,2-D,3-D等等.下面介绍一下使用Numpy进行矩阵维度变更的相关方法.主要包括以下几种: 1.np.newaxis扩充矩阵维度 2.np.expand_dims扩充矩阵维度 3.np.squeeze删除矩阵中维度大小为1的维度 np.newaxis,np.expand_dims扩充矩阵维度: import numpy as np x = np.arange(8).reshape(2, 4) print(x.shape)

  • numpy基础教程之np.linalg

    前言 numpy.linalg模块包含线性代数的函数.使用这个模块,可以计算逆矩阵.求特征值.解线性方程组以及求解行列式等.本文讲给大家介绍关于numpy基础之 np.linalg的相关内容,下面话不多说了,来一起看看详细的介绍吧 (1)np.linalg.inv():矩阵求逆 (2)np.linalg.det():矩阵求行列式(标量) np.linalg.norm 顾名思义,linalg=linear+algebra linalg=linear+algebra\mathrm{linalg=li

  • numpy求平均值的维度设定的例子

    废话不多说,我就直接上代码吧! >>> a = np.array([[1, 2], [3, 4]]) >>> np.mean(a) # 将上面二维矩阵的每个元素相加除以元素个数(求平均数) 2.5 >>> np.mean(a, axis=0) # axis=0,计算所有子数组的平均值 array([ 2., 3.]) >>> np.mean(a, axis=1) # axis=1,对每一个子数组,计算它的平均值 array([ 1.5

随机推荐