关于这部分的理论知识可以参考我的这篇博客《特征值与特征向量》定义、意义及例子,下面主要介绍如何计算方阵的特征值和特征向量

目录

  • 1.np.linalg.eig()
  • 2.例子
  • 3. 应用
  • 4.其他例子
  • 5.官方完整说明

1.np.linalg.eig()

计算方阵的特征值和特征向量,numpy提供了接口eig,直接调用就行,下面主要介绍该函数:

该函数的原型如下:

def eig(a):Parameters----------a : (..., M, M) arrayMatrices for which the eigenvalues and right eigenvectors willbe computedReturns-------w : (..., M) arrayThe eigenvalues, each repeated according to its multiplicity.The eigenvalues are not necessarily ordered. The resultingarray will be of complex type, unless the imaginary part iszero in which case it will be cast to a real type. When `a`is real the resulting eigenvalues will be real (0 imaginarypart) or occur in conjugate pairsv : (..., M, M) arrayThe normalized (unit "length") eigenvectors, such that thecolumn ``v[:,i]`` is the eigenvector corresponding to theeigenvalue ``w[i]``.

可以看出,该函数的参数只有一个,也就是我们要求特征值和特征向量的方阵(只有方阵才有特征值和特征向量),该函数的返回值有两个分别为w和v。

w: 代表特征值
返回值w是一个一维的array,w的长度和方阵的维度是相同的,对于一个m x m的方阵,其特征值的个数也为m,另外注意特征值不一定是有序排列。

v: 代表特征向量
返回值v是一个array类型的数据,其维度和方阵的维度是相同的,对于一个m x m的方阵,v的维度也为m x m,v中包含m个特征向量,每个特征向量的长度为m,v[:,i]对应特征值为w[i]的特征向量,特征向量是进行单位化(除以所有元素的平方和的开方)的形式。

2.例子

下面举例说明一下:

对于上面的这样一个例子,直接转化为代码:

>>> import numpy as np
>>> a = np.array([[1, -2], [1, 4]])
>>> a
array([[ 1, -2],[ 1,  4]])
>>> np.linalg.eig(a)
(array([2., 3.]), array([[-0.89442719,  0.70710678],[ 0.4472136 , -0.70710678]]))
>>>

可以看出求得的特征值为[2, 3],特征向量为[-0.89442719, 0.4472136] 与[0.70710678, -0.70710678],很明显特征向量进行了单位化,例如第一个特征向量的单位化如下:

3. 应用

对于求特征值与特征向量的应用,最常见的就是对称矩阵的对角化,对于实对称矩阵A,可以对角化转化为下式

转换后的式中p以及对角阵的求取,就可以利用np.linalg.eig()

diag, p = np.linalg.eig(A)

关于对称矩阵对角化的具体求法可以参考这篇博客对称矩阵的对角化.

4.其他例子

官方还给出了一起其他的例子,包括特征值是复数的例子如下:

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.]])

5.官方完整说明

官方的apihttps://numpy.org/doc/stable/reference/generated/numpy.linalg.eig.html

完整的说明我也给放在了下面

    """Compute the eigenvalues and right eigenvectors of a square array.Parameters----------a : (..., M, M) arrayMatrices for which the eigenvalues and right eigenvectors willbe computedReturns-------w : (..., M) arrayThe eigenvalues, each repeated according to its multiplicity.The eigenvalues are not necessarily ordered. The resultingarray will be of complex type, unless the imaginary part iszero in which case it will be cast to a real type. When `a`is real the resulting eigenvalues will be real (0 imaginarypart) or occur in conjugate pairsv : (..., M, M) arrayThe normalized (unit "length") eigenvectors, such that thecolumn ``v[:,i]`` is the eigenvector corresponding to theeigenvalue ``w[i]``.Raises------LinAlgErrorIf the eigenvalue computation does not converge.See Also--------eigvals : eigenvalues of a non-symmetric array.eigh : eigenvalues and eigenvectors of a real symmetric or complexHermitian (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 thegeneralized eigenvalue problem.scipy.linalg.schur : Best choice for unitary and other non-Hermitiannormal matrices.Notes-----.. versionadded:: 1.8.0Broadcasting rules apply, see the `numpy.linalg` documentation fordetails.This is implemented using the ``_geev`` LAPACK routines which computethe 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, someof the columns may be linearly dependent, although round-off error mayobscure that fact. If the eigenvalues are all different, then theoreticallythe eigenvectors are linearly independent and `a` can be diagonalized bya 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 isnot the case when using `eig`. The Schur factorization produces anupper triangular matrix rather than a diagonal matrix, but for normalmatrices only the diagonal of the upper triangular matrix is needed, therest is roundoff error.Finally, it is emphasized that `v` consists of the *right* (as inright-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 eigenvectorsof a matrix are not necessarily the (perhaps conjugate) transposesof each other.References----------G. Strang, *Linear Algebra and Its Applications*, 2nd Ed., Orlando, FL,Academic Press, Inc., 1980, Various pp.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; varray([1., 2., 3.])array([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]])Real matrix possessing complex e-values and e-vectors; note that thee-values are complex conjugates of each other.>>> w, v = LA.eig(np.array([[1, -1], [1, 1]]))>>> w; varray([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; varray([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; varray([1., 1.])array([[1., 0.],[0., 1.]])"""

如何计算方阵的特征值和特征向量np.linalg.eig()相关推荐

  1. numpy求矩阵的特征值与特征向量(np.linalg.eig函数详解)

    numpy求矩阵的特征值与特征向量(np.linalg.eig) 语法 np.linalg.eig(a) 功能 Compute the eigenvalues and right eigenvecto ...

  2. 线性代数:第五章 相似矩阵及二次型(1)向量的内积 方阵的特征值与特征向量 相似矩阵

    第一节  向量的内积 一.数学概念 1. 内积:设有n维向量 令         , 则称[x,y]为向量x与y的内积. 2. 范数:称  为向量x的范数(或长度). 3. 单位向量:称  时的向量x ...

  3. 方阵的特征值与特征向量

    定义 设AAA是nnn阶方阵,如果数λ\lambdaλ和nnn维非零列向量xxx,使关系式 Ax=λxAx = \lambda{x}Ax=λx 成立,那么,数λ\lambdaλ称为方阵AAA的特征值, ...

  4. (17)方阵的特征值与特征向量

    定义1:设 A是 n 阶方阵,若存在数λ和 n 维非零列向量ξ,使得Aξ=λξ,则称λ是方阵A的一个特征值,ξ为方阵A的属于特征值λ的一个特征向量. 注: (1) A 是方阵 (2)特征向量ξ是非零列 ...

  5. MATLAB学习笔记:方阵的特征值与特征向量

    >> A=[1 2;2 1]; >> eigshow(A) MATLAB计算特征值和特征向量的命令: d=eig(A)   仅计算A的特征值(以向量方式d存放) [V,D]=e ...

  6. python求矩阵的秩和特征向量_numpy.linalg.eig() 计算矩阵特征向量方式

    在PCA中有遇到,在这里记录一下 计算矩阵的特征值个特征向量,下面给出几个示例代码: 在使用前需要单独import一下 >>> from numpy import linalg as ...

  7. 方阵的特征值和特征向量的求解案例(三阶方阵)

    下面验证一下 具体特征值的求解过程如下(写的比较乱)

  8. 方阵的特征值和特征向量的求解案例(二阶方阵)

  9. np.linalg 线性代数

    np.linalg.det(a) #计算行列式 np.linalg.norm(a,ord=None) #范数 np.linalg.eig(a) #特征值和特征向量 np.linalg.inv(a) # ...

最新文章

  1. (C++)一行代码递归实现辗转相除法
  2. 【iOS学习笔记】IOS开发中设置applicationIconBadgeNumber和消息推送
  3. SpringMVC中的文件上传
  4. 面试,MySQL 搞透这 20 道就稳了
  5. Android github 快速实现多人协作
  6. css 列 布局,CSS二列三列布局
  7. 学习jvm,关于MAT an internal error occurred during:Parsing heap dump from问题
  8. 为当前会话的所有作用域中的任何表最后生成的标识值。
  9. android 模仿uc标签页,android模仿UC首页天气效果
  10. STM32工作笔记0030---编写跑马灯实验--使用库函数
  11. linux/windows下查看目标文件.a/.lib的函数符号名称
  12. Flutter音频播放插件just_audio入门指南
  13. Unity Behaviors for Interception
  14. 目标:安全纯净互联网 软件升级报38期
  15. 解读:大数据分析及其数据来源
  16. 读《解忧杂货店》有感
  17. 那一份无怨亦无悔的真情实意
  18. DNSPod十问纪中展:从摇滚文青到科学队长
  19. QPixmap存在的坑,内存泄漏
  20. 物联网技术如何改变了我们的生活?

热门文章

  1. H3C NE考试复习GB0-190
  2. 免费学习机器学习交易的资源
  3. 中望3D二次开发 控制台命令转PDF
  4. 【英语语法入门】 第17讲 不定量表达法 (3)
  5. 4F理论对品牌建设与维护的支撑性作用
  6. 【深度学习】这个CV模型,让你猜球必赢
  7. 中国机械式停车设备深度调研与投资战略报告(2021版)
  8. 达人评测 R7 7735HS和R7 6800Hs选哪个 锐龙R77735HS和6800hs对比
  9. QT找不到c++头文件的解决
  10. CF 293 E Close Vertices (树的分治+树状数组)