目錄
稀疏矩陣
插播一期稀疏矩陣。
為什么稀疏矩陣
在實際應用中,矩陣大多時候都是稀疏的(例如大圖的鄰接矩陣),稀疏矩陣能減少存儲空間,加快計算速度。
常用稀疏矩陣
1. coo:Coordinate matrix
采用三個數組,row,col,data,分別表示 行坐標,列坐標,和該坐標系下對應的值。下面的例子是用scipy.sparse創建coo稀疏矩陣。
>>> from scipy.sparse import coo_matrix
>>> row = np.array([0, 3, 1, 0])
>>> col = np.array([0, 3, 1, 2])
>>> data = np.array([4, 5, 7, 9])
>>> coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
array([[4, 0, 9, 0],
[0, 7, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 5]])
優點:
-
方便稀疏格式之間的快速轉換;
-
允許重復條目(參見示例);
-
與 CSR/CSC 格式之間的快速轉換;
缺點 :
- 不支持數學運算;
- 不支持切片(slice)。
2. csr和csc:Compressed Sparse Row/Column matrix
分別代表按行和按列的壓縮方式。下面只介紹csr,csc和csr類似。
采用三個數組,data,indices,indptr,分別表示 數值,列號,和偏移量。對應的稠密矩陣的第\(i\) 行的數據表示為(python):
for i in range(len(indptr)-1):
for j in range(indptr[i],indptr[i+1]):
matrix[i][indices[j]] = data[j]
下面用scipy.sparse創建csr稀疏矩陣的例子:
>>> import numpy as np
>>> from scipy.sparse import csr_matrix
>>> indptr = np.array([0, 2, 3, 6])
>>> indices = np.array([0, 2, 2, 0, 1, 2])
>>> data = np.array([1, 2, 3, 4, 5, 6])
>>> csr_matrix((data, indices, indptr), shape=(3, 3)).toarray()
array([[1, 0, 2],
[0, 0, 3],
[4, 5, 6]])
優點:
-
高效算術運算 CSR + CSR、CSR * CSR 等;
-
高效的行切片(row slice);
-
快速矩陣向量乘積 ;
缺點:
- 緩慢的列切片(column slice)操作(考慮使用CSC);
- 稀疏結構的改變代價高昂(考慮 LIL 或 DOK)。
All conversions among the CSR, CSC, and COO formats are efficient, linear-time operations.
python中的scipy.sparse
支持的稀疏格式
| 矩陣格式 | 描述 |
|---|---|
bsr_matrix(arg1[, shape, dtype, copy, blocksize]) |
Block Sparse Row matrix |
coo_matrix(arg1[, shape, dtype, copy]) |
A sparse matrix in COOrdinate format. |
csc_matrix(arg1[, shape, dtype, copy]) |
Compressed Sparse Column matrix |
csr_matrix(arg1[, shape, dtype, copy]) |
Compressed Sparse Row matrix |
dia_matrix(arg1[, shape, dtype, copy]) |
Sparse matrix with DIAgonal storage |
dok_matrix(arg1[, shape, dtype, copy]) |
Dictionary Of Keys based sparse matrix. |
lil_matrix(arg1[, shape, dtype, copy]) |
Row-based list of lists sparse matrix |
spmatrix([maxprint]) |
This class provides a base class for all sparse matrices. |
常用api
| API | 描述 |
|---|---|
eye(m[, n, k, dtype, format]) |
Sparse matrix with ones on diagonal |
identity(n[, dtype, format]) |
Identity matrix in sparse format |
hstack(blocks[, format, dtype]) |
Stack sparse matrices horizontally (column wise) |
vstack(blocks[, format, dtype]) |
Stack sparse matrices vertically (row wise) |
random(m, n[, density, format, dtype, …]) |
Generate a sparse matrix of the given shape and density with randomly distributed values. |
save_npz(file, matrix[, compressed]) |
Save a sparse matrix to a file using .npz format. |
load_npz(file) |
Load a sparse matrix from a file using .npz format. |
multiply(other) |
Point-wise multiplication by another matrix |
power(n[, dtype]) |
This function performs element-wise power. |
dot(other) |
Ordinary dot product |
