[SciPy-user] getting the "best" two eigenvectors for a PCA analysis with a power method

Syd Diamond syd.diamond at gmail.com
Fri Aug 26 17:46:37 CDT 2005

```I'll be honest -- I'm mostly interested in the end not the means right
now.  I need to find the first and second principal components of a
120x520 matrix.

Right now, I'm using the "from LinearAlgebra import eigenvectors"
method to find eigenvectors of the covariance matrix.  This worked
fine for my 120x90 matrix, but it's choking on the 120x520 matrix.
When I talked to a friend, he said usually shortcuts were taken...
like the power method for computing the dominant eigenpair.

I've googled around without any luck.  Can anyone please give me some
guidance here?

In return, I can offer some hilariously inefficient code from a guy
who still doesn't know what he's doing... enjoy :)

Thank you everyone for your continued help.

(And if you can offer guidance on the code as well, by all means be my guest :))

from Numeric import array, zeros, shape, Float
from LinearAlgebra import eigenvectors
from scipy import mean, std, cov

def princomp(X):
evals, evecs = eigenvectors(cov(X))
eSorter = {}
for index in xrange(len(evals)):
eval = evals[index]
evec = evecs[index]
eSorter[eval] = evec
evalsList = eSorter.keys().sort()
evecNew = zeros(shape(evecs), typecode=Float)
counter = 0
for eval in evals:
evec = eSorter[eval]
evecNew[counter] = evec
counter += 1
return evecNew

```