difference between 2 scipy sparse csr matrices

白昼怎懂夜的黑 提交于 2021-01-27 20:17:32

问题


I have 2 scipy.sparse.csr_matrix like this:

A = [ 1 0 1 0 0 1
      1 0 0 1 0 0
      0 1 0 0 0 0 ]

B = [ 1 0 1 0 1 1
      1 1 0 1 0 0
      1 1 1 0 0 0 ]

I am willing to get the "new ones" that appeared in B but weren't in A.

C = [ 0 0 0 0 1 0
      0 1 0 0 0 0
      1 0 1 0 0 0 ]

回答1:


IIUC it should be pretty straightforward:

In [98]: C = B - A

In [99]: C
Out[99]:
<3x6 sparse matrix of type '<class 'numpy.int32'>'
        with 4 stored elements in Compressed Sparse Row format>

In [100]: C.A
Out[100]:
array([[0, 0, 0, 0, 1, 0],
       [0, 1, 0, 0, 0, 0],
       [1, 0, 1, 0, 0, 0]], dtype=int32)


来源:https://stackoverflow.com/questions/46798631/difference-between-2-scipy-sparse-csr-matrices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!