SciPy - 稀疏矩阵
SciPy 中的稀疏矩阵
In SciPy 中,sparse matrix 是一种大多数元素为零的矩阵。与像稠密矩阵那样存储所有元素不同,我们可以使用稀疏矩阵,只存储非零元素及其位置,即索引。
这显著减少了内存使用,并在处理大型矩阵时提高了计算效率,尤其是在科学、工程或机器学习问题中,这些问题中的矩阵往往非常大且稀疏。
SciPy 提供了一个名为 scipy.sparse 的模块,专为高效处理稀疏矩阵而设计。SciPy 中的稀疏矩阵模块支持多种格式和操作,能够高效存储和操作大型稀疏数据。
为什么使用稀疏矩阵?
如上所述,稀疏矩阵 是包含大量零元素的矩阵。这在处理大多数元素为零的大型数据集时,能显著节省内存和计算资源。因此,使用稀疏矩阵的主要原因如下 −
- 内存效率: 稀疏矩阵仅存储非零元素,从而大幅减少内存使用,尤其适用于包含大量零的大型矩阵。
- 更快的计算: 稀疏矩阵算法跳过零元素,从而加速矩阵运算,如乘法和求解线性系统。
- 可扩展性: 它们允许更高效地处理大型数据集,如图数据或文本数据(如词-文档矩阵)。
- 压缩: 稀疏矩阵非常适合通过仅表示关键非零元素来压缩大型数据集。
- 优化算法: 许多算法,特别是在线性代数和优化领域,已针对稀疏矩阵进行了优化,从而提升性能。
- 更好的 I/O: 存储稀疏矩阵可减少磁盘空间并加速数据传输,尤其适用于大规模系统。
稀疏矩阵格式
下表列出了 SciPy 提供的不同 Sparse Matrix Formats −
| 序号 | 稀疏矩阵格式 | 函数 & 描述 |
|---|---|---|
| 1 | Compressed Sparse Row (CSR) format。 | scipy.sparse.csr_matrix() 优化用于快速行切片和矩阵-向量乘法。将非零元素存储在 1D array 中,同时存储行索引指针和列索引。 |
| 2 | Compressed Sparse Column (CSC) format。 | scipy.sparse.csc_matrix() 优化用于快速列切片和矩阵-向量乘法。与 CSR 类似,但存储列索引和行指针,而不是行索引。 |
| 3 | Coordinate List (COO) format。 | scipy.sparse.coo_matrix() 将矩阵存储为 (row, column) 索引列表及其对应的值。适用于逐步构建稀疏矩阵。 |
| 4 | Diagonal (DIA) format。 | scipy.sparse.dia_matrix() 专为具有少量非零对角线的矩阵设计。对于对角矩阵高效,以紧凑格式存储对角线。 |
| 5 | List of Lists (LIL) format。 | scipy.sparse.lil_matrix() 将每行存储为列索引和非零值的列表。适用于逐步构建稀疏矩阵和修改单个元素。 |
| 6 | Block Sparse Row (BSR) format。 | scipy.sparse.bsr_matrix() 用于块稀疏矩阵,其中矩阵被划分为子块。适用于块级矩阵操作高效,例如块对角矩阵。 |
| 7 | Dictionary Of Keys (DOK) format。 | scipy.sparse.dok_matrix() 以键值对形式存储数据。键作为每个值的标识符,支持高效访问、修改和检索数据。在 Python 中,通常使用 dict 数据结构来表示字典。 |
稀疏格式之间的转换
SciPy 中不同稀疏矩阵格式之间的转换非常简单,并且通常是必要的,因为每种格式都针对特定操作进行了优化。SciPy 提供了如 .tocsr()、.tocsc()、.tocoo()、.tolil()、.todok() 和 .todia() 等方法来执行上述不同稀疏矩阵转换。
示例
以下示例展示了如何在不同稀疏格式之间进行转换。在此示例中,我们有一个 COO (Coordinate) 格式的稀疏矩阵,并希望使用 scipy.sparse 模块将其转换为其他格式 −
from scipy.sparse import coo_matrix
# 定义一个 COO 稀疏矩阵
row = [0, 1, 2]
col = [0, 2, 0]
data = [1, 3, 4]
coo = coo_matrix((data, (row, col)), shape=(3, 3))
print("COO Matrix:")
print(coo)
# 将 COO 转换为 CSR (Compressed Sparse Row)
csr = coo.tocsr()
print("\nConverted to CSR Format:")
print(csr)
# 将 COO 转换为 CSC (Compressed Sparse Column)
csc = coo.tocsc()
print("\nConverted to CSC Format:")
print(csc)
# 将 COO 转换为 LIL (List of Lists)
lil = coo.tolil()
print("\nConverted to LIL Format:")
print(lil)
# 将 COO 转换为 DOK (Dictionary of Keys)
dok = coo.todok()
print("\nConverted to DOK Format:")
print(dok)
以下是 2D 矩阵的 L2 范数的输出 −
COO Matrix:
<COOrdinate sparse matrix of dtype 'int64'
with 3 stored elements and shape (3, 3)>
Coords Values
(0, 0) 1
(1, 2) 3
(2, 0) 4
Converted to CSR Format:
<Compressed Sparse Row sparse matrix of dtype 'int64'
with 3 stored elements and shape (3, 3)>
Coords Values
(0, 0) 1
(1, 2) 3
(2, 0) 4
Converted to CSC Format:
<Compressed Sparse Column sparse matrix of dtype 'int64'
with 3 stored elements and shape (3, 3)>
Coords Values
(0, 0) 1
(2, 0) 4
(1, 2) 3
Converted to LIL Format:
<List of Lists sparse matrix of dtype 'int64'
with 3 stored elements and shape (3, 3)>
Coords Values
(0, 0) 1
(1, 2) 3
(2, 0) 4
Converted to DOK Format:
<Dictionary Of Keys sparse matrix of dtype 'int64'
with 3 stored elements and shape (3, 3)>
Coords Values
(0, 0) 1
(1, 2) 3
(2, 0) 4