LoginSignup
0
1

More than 5 years have passed since last update.

scipy.sparse.csr_matrixのオブジェクトの内容が同じか比較する方法

Last updated at Posted at 2017-09-11
>>> 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_matrixscipy.sparse.csc_matixでも同様の比較方法が使える。

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1