csr_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.

我们主要看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()

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

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 0 2]
 [0 0 3]
 [4 5 6]
 [0 0 0]]

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

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

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

得到的输出为

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

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

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值