scipy csr_matrix和csc_matrix函数详解
2017-07-03 16:16
399 查看
在用Python进行科学运算时,常常需要把一个稀疏的np.array压缩,这时候就用到scipy库中的sparse.csr_matrix(csr:Compressed
Sparse Row marix) 和sparse.csc_matric(csc:Compressed Sparse Column marix)
官方API介绍(省略前几种容易理解的了)
csr_matrix((data, indices, indptr), [shape=(M, N)])
is the standard CSR representation where the column indices for row i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding values are stored in data[indptr[i]:indptr[i+1]]. If the shape parameter is not supplied, the matrix dimensions are inferred
from the index arrays.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
官方API介绍(省略前几种容易理解的了)
csc_matrix((data, indices, indptr), [shape=(M, N)])
is the standard CSC representation where the row indices for column i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding values are stored in data[indptr[i]:indptr[i+1]]. If the shape parameter is not supplied, the matrix dimensions are inferred
from the index arrays.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
顶
0
踩
Sparse Row marix) 和sparse.csc_matric(csc:Compressed Sparse Column marix)
scipy.sparse.csr_matrix
官方API介绍(省略前几种容易理解的了) csr_matrix((data, indices, indptr), [shape=(M, N)])
is the standard CSR representation where the column indices for row i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding values are stored in data[indptr[i]:indptr[i+1]]. If the shape parameter is not supplied, the matrix dimensions are inferred
from the index arrays.
# 示例解读 >>> 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]]) # 按row行来压缩 # 对于第i行,列是indices[indptr[i]:indptr[i+1]] 数据是data[indptr[i]:indptr[i+1]] # 在本例中 # 第0行,有非0的数据列是indptr[indices[0]:indices[1]] = indptr[0,2] = [0,2] # 数据是data[indptr[0]:indptr[1]] = data[0,2] = [1,2],所以在第0行第0列是1,第2列是2 # 第1行,有非0的数据列是indptr[indices[1]:indices[2]] = indptr[2,3] = [2] # 数据是data[indptr[1]:indptr[2] = data[2,3] = [3],所以在第1行第2列是3 # 第2行,有非0的数据列是indptr[indices[2]:indices[3]] = indptr[3,6] = [0,1,2] # 数据是data[indptr[2]:indptr[3]] = data[3,6] = [4,5,6],所以在第2行第0列是4,第1列是5,第2列是61
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
scipy.sparse.csc_matrix
官方API介绍(省略前几种容易理解的了) csc_matrix((data, indices, indptr), [shape=(M, N)])
is the standard CSC representation where the row indices for column i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding values are stored in data[indptr[i]:indptr[i+1]]. If the shape parameter is not supplied, the matrix dimensions are inferred
from the index arrays.
# 示例解读 >>> 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]) >>> csc_matrix((data, indices, indptr), shape=(3, 3)).toarray() array([[1, 0, 4], [0, 0, 5], [2, 3, 6]]) # 按col列来压缩 # 对于第i列,行是indices[indptr[i]:indptr[i+1]] 数据是data[indptr[i]:indptr[i+1]] # 在本例中 # 第0列,有非0的数据行是indptr[indices[0]:indices[1]] = indptr[0,2] = [0,2] # 数据是data[indptr[0]:indptr[1]] = data[0,2] = [1,2],所以在第0列第0行是1,第2行是2 # 第1行,有非0的数据行是indptr[indices[1]:indices[2]] = indptr[2,3] = [2] # 数据是data[indptr[1]:indptr[2] = data[2,3] = [3],所以在第1列第2行是3 # 第2行,有非0的数据行是indptr[indices[2]:indices[3]] = indptr[3,6] = [0,1,2] # 数据是data[indptr[2]:indptr[3]] = data[3,6] = [4,5,6],所以在第2列第0行是4,第1行是5,第2行是61
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
顶
0
踩
相关文章推荐
- scipy.sparse中csc_martrix和csr_matrix两个稀疏矩阵的区别
- 利用scipy.sparse.csr_matrix构建term-document matrix
- 利用scipy.sparse.csr_matrix构建term-document矩阵
- scipy csc csr到底是什么东西
- 利用scipy.sparse.csr_matrix构建term-document矩阵
- 利用scipy.sparse.csr_matrix构建term-document矩阵
- OpenGL的glPushMatrix()和glPopMatrix()矩阵栈顶操作函数详解
- Save / load scipy array,sparse csr_matrix
- 十分钟理解Scipy.csc_matrix和coo_matrix
- XML系列函数详解
- Oracle函数详解
- 详解Variant 的相关函数
- Boost String Algorithms Library 函数详解三 (find)
- PL/SQL单行函数和组函数详解
- XML系列函数详解
- Boost String Algorithms Library 函数详解四 (Erase/Replace)
- Oracle中PL/SQL单行函数和组函数详解
- ASP的函数详解
- Boost String Algorithms Library 函数详解二 (predicate)
- PL/SQL单行函数和组函数详解