确定稀疏矩阵商 [英] Determining a sparse matrix quotient

查看:100
本文介绍了确定稀疏矩阵商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在python 2.7中划分两个稀疏矩阵,本质上是k = numerator / denominator,结果是类型为sp.csr_matrix的稀疏矩阵.我正在使用scipy as spnumpy as np.

I am looking to divide two sparce matricies in python 2.7, essentially k = numerator / denominator, with the result as a sparse matrix of type sp.csr_matrix. I am using scipy as sp and numpy as np.

要做到这一点,我遵循线性格式,取分子的点积与分母的倒数.这两个项目的格式均为sp.csr_matrix(([],([],[])),shape=[R,R]).

To do this, I am following linear format of taking the dot product of numerator and inverse of denominator. Both items are of format sp.csr_matrix(([],([],[])),shape=[R,R]).

k本身的计算方式是

k = sp.csr_matrix(numerator.dot(sp.linalg.inv(denominator)))

执行此操作将返回警告:

Doing this returns the warning:

SparseEfficiencyWarning: splu requires CSC matrix format
warn('splu requires CSC matrix format', SparseEfficiencyWarning)

以上警告是关于确定k作为两个稀疏矩阵之间的商的标识是什么?

What does the above warning mean in relation to determining the identity of k as the quotient between two sparse matrices?

是否有更有效的方法来生成python(两个稀疏矩阵的商)的稀疏矩阵和稀疏矩阵逆的点积?

Is there a more efficient way to generate the dot product of a sparse matrix and a sparse matrix inverse in python (the quotient of two sparse matrices)?

我以前发现用scipy反转大型稀疏矩阵,但是我不知道这是否会过时.

I had previously found Inverting large sparse matrices with scipy, however I wonder if this may be outdated.

推荐答案

借用2013年的答案:

Borrowing from the 2013 answer:

In [409]: a=np.random.rand(3,3)
In [410]: A=sparse.csr_matrix(a)
In [411]: np.linalg.inv(a)
Out[411]: 
array([[ 26.11275413,  -4.17749006,  -9.82626551],
       [-37.22611759,   9.38404027,  13.80073216],
       [  7.59314843,  -2.04314605,  -1.58410661]])

np inv不是sparse-aware:

In [412]: np.linalg.inv(A) 
 ....
LinAlgError: 0-dimensional array given. Array must be at least two-dimensional

使用from scipy.sparse import linalg:

In [414]: linalg.inv(A).A
/usr/lib/python3/dist-packages/scipy/sparse/linalg/dsolve/linsolve.py:243: SparseEfficiencyWarning: splu requires CSC matrix format
  warn('splu requires CSC matrix format', SparseEfficiencyWarning)
/usr/lib/python3/dist-packages/scipy/sparse/linalg/dsolve/linsolve.py:161: SparseEfficiencyWarning: spsolve is more efficient when sparse b is in the CSC matrix format
  'is in the CSC matrix format', SparseEfficiencyWarning)
Out[414]: 
array([[ 26.11275413,  -4.17749006,  -9.82626551],
       [-37.22611759,   9.38404027,  13.80073216],
       [  7.59314843,  -2.04314605,  -1.58410661]])

因此,请使用csc格式而不是csr:

So use the csc format instead of csr:

In [415]: A1=sparse.csc_matrix(a)
In [416]: linalg.inv(A1).A
Out[416]: 
array([[ 26.11275413,  -4.17749006,  -9.82626551],
       [-37.22611759,   9.38404027,  13.80073216],
       [  7.59314843,  -2.04314605,  -1.58410661]])

相同,但没有出现稀疏警告.在不赘述的情况下,inv必须使用一种在列而不是行上进行迭代的方法.它确实是spsolve(A, I)(I是稀疏的eye矩阵).

Same thing, but without the sparsity warning. Without getting into details, the inv must be using a method that iterates on columns rather than rows. It does spsolve(A, I) (I is a sparse eye matrix).

这篇关于确定稀疏矩阵商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆