>>> import scipy
>>> scipy.__version__
'0.19.1'
>>> from scipy.sparse import csr_matrix
>>> A1 = csr_matrix([[1,2],[2,3],[3,4]])
>>> A2 = csr_matrix([[1,2],[2,3],[3,4]])
>>> B = csr_matrix([[1,2],[3,4],[5,6]])
ʻA1 --Check if the number of non-zero elements in A2` becomes 0.
# equal
>>> A1 - A2
<3x2 sparse matrix of type '<class 'numpy.int64'>'
with 0 stored elements in Compressed Sparse Row format>
>>> (A1 - A2).nnz == 0
True
# not equal
>>> A1 - B
<3x2 sparse matrix of type '<class 'numpy.int64'>'
with 4 stored elements in Compressed Sparse Row format>
>>> (A1 - B).nnz == 0
False
It can also be confirmed by checking whether the number of non-zero elements of ʻA1! = A2` becomes 0.
# equal
>>> A1 != A2
<3x2 sparse matrix of type '<class 'numpy.bool_'>'
with 0 stored elements in Compressed Sparse Row format>
>>> (A1 != A2).nnz == 0
True
# not equal
>>> A1 != B
<3x2 sparse matrix of type '<class 'numpy.bool_'>'
with 4 stored elements in Compressed Sparse Row format>
>>> (A1 != B).nnz == 0
False
A similar comparison method can be used with scipy.sparse.coo_matrix
and scipy.sparse.csc_matix
.
Recommended Posts