当前位置:   article > 正文

csr_matrix详细解读_python crs matrix

python crs matrix

1.csr_matrix格式

看代码过程汇总,发现有用到稀疏矩阵。经过仔细阅读发现以前对csr_matrix的理解不是特别深入,特此再对csr_matrix进行分析解读。

python中csr_matrix的说明注释如下

class csr_matrix(_cs_matrix):
    """
    Compressed Sparse Row matrix

    This can be instantiated in several ways:
        csr_matrix(D)
            with a dense matrix or rank-2 ndarray D

        csr_matrix(S)
            with another sparse matrix S (equivalent to S.tocsr())

        csr_matrix((M, N), [dtype])
            to construct an empty matrix with shape (M, N)
            dtype is optional, defaulting to dtype='d'.

        csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
            where ``data``, ``row_ind`` and ``col_ind`` satisfy the
            relationship ``a[row_ind[k], col_ind[k]] = data[k]``.

        csr_matrix((data, indices, indptr), [shape=(M, N)])
            is the standard CSR representation where the column indices for
            row i are stored in ``indices[indptr[i]:indptr[i+1]]`` and their
            corresponding values are stored in ``data[indptr[i]:indptr[i+1]]``.
            If the shape parameter is not supplied, the matrix dimensions
            are inferred from the index arrays.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

我们主要看csr_matrix((data, indices, indptr), [shape=(M, N)])这种方式。

2.实例分析

结合源码中给的实例我们来进行解读。

def func():
    from scipy import sparse
    import numpy as np
    data = np.array([1, 2, 3, 4, 5, 6])
    indices = np.array([0, 2, 2, 0, 1, 2])
    indptr = np.array([0, 2, 3, 6])
    coo = sparse.csr_matrix((data, indices, indptr), shape=(3, 3)).toarray()
    print(coo)

if __name__ == '__main__':
    func()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

data数组表示存储的最终数据。
因为是csc的存储方式,indptr表示按行来”计算"。其中每行的非零数据为data[indptr[i]:indptr[i+1]]。
而对应的非零数值的列索引存储在indices中,为indices[indptr[i]:indptr[i+1]]。

以第一行为例,第一行的非零数据为data[indptr[0]:indptr[1]],即data[0:2], 包含有两个元素,分别为1,2。而这两个元素的列索引为indices[indptr[0]:indptr[1]],分别为0,2。即第一行为[1, 0, 2]。
第二、三行,则以此类推。

最后构造的矩阵为:

[[1 0 2]
 [0 0 3]
 [4 5 6]]
  • 1
  • 2
  • 3

3.添加全零行或者列

实验过程中,发现添加了全0行或者全0列的方法。

如果我们想添加全0行

def func():
    from scipy import sparse
    import numpy as np
    data = np.array([1, 2, 3, 4, 5, 6])
    indices = np.array([0, 2, 2, 0, 1, 2])
    indptr = np.array([0, 2, 3, 6, 6])
    coo = sparse.csr_matrix((data, indices, indptr), shape=(4, 3)).toarray()
    print(coo)

if __name__ == '__main__':
    func()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

得到的输出为

[[1 0 2]
 [0 0 3]
 [4 5 6]
 [0 0 0]]
  • 1
  • 2
  • 3
  • 4

此时indptr虽然有5个数字表示4行,但第4行非0元素为6-6=0,说明第三行为全0行。

如果想得到全0列,只需要将coo做如下修改

coo = sparse.csr_matrix((data, indices, indptr), shape=(3, 4)).toarray()
  • 1

得到的输出为

[[1 0 2 0]
 [0 0 3 0]
 [4 5 6 0]]
  • 1
  • 2
  • 3

因为所有非0列的index最大值为2,而指定shape中列为4,说明第四列为全0列。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/989475
推荐阅读
相关标签
  

闽ICP备14008679号