相关文章推荐
烦恼的毛豆  ·  C# ...·  2 年前    · 
潇洒的四季豆  ·  js ...·  2 年前    · 
首页 > 脚本专栏 > python > numpy求矩阵的特征值与特征向量

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

作者:Codefmeister

这篇文章主要介绍了numpy求矩阵的特征值与特征向量(np.linalg.eig函数用法),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

求矩阵的特征值与特征向量(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)))
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]]))
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)
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)
array([1., 1.])
array([[1., 0.],
       [0., 1.]])

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

您可能感兴趣的文章:
  • numpy求矩阵的特征值与特征向量(np.linalg.eig函数用法)
    numpy求矩阵的特征值与特征向量(np.linalg.eig函数用法)
    2023-02-02
  • 使用Pytorch如何完成多分类问题
    使用Pytorch如何完成多分类问题
    2023-02-02
  • python中super().__init__()作用详解
    python中super().__init__()作用详解
    2023-02-02
  • python程序中调用其他程序的实现
    python程序中调用其他程序的实现
    2023-02-02
  • numpy求矩阵的特征值与特征向量(np.linalg.eig函数用法)
    numpy求矩阵的特征值与特征向量(np.linalg.eig函数用法)
    2023-02-02
  • python numpy.linalg.norm函数的使用及说明
    python numpy.linalg.norm函数的使用及说明
    2023-02-02
  • Python求字符串的长度示例代码
    Python求字符串的长度示例代码
    2023-02-02
  • 关于yolov8训练的一些改动及注意事项
    关于yolov8训练的一些改动及注意事项
    2023-02-02
  • 美国设下计谋,用娘炮文化重塑日本,已影响至中国
    美国设下计谋,用娘炮文化重塑日本,已影响至中国
    2021-11-19
  • 时空伴随者是什么意思?时空伴随者介绍
    时空伴随者是什么意思?时空伴随者介绍
    2021-11-09
  • 工信部称网盘企业免费用户最低速率应满足基本下载需求,天翼云盘回应:坚决支持,始终
    工信部称网盘企业免费用户最低速率应满足基本下载需求,天翼云盘回应:坚决支持,始终
    2021-11-05
  • 2022年放假安排出炉:五一连休5天 2022年所有节日一览表
    2022年放假安排出炉:五一连休5天 2022年所有节日一览表
    2021-10-26
  • 电脑版 - 返回首页

    2006-2023 脚本之家 JB51.Net , All Rights Reserved.
    苏ICP备14036222号