前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python稀疏矩阵运算库scipy.sparse用法精要

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

作者头像
Python小屋屋主
发布2018-04-16 14:52:20
8K0
发布2018-04-16 14:52:20
举报
文章被收录于专栏:Python小屋

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-05-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python小屋 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档