# Python稀疏矩阵运算库scipy.sparse用法精要

1、稀疏矩阵的常见存储形式

• 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])

2、不同存储形式的区别

>>> from scipy import sparse

>>> sparse.bsr_matrix([[1,0,0,0,0],[0,1,0,0,1]])

<2x5 sparse matrix of type '<class 'numpy.int32'>'

with 3 stored elements (blocksize = 1x1) in Block Sparse Row format>

>>> sparse.coo_matrix([[1,0,0,0,0],[0,1,0,0,1]])

<2x5 sparse matrix of type '<class 'numpy.int32'>'

with 3 stored elements in COOrdinate format>

>>> sparse.csc_matrix([[1,0,0,0,0],[0,1,0,0,1]])

<2x5 sparse matrix of type '<class 'numpy.int32'>'

with 3 stored elements in Compressed Sparse Column format>

>>> sparse.csr_matrix([[1,0,0,0,0],[0,1,0,0,1]])

<2x5 sparse matrix of type '<class 'numpy.int32'>'

with 3 stored elements in Compressed Sparse Row format>

>>> sparse.dia_matrix([[1,0,0,0,0],[0,1,0,0,1]])

<2x5 sparse matrix of type '<class 'numpy.int32'>'

with 4 stored elements (2 diagonals) in DIAgonal format>

>>> sparse.dok_matrix([[1,0,0,0,0],[0,1,0,0,1]])

<2x5 sparse matrix of type '<class 'numpy.int32'>'

with 3 stored elements in Dictionary Of Keys format>

>>> sparse.lil_matrix([[1,0,0,0,0],[0,1,0,0,1]])

<2x5 sparse matrix of type '<class 'numpy.int32'>'

with 3 stored elements in LInked List format>

3、sparse模块中用于创建稀疏矩阵的函数

• eye(m[, n, k, dtype, format])

Sparse matrix with ones on diagonal

• identity(n[, dtype, format])

Identity matrix in sparse format

• kron(A, B[, format])

kronecker product of sparse matrices A and B

• kronsum(A, B[, format])

kronecker sum of sparse matrices A and B

• diags(diagonals[, offsets, shape, format, dtype])

Construct a sparse matrix from diagonals.

• spdiags(data, diags, m, n[, format])

Return a sparse matrix from diagonals.

• block_diag(mats[, format, dtype])

Build a block diagonal sparse matrix from provided matrices.

• tril(A[, k, format])

Return the lower triangular portion of a matrix in sparse format

• triu(A[, k, format])

Return the upper triangular portion of a matrix in sparse format

• bmat(blocks[, format, dtype])

Build a sparse matrix from sparse sub-blocks

• hstack(blocks[, format, dtype])

Stack sparse matrices horizontally (column wise)

• vstack(blocks[, format, dtype])

Stack sparse matrices vertically (row wise)

• rand(m, n[, density, format, dtype, ...])

Generate a sparse matrix of the given shape and density with uniformly distributed values.

• random(m, n[, density, format, dtype, ...])

Generate a sparse matrix of the given shape and density with randomly distributed values.

4、用法演示（为了不影响排版，直接从我整理的PPT上截图过来了）

651 篇文章83 人订阅

0 条评论

## 相关文章

### 《Redis设计与实现》读书笔记（三十五） ——Redis 二进制位数组及SWAR汉明重量算法

《Redis设计与实现》读书笔记（三十五） ——Redis 二进制位数组及SWAR汉明重量算法 （原创内容，转载请注明来源，谢谢） 一、基本概念 redis提供...

41040

10410

31840

11730

27270

18120

32050

16350

51970

18280