zoukankan      html  css  js  c++  java
  • 不同稀疏矩阵的优缺点和使用经验

    sparse matrix稀疏矩阵不同的存储形式在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 linked list sparse matrix

    scipy不同稀疏矩阵的介绍和优缺点

    scipy.sparse库中提供了多种表示稀疏矩阵的格式,每种格式都有不同的用处。同时稀疏矩阵可以支持加、减、乘、除和幂等算术操作。Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division,and matrix power.

    分块压缩稀疏行格式(BSR)bsr_matrix(arg1, shape=None, dtype=None, copy=False, blocksize=None)Block Sparse Row matrix:

    和压缩稀疏行格式(CSR)很相似,但是BSR更适合于有密集子矩阵的稀疏矩阵,分块矩阵通常出现在向量值有限的离散元中,在这种情景下,比CSR和CSC算术操作更有效。The Block Compressed Row (BSR) format is very similar to the Compressed Sparse Row (CSR) format. BSR is appropriate for sparse matrices with dense sub matrices. Block matrices often arise in vector-valued finite element discretizations. In such cases, BSR is considerably more efficient than CSR and CSC for many sparse arithmetic operations.

    csc_matrix(arg1,shape=None, dtype=None, copy=False)压缩的列稀疏矩阵CSC :

    高效的CSC +CSC, CSC * CSC算术运算;高效的列切片操作。但是矩阵内积操作没有CSR, BSR快;行切片操作慢(相比CSR);稀疏结构的变化代价高(相比LIL 或者 DOK)。

    Advantages of the CSC format
    •efficient arithmetic operations CSC + CSC, CSC * CSC, etc.
    •efficient column slicing
    •fast matrix vector products (CSR, BSR may be faster!)
    Disadvantages of the CSC format
    •slow row slicing operations (consider CSR)
    •changes to the sparsity structure are expensive (consider LIL or DOK)

    csr_matrix(arg1, shape=None, dtype=None, copy=False)Compressed Sparse Row matrix压缩稀疏行格式(CSR):

    高效的CSR + CSR, CSR *CSR算术运算;高效的行切片操作;高效的矩阵内积内积操作。但是列切片操作慢(相比CSC);稀疏结构的变化代价高(相比LIL 或者 DOK)。CSR格式在存储稀疏矩阵时非零元素平均使用的字节数(Bytes per Nonzero Entry)最为稳定(float类型约为8.5,double类型约为12.5)。CSR格式常用于读入数据后进行稀疏矩阵计算。

    Advantages of the CSR format
    •efficient arithmetic operations CSR + CSR, CSR * CSR, etc.
    •efficient row slicing
    •fast matrix vector products
    Disadvantages of the CSR format
    •slow column slicing operations (consider CSC)
    •changes to the sparsity structure are expensive (consider LIL or DOK)

    coo_matrix(arg1,shape=None,dtype=None,copy=False)坐标格式(COO):

    坐标形式的一种稀疏矩阵。采用三个数组row、col和data保存非零元素的信息。这三个数组的长度相同,row保存元素的行,col保存元素的列,data保存元素的值。

    coo_matrix不支持元素的存取和增删,一旦创建之后,除了将之转换成其它格式的矩阵,几乎无法对其做任何操作和矩阵运算。

    Advantages of the COO format
    •facilitates fast conversion among sparse formats
    •permits duplicate entries (see example)
    •very fast conversion to and from CSR/CSC formats

    优点:便利快捷的在不同稀疏格式间转换;允许重复录入,允许重复的元素;从CSRCSC格式转换非常快速。
    Disadvantages of the COO format
    •does not directly support:
    –arithmetic operations
    –slicing
    缺点:不能直接进行科学计算和切片操作

    COO格式常用于从文件中进行稀疏矩阵的读写,如matrix market即采用COO格式。

    最常用的函数:

    tocsc()

    Return a copy of this matrix in Compressed Sparse Column format

    tocsr()

    Return a copy of this matrix in Compressed Sparse Row format

    todense([order, out])

    Return a dense matrix representation of this matrix

    许多稀疏矩阵的数据都是采用这种格式保存在文件中的,例如某个CSV文件中可能有这样三列:“用户ID,商品ID,评价值”。采用numpy.loadtxt或pandas.read_csv将数据读入之后,可以通过coo_matrix快速将其转换成稀疏矩阵:矩阵的每行对应一位用户,每列对应一件商品,而元素值为用户对商品的评价。

    dia_matrix(arg1, shape=None, dtype=None, copy=False)Sparse matrix with DIAgonal storage

    对角存储格式(DIA)和ELL格式在进行稀疏矩阵-矢量乘积(sparse matrix-vector products)时效率最高,所以它们是应用迭代法(如共轭梯度法)解稀疏线性系统最快的格式;DIA格式存储数据的非零元素平均使用的字节数与矩阵类型有较大关系,适合于StructuredMesh结构的稀疏矩阵(float类型约为4.05,double类型约为8.10)。对于Unstructured Mesh以及Random Matrix,DIA格式使用的字节数是CSR格式的十几倍。

    dok_matrix(arg1, shape=None, dtype=None, copy=False)Dictionary Of Keys based sparse matrix.

    dok_matrix从dict继承,它采用字典保存矩阵中不为0的元素:字典的键是一个保存元素(行,列)信息的元组,其对应的值为矩阵中位于(行,列)中的元素值。显然字典格式的稀疏矩阵很适合单个元素的添加、删除和存取操作。通常用来逐渐添加非零元素,然后转换成其它支持快速运算的格式。

    基于字典存储的稀疏矩阵。

    This is an efficient structure for constructing sparse matrices incrementally.Allows for efficient O(1) access of individual elements. Duplicates are not allowed. Can be efficiently converted to a coo_matrix once constructed.

    lil_matrix(arg1, shape=None, dtype=None, copy=False)Row-based linked list sparse matrix
    This is an efficient structure for constructing sparse matrices incrementally.

    基于行连接存储的稀疏矩阵。lil_matrix使用两个列表保存非零元素。data保存每行中的非零元素,rows保存非零元素所在的列。这种格式也很适合逐个添加元素,并且能快速获取行相关的数据。

    Advantages of the LIL format
    •supports flexible slicing
    •changes to the matrix sparsity structure are efficient
    Disadvantages of the LIL format
    •arithmetic operations LIL + LIL are slow (consider CSR or CSC)
    •slow column slicing (consider CSC)
    •slow matrix vector products (consider CSR or CSC)
    Intended Usage
    •LIL is a convenient format for constructing sparse matrices
    •once a matrix has been constructed, convert to CSR or CSC format for fast arithmetic and matrix vector operations
    •consider using the COO format when constructing large matrices

    Note:{dok_matrix和lil_matrix适合逐渐添加元素}

    综合

    2. COO和CSR格式比起DIA和ELL来,更加灵活,易于操作;

    3. ELL的优点是快速,而COO优点是灵活,二者结合后的HYB格式是一种不错的稀疏矩阵表示格式;

    4. 根据Nathan Bell的工作:

    CSR格式在存储稀疏矩阵时非零元素平均使用的字节数(Bytes per Nonzero Entry)最为稳定(float类型约为8.5,double类型约为12.5)

    而DIA格式存储数据的非零元素平均使用的字节数与矩阵类型有较大关系,适合于StructuredMesh结构的稀疏矩阵(float类型约为4.05,double类型约为8.10)

    对于Unstructured Mesh以及Random Matrix,DIA格式使用的字节数是CSR格式的十几倍;

    5. 一些线性代数计算库:COO格式常用于从文件中进行稀疏矩阵的读写,如matrix market即采用COO格式,而CSR格式常用于读入数据后进行稀疏矩阵计算

  • 相关阅读:
    ReentrantLock可重入锁的使用场景(转)
    可重入锁(good)
    AbstractQueuedSynchronizer的介绍和原理分析(转)
    Android DB类,支持MDB,SQLITE,SQLSERVER,支持查询、事务,对象直接插入和更新操作等
    sqlite 时间函数及时间处理
    android query 模糊查询
    Android 时间轴
    Android fastjson
    Android SQLite案例
    Android 查看和管理sqlite数据库
  • 原文地址:https://www.cnblogs.com/YangZnufe/p/8413374.html
Copyright © 2011-2022 走看看