如何在Python中创建稀疏矩阵
如果矩阵的大部分元素的值都为 0 ,则称为稀疏矩阵。使用稀疏矩阵而不是简单矩阵的两个主要好处是:
- 存储:非零元素比零少,因此可以使用更少的内存仅存储这些元素。
- 计算时间:可以通过逻辑设计一个只遍历非零元素的数据结构来节省计算时间。
稀疏矩阵通常是 用于应用机器学习,例如包含将类别映射到计数的数据编码的数据,以及机器学习的整个子领域,例如自然语言处理 (NLP)。
例子:
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0
用二维数组表示稀疏矩阵会导致大量内存的浪费,因为矩阵中的零在大多数情况下都没有用。因此,我们只存储非零元素,而不是用非零元素存储零。这意味着使用三元组(行、列、值)存储非零元素。
在Python中创建稀疏矩阵
Python 的SciPy 提供使用多种数据结构创建稀疏矩阵的工具,以及将密集矩阵转换为稀疏矩阵的工具。函数csr_matrix()用于创建压缩稀疏行格式的稀疏矩阵,而csc_matrix()用于创建压缩稀疏列格式的稀疏矩阵。
# 使用csr_matrix()
Syntax:
scipy.sparse.csr_matrix(shape=None, dtype=None)
Parameters:
shape: Get shape of a matrix
dtype: Data type of the matrix
示例 1:
Python
# Python program to create
# sparse matrix using csr_matrix()
# Import required package
import numpy as np
from scipy.sparse import csr_matrix
# Creating a 3 * 4 sparse matrix
sparseMatrix = csr_matrix((3, 4),
dtype = np.int8).toarray()
# Print the sparse matrix
print(sparseMatrix)
Python
# Python program to create
# sparse matrix using csr_matrix()
# Import required package
import numpy as np
from scipy.sparse import csr_matrix
row = np.array([0, 0, 1, 1, 2, 1])
col = np.array([0, 1, 2, 0, 2, 2])
# taking data
data = np.array([1, 4, 5, 8, 9, 6])
# creating sparse matrix
sparseMatrix = csr_matrix((data, (row, col)),
shape = (3, 3)).toarray()
# print the sparse matrix
print(sparseMatrix)
Python
# Python program to create
# sparse matrix using csc_matrix()
# Import required package
import numpy as np
from scipy.sparse import csc_matrix
# Creating a 3 * 4 sparse matrix
sparseMatrix = csc_matrix((3, 4),
dtype = np.int8).toarray()
# Print the sparse matrix
print(sparseMatrix)
Python
# Python program to create
# sparse matrix using csc_matrix()
# Import required package
import numpy as np
from scipy.sparse import csc_matrix
row = np.array([0, 0, 1, 1, 2, 1])
col = np.array([0, 1, 2, 0, 2, 2])
# taking data
data = np.array([1, 4, 5, 8, 9, 6])
# creating sparse matrix
sparseMatrix = csc_matrix((data, (row, col)),
shape = (3, 3)).toarray()
# print the sparse matrix
print(sparseMatrix)
输出:
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
示例 2:
Python
# Python program to create
# sparse matrix using csr_matrix()
# Import required package
import numpy as np
from scipy.sparse import csr_matrix
row = np.array([0, 0, 1, 1, 2, 1])
col = np.array([0, 1, 2, 0, 2, 2])
# taking data
data = np.array([1, 4, 5, 8, 9, 6])
# creating sparse matrix
sparseMatrix = csr_matrix((data, (row, col)),
shape = (3, 3)).toarray()
# print the sparse matrix
print(sparseMatrix)
输出:
[[ 1 4 0]
[ 8 0 11]
[ 0 0 9]]
# 使用csc_matrix()
Syntax:
scipy.sparse.csc_matrix(shape=None, dtype=None)
Parameters:
shape: Get shape of a matrix
dtype: Data type of the matrix
示例 1:
Python
# Python program to create
# sparse matrix using csc_matrix()
# Import required package
import numpy as np
from scipy.sparse import csc_matrix
# Creating a 3 * 4 sparse matrix
sparseMatrix = csc_matrix((3, 4),
dtype = np.int8).toarray()
# Print the sparse matrix
print(sparseMatrix)
输出:
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
示例 2:
Python
# Python program to create
# sparse matrix using csc_matrix()
# Import required package
import numpy as np
from scipy.sparse import csc_matrix
row = np.array([0, 0, 1, 1, 2, 1])
col = np.array([0, 1, 2, 0, 2, 2])
# taking data
data = np.array([1, 4, 5, 8, 9, 6])
# creating sparse matrix
sparseMatrix = csc_matrix((data, (row, col)),
shape = (3, 3)).toarray()
# print the sparse matrix
print(sparseMatrix)
输出:
[[ 1 4 0]
[ 8 0 11]
[ 0 0 9]]