Fastest way to compute k largest eigenvalues and corresponding eigenvectors with numpy

Anthony Bak picture Anthony Bak · Aug 28, 2012 · Viewed 27.8k times · Source

I have a large NxN dense symmetric matrix and want the eigenvectors corresponding to the k largest eigenvalues. What's the best way to find them (preferably using numpy but perhaps in general using blas/atlas/lapack if that's the only way to go)? In general N is much much larger then k (say N > 5000, k < 10).

Numpy seems to only have functions for finding the k largest eigenvalues if my starting matrix is sparse.

Answer

Andy Hayden picture Andy Hayden · Aug 29, 2012

In SciPy, you can use the linalg.eigh function, with the eigvals parameter.

eigvals : tuple (lo, hi) Indexes of the smallest and largest (in ascending order) eigenvalues and corresponding eigenvectors to be returned: 0 <= lo < hi <= M-1. If omitted, all eigenvalues and eigenvectors are returned.

Which in your case should be set to (N-k,N-1).