Row Division in Scipy Sparse Matrix

后端 未结 3 1219
时光取名叫无心
时光取名叫无心 2020-12-12 00:11

I want to divide a sparse matrix\'s rows by scalars given in an array.

For example, I have a csr_matrix C :

C = [[2,4,6], [5,         


        
3条回答
  •  伪装坚强ぢ
    2020-12-12 00:29

    Approach #1

    Here's a sparse matrix solution using manual replication with indexing -

    from scipy.sparse import csr_matrix
    
    r,c = C.nonzero()
    rD_sp = csr_matrix(((1.0/D)[r], (r,c)), shape=(C.shape))
    out = C.multiply(rD_sp)
    

    The output is a sparse matrix as well as opposed to the output from C / D[:,None] that creates a full matrix. As such, the proposed approach saves on memory.

    Possible performance boost with replication using np.repeat instead of indexing -

    val = np.repeat(1.0/D, C.getnnz(axis=1))
    rD_sp = csr_matrix((val, (r,c)), shape=(C.shape))
    

    Approach #2

    Another approach could involve data method of the sparse matrix that gives us a flattened view into the sparse matrix for in-place results and also avoid the use of nonzero, like so -

    val = np.repeat(D, C.getnnz(axis=1))
    C.data /= val
    

提交回复
热议问题