Scipy 稀疏矩阵中的行划分 [英] Row Division in Scipy Sparse Matrix

查看:33
本文介绍了Scipy 稀疏矩阵中的行划分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用数组中给定的标量划分稀疏矩阵的行.

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 的稀疏矩阵解决方案 -

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)

输出是一个稀疏矩阵,与创建完整矩阵的 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 结果的稀疏矩阵的扁平化视图,并且还避免使用 非零,就像这样 -

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆