Search code examples
pythonscipysparse-matrixsvd

Why does Scipy sparse implementation returns incorrect number of singular values although dense one returns correct?


I am trying to evaluate Singular Value decompostion on my data set which has dimensions 1401375, 51. When I try to use scipy.linalg.svd, it gives me correct number of singular values (51) and all of them are correct (I checked against LAPACK). However, when I try using scipy.sparse.linalg.svds it gives an error: ValueError: k must be between 1 and min(A.shape), k=51

  _, sigma, _ = sp.sparse.linalg.svds(A_s, k=51, return_singular_vectors='vh',which='LM')
_, sigma, _ = sp.linalg.svd(A_D, full_matrices=False,
                                            overwrite_a=True, check_finite=False,
                                            compute_uv=True)

I expect the sparse version to return 51 singular values, but it fails. When I lower the value of k to 50, it executes successfully.


Solution

  • You can find this on the scipy documentation for scipy, but k must be: 1 <= k < min(A.shape) so if your shape is 1401375, 51 then the min is 51 and k has to be less than that not less than or equal