您当前的位置:首页 > 计算机 > 编程开发 > Python

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

时间:12-25来源:作者:点击数:

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

Row-based linked list sparse matrix

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上截图过来了)

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门