SciPy - 稀疏矩阵乘法
稀疏矩阵是那些元素最多为零的矩阵。 scipy.sparse是用于数值数据的 SciPy 二维稀疏矩阵包。它为我们提供了不同的类来创建稀疏矩阵。 csc_matrix和csr_matrix是两个这样的类。 csc_matrix()用于创建压缩稀疏列矩阵,而csr_matrix()用于创建压缩稀疏行矩阵。
注意:有关如何创建稀疏矩阵的更多信息,请访问如何在Python创建稀疏矩阵
我们使用csc_matrix和csr_matrix类中提供的 multiply() 方法将两个稀疏矩阵相乘。我们可以将两个相同格式的矩阵(两个矩阵都是 csc 或 csr 格式)和不同格式的矩阵(一个矩阵是 csc,另一个是 csr 格式)相乘。
示例 1:将两个 csc 矩阵相乘
我们使用 csc_matrix() 创建两个压缩稀疏列格式的稀疏矩阵,并使用 multiply() 方法将它们相乘。
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())
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() )
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() )
输出:
示例 2. 将两个 csr 矩阵相乘
我们使用 csr_matrix() 创建两个压缩稀疏行格式的稀疏矩阵,并使用 multiply() 方法将它们相乘。
蟒蛇3
# 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() )
输出:
示例 3. 将 csc 和 csr 矩阵相乘
我们创建了两个稀疏矩阵,一个是压缩稀疏列格式,另一个是压缩稀疏行格式。使用 multiply() 方法将它们相乘。
蟒蛇3
# 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() )
输出: