Thursday, December 26, 2024
Google search engine
HomeLanguagesSciPy – Sparse Matrix Multiplication

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

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 libraries
import numpy as np
from scipy.sparse import csc_matrix
  
# Create first csc matrix A
row_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 matrix
print("first csc matrix: \n"
      cscMatrix_A.toarray())
  
# Create second csc matrix B
row_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 matrix
print("second csc matrix:\n", cscMatrix_B.toarray())
  
# Multiply these matrices
sparseMatrix_AB = cscMatrix_A.multiply(cscMatrix_B)
  
# print resultant matrix
print("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 libraries
import numpy as np
from scipy.sparse import csr_matrix
  
# Create first csr matrix A
row_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 matrix
print("first csr matrix: \n", csrMatrix_A.toarray())
  
# Create second csr matrix B
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 = csr_matrix((data_B, (row_B, col_B)),
                        shape = (3, 3))
  
# print second scr matrix
print("second csr matrix:\n", csrMatrix_B.toarray())
  
# Multiply these matrices
sparseMatrix_AB = csrMatrix_A.multiply(csrMatrix_B)
  
# print resultant matrix
print("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 libraries
import numpy as np
from scipy.sparse import csc_matrix
  
# Create csc matrix
row_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 matrix
print("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 matrix
print("csr matrix:\n", csrMatrix_B.toarray())
  
# Multiply csc matrix with csr matrix
sparseMatrix = cscMatrix_A.multiply(csrMatrix_B)
  
# print resultant matrix
print("Product csc with csr Matrix:\n",
      sparseMatrix.toarray() )
  
# Multiply csr matrix with csc matrix
sparseMatrix = csrMatrix_A.multiply(cscMatrix_B)
  
# print resultant matrix
print("Product csr with csc Matrix:\n",
      sparseMatrix.toarray() )


Output:

RELATED ARTICLES

Most Popular

Recent Comments