- Python Data Analysis
- Ivan Idris
- 368字
- 2025-04-04 21:30:24
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:
- 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]]
- 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.]
- 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 matchingeigenvectors
, 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
andeigenvectors
values will be:First tuple of eig [ 2. 1.] Second tuple of eig [[ 0.89442719 0.70710678] [ 0.4472136 0.70710678]]
- Check the result.
Check the answer with the
dot
()
function by computing both sides of the eigenvalues equationAx = 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]]