ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

稀疏矩阵

2021-09-28 11:33:48  阅读:246  来源: 互联网

标签:matrix dtype 矩阵 稀疏 shape sparse


目录

稀疏矩阵

插播一期稀疏矩阵。

为什么稀疏矩阵

在实际应用中,矩阵大多时候都是稀疏的(例如大图的邻接矩阵),稀疏矩阵能减少存储空间,加快计算速度。

常用稀疏矩阵

1. coo:Coordinate matrix

采用三个数组,row,col,data,分别表示 行坐标,列坐标,和该坐标系下对应的值。下面的例子是用scipy.sparse创建coo稀疏矩阵。

>>> from scipy.sparse import coo_matrix
>>> row  = np.array([0, 3, 1, 0])
>>> col  = np.array([0, 3, 1, 2])
>>> data = np.array([4, 5, 7, 9])
>>> coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
array([[4, 0, 9, 0],
       [0, 7, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 5]])

优点

  • 方便稀疏格式之间的快速转换;

  • 允许重复条目(参见示例);

  • 与 CSR/CSC 格式之间的快速转换;

缺点

  • 不支持数学运算;
  • 不支持切片(slice)。
2. csr和csc:Compressed Sparse Row/Column matrix

分别代表按行和按列的压缩方式。下面只介绍csr,csc和csr类似。

采用三个数组,data,indices,indptr,分别表示 数值,列号,和偏移量。对应的稠密矩阵的第\(i\) 行的数据表示为(python):

for i in range(len(indptr)-1):
    for j in range(indptr[i],indptr[i+1]):
		matrix[i][indices[j]] = data[j]

下面用scipy.sparse创建csr稀疏矩阵的例子:

>>> import numpy as np
>>> from scipy.sparse import csr_matrix
>>> 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]])

优点

  • 高效算术运算 CSR + CSR、CSR * CSR 等;

  • 高效的行切片(row slice);

  • 快速矩阵向量乘积 ;

缺点

  • 缓慢的列切片(column slice)操作(考虑使用CSC);
  • 稀疏结构的改变代价高昂(考虑 LIL 或 DOK)。

All conversions among the CSR, CSC, and COO formats are efficient, linear-time operations.

python中的scipy.sparse

支持的稀疏格式
矩阵格式 描述
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 list of lists sparse matrix
spmatrix([maxprint]) This class provides a base class for all sparse matrices.
常用api
API 描述
eye(m[, n, k, dtype, format]) Sparse matrix with ones on diagonal
identity(n[, dtype, format]) Identity matrix in sparse format
hstack(blocks[, format, dtype]) Stack sparse matrices horizontally (column wise)
vstack(blocks[, format, dtype]) Stack sparse matrices vertically (row wise)
random(m, n[, density, format, dtype, …]) Generate a sparse matrix of the given shape and density with randomly distributed values.
save_npz(file, matrix[, compressed]) Save a sparse matrix to a file using .npz format.
load_npz(file) Load a sparse matrix from a file using .npz format.
multiply(other) Point-wise multiplication by another matrix
power(n[, dtype]) This function performs element-wise power.
dot(other) Ordinary dot product

标签:matrix,dtype,矩阵,稀疏,shape,sparse
来源: https://www.cnblogs.com/clearhanhui/p/15347044.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有