c语言稀疏矩阵做除法,稀疏矩阵的除法

小编典典

我M闲逛:

In [241]: M

Out[241]:

<6x3 sparse matrix of type ''

with 6 stored elements in Compressed Sparse Row format>

In [242]: M.A

Out[242]:

array([[1, 0, 0],

[0, 1, 0],

[0, 0, 1],

[0, 1, 0],

[0, 0, 1],

[1, 0, 0]], dtype=uint8)

In [243]: M.sum(1) # dense matrix

Out[243]:

matrix([[1],

[1],

[1],

[1],

[1],

[1]], dtype=uint32)

In [244]: M/M.sum(1) # dense matrix - full size of M

Out[244]:

matrix([[ 1., 0., 0.],

[ 0., 1., 0.],

[ 0., 0., 1.],

[ 0., 1., 0.],

[ 0., 0., 1.],

[ 1., 0., 0.]])

这将解释内存错误-如果内存错误M太大,则会M.A产生内存错误。

In [262]: S = sparse.csr_matrix(M.sum(1))

In [263]: S.shape

Out[263]: (6, 1)

In [264]: M.shape

Out[264]: (6, 3)

In [265]: M/S

....

ValueError: inconsistent shapes

我不完全确定这里发生了什么。

元素明智的乘法工作

In [266]: M.multiply(S)

Out[266]:

<6x3 sparse matrix of type ''

with 6 stored elements in Compressed Sparse Row format>

所以如果我构造S为S = sparse.csr_matrix(1/M.sum(1))

如果某些行的总和为零,则存在除以零的问题。

如果我修改M为0行

In [283]: M.A

Out[283]:

array([[1, 0, 0],

[0, 1, 0],

[0, 0, 0],

[0, 1, 0],

[0, 0, 1],

[1, 0, 0]], dtype=uint8)

In [284]: S = sparse.csr_matrix(1/M.sum(1))

/usr/local/bin/ipython3:1: RuntimeWarning: divide by zero encountered in true_divide

#!/usr/bin/python3

In [285]: S.A

Out[285]:

array([[ 1.],

[ 1.],

[ inf],

[ 1.],

[ 1.],

[ 1.]])

In [286]: M.multiply(S)

Out[286]:

<6x3 sparse matrix of type ''

with 5 stored elements in Compressed Sparse Row format>

In [287]: _.A

Out[287]:

array([[ 1., 0., 0.],

[ 0., 1., 0.],

[ 0., 0., 0.],

[ 0., 1., 0.],

[ 0., 0., 1.],

[ 1., 0., 0.]])

这不是最好M的证明,但是它建议一种有用的方法。行总和将是密集的,因此您可以使用通常的密集数组方法清除其逆。

2020-12-20

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值