稀疏矩阵的除法 [英] Division of sparse matrix

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

问题描述

我有一个包含45671x45671元素的scipy.sparse矩阵.在此矩阵中,某些行仅包含"0"值.

I have a scipy.sparse matrix with 45671x45671 elements. In this matrix, some rows contain only '0' value.

我的问题是,如何将各行值除以行总和.显然,使用for循环是可行的,但是我正在寻找一种有效的方法...

My question is, how to divide each row values by the row sum. Obviously, with for loop it's work, but I look for an efficient method...

我已经尝试过:

  • matrix / matrix.sum(1),但是我有MemoryError问题.
  • matrix / scs.csc_matrix((matrix.sum(axis=1)))ValueError: inconsistent shapes
  • 其他古怪的东西...
  • matrix / matrix.sum(1) but I have MemoryError issue.
  • matrix / scs.csc_matrix((matrix.sum(axis=1))) but ValueError: inconsistent shapes
  • Other wacky things...

此外,我想跳过仅包含'0'值的行.

Moreover, I want to skip rows with only '0' values.

所以,如果您有任何解决方案...

So, if you have any solution...

提前谢谢!

推荐答案

我有一个M闲逛:

In [241]: M
Out[241]: 
<6x3 sparse matrix of type '<class 'numpy.uint8'>'
    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会产生内存错误.

That will explain the memory error - if M is so large that M.A produces a memory error.

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

我不太确定这是怎么回事.

I'm not entirely sure what is going on here.

按元素乘法有效

In [266]: M.multiply(S)
Out[266]: 
<6x3 sparse matrix of type '<class 'numpy.uint32'>'
    with 6 stored elements in Compressed Sparse Row format>

因此,如果我将S构造为S = sparse.csr_matrix(1/M.sum(1))

So it should work if I construct S as S = sparse.csr_matrix(1/M.sum(1))

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

If some of the rows sum to zero, you have a division by zero problem.

如果我将M修改为具有0行

If I modify M to have 0 row

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 '<class 'numpy.float64'>'
    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来证明这一点,但是它建议一种有用的方法.行总和将是密集的,因此您可以使用通常的密集数组方法清除其逆.

This isn't the best M to demonstrate this on, but it suggests a useful approach. The row sum will be dense, so you can clean up its inverse using the usual dense array approaches.

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

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