In [442]: np.linalg.pinv(M)
...
LinAlgError: 0-dimensional array given. Array must be at least two-dimensional
有一个稀疏的linalg inv,但它只是spsolve(A,I)。它还警告If the inverse ofAis expected to be non-sparse, it will likely be faster to convertAto dense and use scipy.linalg.inv.同样的警告可能适用于pinv或等效物。在
试着看看np.linalg.pinv公司公司名称:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.pinv.html
这是“矩阵的广义逆”,对矩形(非正方形)矩阵有效。注意,正如已经指出的,矩形矩阵没有唯一的逆,但是,如果我们施加额外的要求(通过最小二乘法最小化重建误差),我们可以得到唯一的答案。只是要小心解释。在
有空的话可以在这里多读一些:https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse
另外,由于您使用的是scipy库中的CSR矩阵,我假设速度很重要,所以请阅读以下内容:
The difference of pseudo-inverse between SciPy and Numpy
我不确定是否有类似于pinv的CSR矩阵的方法,但是如果没有,您可以使用“
my_csr_matrix.toarray()
”方法将CSR转换为numpy矩阵,但是要考虑开销等因素(这将取决于应用程序是否可行)。在做一个小数组:
制作稀疏副本:
^{pr2}$toarray
的pinv
与之前相同:不能直接在稀疏矩阵上使用numpy inv-因为它不知道如何正确地读取数据结构
有一个稀疏的linalg
inv
,但它只是spsolve(A,I)
。它还警告If the inverse of
Ais expected to be non-sparse, it will likely be faster to convert
Ato dense and use scipy.linalg.inv.
同样的警告可能适用于pinv
或等效物。在我没有立即看到稀疏linalg列表中的
pinv
,但它确实有一个lsqr
。在=================
pseudo inverse of sparse matrix in python(2011年)
支持伪逆很可能是稠密的观点。但它也提出了一种利用
svds
的稀疏解决方案。在还有
How to calculate the generalized inverse of a Sparse Matrix in scipy
相关问题 更多 >
编程相关推荐