Largest Eigenvector and Possible Scipy Weirdness
Not sure if this is a package bug or some other reason, but here we go.
I am using the following package to find the largest eigenvalue and its corresponding eigenvector over a symmetric similarity score matrix (size 10x10):
scipy.sparse.linalg.eigen.arpack.eigsh
So:
scipy.sparse.linalg.eigen.arpack.eigsh(mymatrix, 1, which='LM')
Now my question is, when I run it multiple times (using the same matrix, settings, etc.), sometimes the values in the eigenvectors are positive and sometimes negative (see Run 3 ).
Does anyone know why this might be, or is this a bug? It doesn't seem to have a template, but it only happens when I run the code without closing Python after each iteration (for example, I press F5 after each run).
### Run 1: ###
[[-0.31056873]
[-0.31913092]
[-0.3149287 ]
[-0.32262921]
[-0.32190688]
[-0.31292658]
[-0.32424732]
[-0.31885208]
[-0.31808024]
[-0.298174 ]]
### Run 2: ###
[[-0.31056873]
[-0.31913092]
[-0.3149287 ]
[-0.32262921]
[-0.32190688]
[-0.31292658]
[-0.32424732]
[-0.31885208]
[-0.31808024]
[-0.298174 ]]
### Run 3:###
[[ 0.31056873]
[ 0.31913092]
[ 0.3149287 ]
[ 0.32262921]
[ 0.32190688]
[ 0.31292658]
[ 0.32424732]
[ 0.31885208]
[ 0.31808024]
[ 0.298174 ]]
### Run 4: ###
[[-0.31056873]
[-0.31913092]
[-0.3149287 ]
[-0.32262921]
[-0.32190688]
[-0.31292658]
[-0.32424732]
[-0.31885208]
[-0.31808024]
[-0.298174 ]]
This is not a major problem, but I don't like the vagueness in my code; -)
Thank you very much in advance,
Martyn
source to share
This is actually a mathematical question.
But the reason is that there is an arbitrary phase when calculating the eigenvectors. You are solving Ax = bx for x. The equation is invariant under multiplication by (possibly ocmplex) phase.
As for why this is happening (seemingly) randomly, I don't know. But I'm sure this is not a mistake.
source to share
You are using a function with 'LM'
in a which
function parameter . Using these settings, you are looking for the eigenvalue of the highest magnitude , I noticed that the smallest and largest eigenvalues of your matrix are pretty close in absolute value. Add that to the fact that the algorithm evaluates the eigenvalues and you get the answer to your question.
Try to run the code with which = 'LR'
, this will give you your own value with the highest real value (your own values are real anyway).
source to share