博客
关于我
如何计算方阵的特征值和特征向量np.linalg.eig()
阅读量:550 次
发布时间:2019-03-09

本文共 7168 字,大约阅读时间需要 23 分钟。

关于这部分的理论知识可以参考我的这篇博客,下面主要介绍如何计算方阵的特征值和特征向量

目录

1.np.linalg.eig()

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

该函数的原型如下:

def eig(a):	Parameters	----------	a : (..., M, M) array	Matrices for which the eigenvalues and right eigenvectors will	be computed	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    v : (..., M, M) array        The normalized (unit "length") eigenvectors, such that the        column ``v[:,i]`` is the eigenvector corresponding to the        eigenvalue ``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]])>>> aarray([[ 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; 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 the e-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-9w, v = LA.eig(a)w; varray([1., 1.])array([[1., 0.],       [0., 1.]])

5.官方完整说明

官方的api

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

"""    Compute the eigenvalues and right eigenvectors of a square array.    Parameters    ----------    a : (..., M, M) array        Matrices for which the eigenvalues and right eigenvectors will        be computed    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    v : (..., M, M) array        The normalized (unit "length") eigenvectors, such that the        column ``v[:,i]`` is the eigenvector corresponding to the        eigenvalue ``w[i]``.    Raises    ------    LinAlgError        If the eigenvalue computation does not converge.    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.    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.    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.]])    """

转载地址:http://ocqsz.baihongyu.com/

你可能感兴趣的文章
nginx反向代理
查看>>
Nginx反向代理
查看>>
nginx反向代理、文件批量改名及统计ip访问量等精髓总结
查看>>
Nginx反向代理与正向代理配置
查看>>
Nginx反向代理及负载均衡实现过程部署
查看>>
Nginx反向代理和负载均衡部署指南
查看>>
Nginx反向代理是什么意思?如何配置Nginx反向代理?
查看>>
nginx反向代理解决跨域问题,使本地调试更方便
查看>>
nginx反向代理转发、正则、重写、负摘均衡配置案例
查看>>
Nginx反向代理配置
查看>>
Nginx启动SSL功能,并进行功能优化,你看这个就足够了
查看>>
nginx启动脚本
查看>>
Nginx和Tomcat的区别
查看>>
Nginx在Windows上和Linux上(Docker启动)分别配置基本身份认证示例
查看>>
Nginx在Windows下载安装启动与配置前后端请求代理
查看>>
Nginx在开发中常用的基础命令
查看>>
Nginx基础知识点与使用场景梳理
查看>>
Nginx多域名,多证书,多服务配置,实用版
查看>>
nginx如何实现图片防盗链
查看>>
Nginx学习总结(10)——Nginx前后端分离将多个请求转发到多个Tomcat,负载均衡反向代理
查看>>