Book Image

Python Data Analysis

By : Ivan Idris
Book Image

Python Data Analysis

By: Ivan Idris

Overview of this book

Table of Contents (22 chapters)
Python Data Analysis
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Key Concepts
Online Resources
Index

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...