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
", A

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

eigenvalues, eigenvectors = np.linalg.eig(A)
print "First tuple of eig", eigenvalues
print "Second tuple of eig
", 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
    ", 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
    ", 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]]
    
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.104.124