0

This is related and there is another this dealing with equality, but if I have two sparse matrices (and they are coo format) how do I find which positions in the matrices are different?

If I were to subtract two matrices, I would still have to figure out which ones are nonzero.

I am seeing some non-deterministic values in my matrix formation, therefore I am trying to to find out which cells are changing (a smaller number) and which are consistent across runs (the overwhelming majority)

demongolem
  • 9,474
  • 36
  • 90
  • 105
  • 1
    Are there differences in sparsity (the indices of the nonzero elements), or just differences in values of matching nonzero elements? `coo` matrices can have the nonzero values in any order, which will make comparisons hard. And math (such as subtraction) is not defined for `coo` format - they are usually converted to `csr` for that. – hpaulj Sep 11 '20 at 17:43
  • @hpaulj If I call find on the csr version of matrix 1 and then find on the csr version of matrix 2, it does appear that there is a difference in sparsity (that is some values in one of the matrices is zero in the other). – demongolem Sep 11 '20 at 18:52

1 Answers1

1

scipy.sparse has a built in function called find for determining which entries are non-zero.

Subtracting the two matricies from one another and feeding it into scipy.sparse.find will return all the entries which have changed (and thus were different in the original matricies).

NolantheNerd
  • 338
  • 3
  • 10
  • 1
    This answer taken with @hpaulj worked for me. First, get your coo matrices into csr format. Then second, you are able to subtract the two matrices like C= B -A where B and A are the two matrices being compared. Third, call find on the difference matrix and get back a 3-tuple which gives rows, columns and (nonzero) values respectively. – demongolem Sep 11 '20 at 18:38