Row Division in Scipy Sparse Matrix

谁说胖子不能爱 提交于 2019-11-29 14:52:21

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

one line code: result = [[C[i][j]/D[i] for j in range(len(C[0]))] for i in range(len(D))]

C = [[2,4,6], [5,10,15]] #len(C[0]) = 3
D = [2,5] # len(D) = 2
result = [[C[i][j]/D[i] for j in range(len(C[0]))] for i in range(len(D))]
print result

If you first cast D to type numpy.matrix (which I'm assuming you can do unless D is too big to fit into memory), then you can just run

C.multiply(1.0 / D.T)

to get what you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!