Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between 2 scipy sparse csr matrices

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 Answers

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)
like image 188
MaxU - stop WAR against UA Avatar answered Apr 30 '26 02:04

MaxU - stop WAR against UA