Compressed Sparse Row Format (CSR)

Compressed Sparse Row Format (CSR)

    • three NumPy arrays: indices, indptr, data
      • indices is array of column indices
      • data is array of corresponding nonzero values
      • indptr points to row starts in indices and data
      • length is n_row + 1, last item = number of values = length of both indices and data
      • nonzero values of the i-th row are data[indptr[i]:indptr[i+1]] with column indices indices[indptr[i]:indptr[i+1]]
      • item (i, j) can be accessed as data[indptr[i]+k], where k is position of j in indices[indptr[i]:indptr[i+1]]
    • subclass of _cs_matrix (common CSR/CSC functionality)
      • subclass of _data_matrix (sparse matrix classes with .data attribute)
  • fast matrix vector products and other arithmetics (sparsetools)
  • constructor accepts:
    • dense matrix (array)
    • sparse matrix
    • shape tuple (create empty matrix)
    • (data, ij) tuple
    • (data, indices, indptr) tuple
  • efficient row slicing, row-oriented operations
  • slow column slicing, expensive changes to the sparsity structure
  • use:
    • actual computations (most linear solvers support this format)

Examples

  • create empty CSR matrix:

    >>> mtx = sparse.csr_matrix((3, 4), dtype=np.int8)
    
           
           
    >>> mtx.todense()
    matrix([[0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0]], dtype=int8)
  • create using (data, ij) tuple:

    >>> row = np.array([0, 0, 1, 2, 2, 2])
    
           
           
    >>> col = np.array([0, 2, 2, 0, 1, 2])
    >>> data = np.array([1, 2, 3, 4, 5, 6])
    >>> mtx = sparse.csr_matrix((data, (row, col)), shape=(3, 3))
    >>> mtx
    <3x3 sparse matrix of type '<... 'numpy.int64'>'
    with 6 stored elements in Compressed Sparse Row format>
    >>> mtx.todense()
    matrix([[1, 0, 2],
    [0, 0, 3],
    [4, 5, 6]]...)
    >>> mtx.data
    array([1, 2, 3, 4, 5, 6]...)
    >>> mtx.indices
    array([0, 2, 2, 0, 1, 2], dtype=int32)
    >>> mtx.indptr
    array([0, 2, 3, 6], dtype=int32)
  • create using (data, indices, indptr) tuple:

    >>> 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])
    >>> mtx = sparse.csr_matrix((data, indices, indptr), shape=(3, 3))
    >>> mtx.todense()
    matrix([[1, 0, 2],
    [0, 0, 3],
    [4, 5, 6]])

See https://scipy-lectures.org/advanced/scipy_sparse/csr_matrix.html

https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.sparse.csr_matrix.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值