Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

python - Row Division in Scipy Sparse Matrix

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,10,15]]
D = [2,5]

I want the result of C after division to be :

result = [[1, 2, 3], [1, 2, 3]]

I have tried this using the method that we use for numpy arrays:

result = C / D[:,None]

But this seems really slow. How to do this efficiently in sparse matrices?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...