![]() |
VOOZH | about |
In scientific computing, large matrices often contain mostly zero values. Storing and multiplying these as dense arrays wastes both memory and processing time. SciPy’s scipy.sparse module efficiently handles sparse matrices(2D arrays with mostly zero values) designed specifically for fast storage and computation.
Two commonly used classes in this module are:
To multiply two sparse matrices, .multiply() method is used both in csr_matrix and in csc_matrix. This method supports multiplication between matrices of same format (e.g., CSR × CSR) or even different formats (e.g., CSR × CSC).
Let's understand it better through Example.
In this example two sparse matrices are created using csc_matrix() class. These matrices are then multiplied element-wise using multiply() method which performs efficient operations on only the non-zero elements.
Output
Here, two sparse matrices are created using csr_matrix() class. These matrices are then multiplied element-wise using the multiply() method focusing only on non-zero elements.
Output
In this example two sparse matrices are created, one in CSC format and other in CSR format. Then element-wise multiplication is performed using multiply() method to demonstrate compatibility between different sparse matrix.