问题描述
我有一个csr_matrix'a'类型的稀疏矩阵.我想执行一个操作来创建一个新的csr_matrix'b',其中'b'的每一行都与'a'的第i行相同.
I have a csr_matrix 'a' type of sparse matrix. I want to perform an operation to create a new csr_matrix 'b' where each row of 'b' is same ith row of 'a'.
我认为对于普通的numpy数组,可以使用"tile"操作.但是我找不到csr_matrix的相同内容.
I think for normal numpy arrays it is possible using 'tile' operation. But I am not able to find the same for csr_matrix.
首先制作一个numpy矩阵并转换为csr_matrix是不可行的,因为矩阵的大小为10000 x10000.
Making first a numpy matrix and converting to csr_matrix is not an option as the size of matrix is 10000 x 10000.
推荐答案
实际上,我可以回答这并不需要创建完整的numpy矩阵,而且对于我而言,它的速度非常快.因此,如果对将来的人有用,则将其添加为答案:
I actually could get to answer which doesn't require creating full numpy matrix and is quite fast for my purpose. So adding it as answer if it's useful for people in future:
rows, cols = a.shape
b = scipy.sparse.csr_matrix((np.tile(a[2].data, rows), np.tile(a[2].indices, rows),
np.arange(0, rows*a[2].nnz + 1, a[2].nnz)), shape=a.shape)
这需要'a'的第二行并将其平铺以创建'b'.
This takes 2nd row of 'a' and tiles it to create 'b'.
以下是时序测试,对于10000x10000矩阵来说似乎非常快:
Following is the timing test, seems quite fast for 10000x10000 matrix:
100 loops, best of 3: 2.24 ms per loop
这篇关于拼贴操作,从另一csr_matrix的一行创建csr_matrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!