官术网_书友最值得收藏!

  • Python Data Analysis
  • Ivan Idris
  • 368字
  • 2021-08-05 17:31:54

Finding eigenvalues and eigenvectors with NumPy

Eigenvalues are scalar solutions to the equation Ax = ax, where A is a two-dimensional matrix and x is a one-dimensional vector. Eigenvectors are vectors corresponding to eigenvalues.

Note

Eigenvalues and eigenvectors are fundamental in mathematics and are used in many important algorithms, such as Principal Component Analysis (PCA). PCA can be used to simplify the analysis of large datasets.

The eigvals() subroutine in the numpy.linalg package computes eigenvalues. The eig() function gives back a tuple holding eigenvalues and eigenvectors.

We will obtain the eigenvalues and eigenvectors of a matrix with the eigvals() and eig() functions of the numpy.linalg subpackage. We will check the outcome by applying the dot() function (see eigenvalues.py in this book's code):

import numpy as np

A = np.mat("3 -2;1 0")
print "A\n", A

print "Eigenvalues", np.linalg.eigvals(A)

eigenvalues, eigenvectors = np.linalg.eig(A)
print "First tuple of eig", eigenvalues
print "Second tuple of eig\n", eigenvectors

for i in range(len(eigenvalues)):
   print "Left", np.dot(A, eigenvectors[:,i])
   print "Right", eigenvalues[i] * eigenvectors[:,i]
   print

Let's calculate the eigenvalues of a matrix:

  1. Create the matrix.

    The following code will create a matrix:

    A = np.mat("3 -2;1 0")
    print "A\n", A

    The matrix we created looks like this:

    A
    [[ 3 -2]
     [ 1 0]]
    
  2. Calculate eigenvalues with the eig() function.

    Apply the eig() subroutine:

    print "Eigenvalues", np.linalg.eigvals(A)

    The eigenvalues of the matrix are as follows:

    Eigenvalues [ 2. 1.]
    
  3. Get eigenvalues and eigenvectors with eig().

    Get the eigenvalues and eigenvectors with the eig() function. This routine returns a tuple, where the first element holds eigenvalues and the second element contains matching eigenvectors, set up column-wise:

    eigenvalues, eigenvectors = np.linalg.eig(A)
    print "First tuple of eig", eigenvalues
    print "Second tuple of eig\n", eigenvectors

    The eigenvalues and eigenvectors values will be:

    First tuple of eig [ 2. 1.]
    Second tuple of eig
    [[ 0.89442719 0.70710678]
     [ 0.4472136 0.70710678]]
    
  4. Check the result.

    Check the answer with the dot () function by computing both sides of the eigenvalues equation Ax = ax:

    for i in range(len(eigenvalues)):
       print "Left", np.dot(A, eigenvectors[:,i])
       print "Right", eigenvalues[i] * eigenvectors[:,i]
       print

    The output is as follows:

    Left [[ 1.78885438]
     [ 0.89442719]]
    Right [[ 1.78885438]
     [ 0.89442719]]
    Left [[ 0.70710678]
     [ 0.70710678]]
    Right [[ 0.70710678]
     [ 0.70710678]]
    
主站蜘蛛池模板: 海兴县| 灵丘县| 东乌珠穆沁旗| 田林县| 鹤庆县| 苗栗市| 抚顺县| 常山县| 五华县| 来安县| 汉源县| 平乐县| 昭苏县| 黔南| 大余县| 济宁市| 青岛市| 太湖县| 和静县| 铁岭县| 磐石市| 瑞昌市| 竹北市| 田东县| 江阴市| 宁波市| 东丰县| 寿阳县| 化州市| 南城县| 仪征市| 漳州市| 虞城县| 伊通| 广州市| 汉中市| 白朗县| 遂昌县| 延川县| 上栗县| 吉首市|