Scipy稀疏矩阵中的行除法 [英] Row Division in Scipy Sparse Matrix
问题描述
我想将稀疏矩阵的行除以数组中给出的标量.
I want to divide a sparse matrix's rows by scalars given in an array.
例如:我有一个csr_matrix C:
For example : I have a csr_matrix C :
C = [[2,4,6], [5,10,15]]
D = [2,5]
我希望C除后的结果是:
I want the result of C after division to be :
result = [[1, 2, 3], [1, 2, 3]]
我已经使用了用于numpy数组的方法进行了尝试:
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 ?
推荐答案
方法1
这是使用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)
输出是一个稀疏矩阵,与C / D[:,None]
的输出相反,它创建一个完整的矩阵.因此,该方法节省了内存.
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.
使用np.repeat
代替索引进行复制可能会提高性能-
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))
方法2
另一种方法可能涉及稀疏矩阵的data
方法,该方法为我们提供了对in-place
结果的稀疏矩阵的扁平视图,并且避免使用nonzero
,就像这样-
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
这篇关于Scipy稀疏矩阵中的行除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!