[PYTHON] How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same

>>> 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.

reference

Recommended Posts

How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same
How to check if the contents of the dictionary are the same in Python by hash value
How to handle multiple versions of CUDA in the same environment
[TensorFlow 2] How to check the contents of Tensor in graph mode
How to get a list of files in the same directory with python
How to get the number of digits in Python
How to check in Python if one of the elements of a list is in another list
[Ubuntu] How to delete the entire contents of a directory
[python] How to check if the Key exists in the dictionary
How to see the contents of the Jupyter notebook ipynb file
How to connect the contents of a list into a string
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
How to determine the existence of a selenium element in Python
How to implement Java code in the background of RedHat (LinuxONE)
How to change the color of just the button pressed in Tkinter
[EC2] How to install chrome and the contents of each command
Output the contents of ~ .xlsx in the folder to HTML with Python
How to smartly define objects that are commonly used in View
How to check the memory size of a dictionary in Python
How to get the vertex coordinates of a feature in ArcPy
Create a function to get the contents of the database in Go
How to display bytes in the same way in Java and Python
How to check the version of Django
How to check local GAE from iPhone browser in the same LAN
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to copy and paste the contents of a sheet in Google Spreadsheet in JSON format (using Google Colab)
How to pass the execution result of a shell command in a list in Python
I want to visualize where and how many people are in the factory
How to use the C library in Python
How to find the area of the Voronoi diagram
How to mention a user group in slack notification, how to check the id of the user group
How to keep track of work in Powershell
Check if the characters are similar in Python
How to uniquely identify the source of access in the Django Generic Class View
How to find out what kind of files are stored in S3 in Python
The contents of the Python tutorial (Chapter 5) are itemized.
Summary of how to import files in Python 3
The contents of the Python tutorial (Chapter 4) are itemized.
How to count the number of elements in Django and output to a template
How to access the contents of a Linux disk on a Mac (but read-only)
Summary of how to use MNIST in Python
The contents of the Python tutorial (Chapter 10) are itemized.
Determining if there are birds in the image
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to find the coefficient of the trendline that passes through the vertices in Python
How to get the files in the [Python] folder
The contents of the Python tutorial (Chapter 6) are itemized.
How to change the appearance of unselected Foreign Key fields in Django's ModelForm
The contents of the Python tutorial (Chapter 3) are itemized.
How to make the font width of jupyter notebook put in pyenv equal width
How to install the deep learning framework Tensorflow 1.0 in the Anaconda environment of Windows
About the matter that the contents of Python print are not visible in docker logs
How to display the modification date of a file in C language up to nanoseconds
[Python] How to calculate the approximation formula of the same intercept 0 as Excel [scikit-learn] Memo
What to do if the progress bar is not displayed in tqdm of python
[Note] How to write QR code and description in the same image with python
How to limit the API to be published in the C language shared library of Linux
How to count the number of occurrences of each element in the list in Python with weight
[Implementation explanation] How to use the Japanese version of BERT in Google Colaboratory (PyTorch)
How to know the port number of the xinetd service
How to get the variable name itself in python