I am trying to use scipy.sparse.linalg.eigsh with fixed seed.
In order to that, I need to specify the v0 parameter. However, I am unable to figure out what exactly needs to go into v0, as the documentation is very meagre here (it merely says numpy.ndarray) and the error message is not informative for me.
Code:
import numpy as np
import scipy.sparse.linalg
A = scipy.sparse.rand(10,10)
# v0 = np.random.rand(10,10)
v0 = np.random.rand(10,5)
w, v = scipy.sparse.linalg.eigsh(A, k=5, v0=v0)
Error:
error: failed in converting 10th argument `workd' of _arpack.dsaupd to C/Fortran array
The correct way to get reproducible results from eigsh
is:
import numpy as np
import scipy.sparse.linalg
np.random.seed(0)
A = scipy.sparse.rand(10,10)
v0 = np.random.rand(min(A.shape))
w, v = scipy.sparse.linalg.eigsh(A, k=5, v0=v0)
Same results everytime. (Credit to @hpaulj for the correct comment)
Note that fixing the seed without setting v0
is not sufficient:
np.random.seed(0)
A = scipy.sparse.rand(10,10)
w, v = scipy.sparse.linalg.eigsh(A, k=5)
Different results every time.