Title: Finding 2 largest eigenvalues of large-sparse matrix in Python
Tags: python;numpy;scipy;sparse-matrix;eigenvalue
Question: I want to find the 1st and 2nd largest eigenvalues of a big, sparse and symmetric matrix (in python). scipy.sparse.linalg.eigsh with k=2 gives the second largest eigenvalue with respect to the absolute value - so it's not a good solution. In addition, I can't use numpy methods because my matrix is too big and numpy is too slow...
I am not sure what is the best solution to this problem - any help is welcomed.
Thanks!

Here is the accepted answer: tl;dr: You can use the which='LA' flag as described in the documentation.
I quote:
scipy.sparse.linalg.eigsh(A, k=6, M=None, sigma=None, which='LM',
v0=None, ncv=None, maxiter=None, tol=0, return_eigenvectors=True,
Minv=None, OPinv=None, mode='normal')
Emphasis mine.
which : str [‘LM’ | ‘SM’ | ‘LA’ | ‘SA’ | ‘BE’]
If A is a complex hermitian matrix, ‘BE’ is invalid. Which k eigenvectors and eigenvalues to find:
‘LM’ : Largest (in magnitude) eigenvalues
‘SM’ : Smallest (in magnitude) eigenvalues
‘LA’ : Largest (algebraic) eigenvalues
‘SA’ : Smallest (algebraic) eigenvalues
‘BE’ : Half (k/2) from each end of the spectrum
So, you can specify which='LA' instead of the default LM.
Example:
In [19]: A = numpy.random.randn(5,5)
In [20]: numpy.linalg.eig(A+A.T)[0] #Actual Eigenvalues
Out[20]: array([ 3.32906012,  0.88700157, -1.16620472, -3.54512752, -2.43562899])
In [21]: sp.eigsh(A+A.T,3)[0] #Three Largest (in Magnitude). What you don't want
Out[21]: array([-3.54512752, -2.43562899,  3.32906012])
In [22]: sp.eigsh(A+A.T,3,which='LA')[0] #Three Largest. What you do want
Out[22]: array([-1.16620472,  0.88700157,  3.32906012])


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:05/16/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><numpy><scipy><sparse-matrix><eigenvalue>', 'Title': 'Finding 2 largest eigenvalues of large-sparse matrix in Python', 'ViewCount': 3314, 'id': 30279750, 'user_AboutMe': None, 'user_AccountId': 3767352.0, 'user_DisplayName': 'Mr Y', 'user_Reputation': 33.0, 'user_Views': 6.0, 'user_id': 3622732.0}}