我有一个需要估计其等级的scipy.sparse.csr.csr_matrix
类型的稀疏矩阵。我发现this on scipy.org似乎很适合这项工作,但不支持csr_matrix
。
from scipy.sparse import load_npz
from scipy.linalg.interpolative import estimate_rank
X = load_npz("https://drive.google.com/uc?export=download&id=1SSR6JWEqG4DXRU9qo78682D9pGJF3Wr0")
print("Rank:", estimate_rank(X, eps=100))
TypeError:无效的输入类型(必须为数组或LinearOperator)
稀疏矩阵具有超过50K的行和近40K的列。首先将其转换为numpy数组似乎毫无意义。我应该怎么做才能使其正常工作?
以下内容也不起作用。
from scipy.sparse import load_npz, linalg
from scipy.linalg.interpolative import estimate_rank
X = load_npz("https://drive.google.com/uc?export=download&id=1SSR6JWEqG4DXRU9qo78682D9pGJF3Wr0")
print("Rank:", estimate_rank(linag.aslinearoperator(X), eps=100))
我用过稀疏,但没有用过estimate_rank
。但是我可以阅读错误和文档。
In [23]: from scipy import sparse
In [24]: from scipy.sparse import linalg
In [25]: M = sparse.random(100,100,.2, 'csr')
In [36]: inter.estimate_rank(M,.001)
---------------------------------------------------------------------------
...
TypeError: invalid input type (must be array or LinearOperator)
测试数组选项:
In [37]: inter.estimate_rank(M.A,.1)
Out[37]: 100
测试linearoperator选项:
In [38]: from scipy.sparse import linalg
In [39]: L = linalg.aslinearoperator(M)
In [40]: L
Out[40]: <100x100 MatrixLinearOperator with dtype=float64>
In [41]: inter.estimate_rank(L,.001)
Out[41]: 99