如何使用Python(可能还有Scipy)估计一个巨大的、稀疏的csr_矩阵的秩?

2024-10-02 02:35:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个巨大的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:无效的输入类型(必须是数组或LinearRoperator)

稀疏矩阵有超过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))

enter image description here ValueError回溯(最近一次调用上次) 在() 3. 4份打印件(类型(X)) ----&燃气轮机;5打印(“文件术语矩阵的秩:”,估计秩(aslinearoperator(X),eps=1))

1帧 /idd中的usr/local/lib/python3.6/dist-packages/scipy/linalg//u interpolative\u backend.py(eps、m、n、matvect) 659:rtype:int 660 """ -->;661 k,ra,ier=_id.idd_find(eps,m,n,matvect) 662如有: 663上升重新编码错误

ValueError:未能创建意图(缓存|隐藏)|可选数组--必须已定义维度,但已获取(-1216667648,)


Tags: fromimportid类型load矩阵scipyeps
1条回答
网友
1楼 · 发布于 2024-10-02 02:35:27

我使用过稀疏,但没有使用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

测试线性构造器选项:

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

相关问题 更多 >

    热门问题