- 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:
- 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]]
- Dreamweaver CS3網頁制作融會貫通
- 返璞歸真:UNIX技術內幕
- PyTorch深度學習實戰
- 自主研拋機器人技術
- CompTIA Network+ Certification Guide
- 大學C/C++語言程序設計基礎
- Excel 2007常見技法與行業應用實例精講
- Applied Data Visualization with R and ggplot2
- WOW!Photoshop CS6完全自學寶典
- 人工智能:智能人機交互
- 計算機應用基礎實訓·職業模塊
- 微機組裝與維護教程
- Spark Streaming實時流式大數據處理實戰
- 微控制器的選擇與應用
- Building Impressive Presentations with Impress.js