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

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dongfuguo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值