>>> 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 - A2
の非ゼロの要素数が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
A1 != A2
の非ゼロの要素数が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
scipy.sparse.coo_matrix
やscipy.sparse.csc_matix
でも同様の比較方法が使える。