SciPy – Sparse Matrix Multiplication
Sparse matrices are those matrices that have the most of their elements as zeroes. scipy.sparse is SciPy 2-D sparse matrix package for numeric data. It provides us different classes to create sparse matrices. csc_matrix and csr_matrix are the two such classes. csc_matrix() is used to create a compressed sparse column matrix whereas csr_matrix() is used to create a compressed sparse row matrix.
Note: For more information about how to create a sparse matrix please visit How to Create a Sparse Matrix in Python
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
We use the multiply() method provided in both csc_matrix and csr_matrix classes to multiply two sparse matrices. We can multiply two matrices of same format( both matrices are csc or csr format) and also of different formats ( one matrix is csc and other is csr format).
Example 1: Multiply two csc matrices
We create two sparse matrices of compressed sparse column format using csc_matrix() and multiply them using multiply() method.
Python3
# Python program to multpliply two # csc matrices using multiply() # Import required librariesimport numpy as npfrom scipy.sparse import csc_matrix # Create first csc matrix Arow_A = np.array([0, 0, 1, 2 ])col_A = np.array([0, 1, 0, 1])data_A = np.array([4, 3, 8, 9]) cscMatrix_A = csc_matrix((data_A, (row_A, col_A)), shape = (3, 3)) # print first csc matrixprint("first csc matrix: \n", cscMatrix_A.toarray()) # Create second csc matrix Brow_B = np.array([0, 1, 1, 2 ])col_B = np.array([0, 0, 1, 0])data_B = np.array([7, 2, 5, 1]) cscMatrix_B = csc_matrix((data_B, (row_B, col_B)), shape = (3, 3)) # print second csc matrixprint("second csc matrix:\n", cscMatrix_B.toarray()) # Multiply these matricessparseMatrix_AB = cscMatrix_A.multiply(cscMatrix_B) # print resultant matrixprint("Product Sparse Matrix:\n", sparseMatrix_AB.toarray()) |
Output:

Example 2. Multiply two csr matrices
We create two sparse matrices of compressed sparse row format using csr_matrix() and multiply them using multiply() method.
Python3
# Python program to multpliply two # csr matrices using multiply() # Import required librariesimport numpy as npfrom scipy.sparse import csr_matrix # Create first csr matrix Arow_A = np.array([0, 0, 1, 2 ])col_A = np.array([0, 1, 0, 1])data_A = np.array([4, 3, 8, 9]) csrMatrix_A = csr_matrix((data_A, (row_A, col_A)), shape = (3, 3)) # print first csr matrixprint("first csr matrix: \n", csrMatrix_A.toarray()) # Create second csr matrix Brow_B = np.array([0, 1, 1, 2 ])col_B = np.array([0, 0, 1, 0])data_B = np.array([7, 2, 5, 1]) csrMatrix_B = csr_matrix((data_B, (row_B, col_B)), shape = (3, 3)) # print second scr matrixprint("second csr matrix:\n", csrMatrix_B.toarray()) # Multiply these matricessparseMatrix_AB = csrMatrix_A.multiply(csrMatrix_B) # print resultant matrixprint("Product Sparse Matrix:\n",sparseMatrix_AB.toarray() ) |
Output:

Example 3. Multiply csc and csr matrices
We create two sparse matrices, one of compressed sparse column format and other of compressed sparse row format. Multiply them using multiply() method.
Python3
# Python program to multpliply # csc and csr matrices using multiply() # Import required librariesimport numpy as npfrom scipy.sparse import csc_matrix # Create csc matrixrow_A = np.array([0, 0, 1, 2 ])col_A = np.array([0, 1, 0, 1])data_A = np.array([4, 3, 8, 9]) cscMatrix = csc_matrix((data_A, (row_A, col_A)), shape = (3, 3)) # print csc matrixprint("csc matrix: \n", cscMatrix.toarray()) # Create csr matrix row_B = np.array([0, 1, 1, 2 ])col_B = np.array([0, 0, 1, 0])data_B = np.array([7, 2, 5, 1]) csrMatrix_B = csc_matrix((data_B, (row_B, col_B)), shape = (3, 3)) # print csr matrixprint("csr matrix:\n", csrMatrix_B.toarray()) # Multiply csc matrix with csr matrixsparseMatrix = cscMatrix_A.multiply(csrMatrix_B) # print resultant matrixprint("Product csc with csr Matrix:\n", sparseMatrix.toarray() ) # Multiply csr matrix with csc matrixsparseMatrix = csrMatrix_A.multiply(cscMatrix_B) # print resultant matrixprint("Product csr with csc Matrix:\n", sparseMatrix.toarray() ) |
Output:



