Tile Operation To Create A Csr_matrix From One Row Of Another Csr_matrix
Solution 1:
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)
This takes 2nd row of 'a' and tiles it to create 'b'.
Following is the timing test, seems quite fast for 10000x10000 matrix:
100 loops, best of 3: 2.24 ms per loop
Solution 2:
There is a blk
format, that lets you create a new sparse matrix from a list of other matrices.
So for a start you could
a1 = a[I,:]
ll = [a1,a1,a1,a1]
sparse.blk_matrix(ll)
I don't have a shell running to test this.
Internally this format turns all input arrays into coo
format, and collects their coo
attributes into 3 large lists (or arrays). In your case of tiled rows, the data
and col
(j) values would just repeat. The row
(I) values would step.
Another way to approach it would be to construct a small test matrix, and look at the attributes. What kinds of repetition do you see? It's easy to see patterns in the coo
format. lil
might also be easy to replicate, maybe with the list *n
operation. csr
is trickier to understand.
Solution 3:
One can do
row = a.getrow(row_idx)
n_rows = a.shape[0]
b = tiled_row = sp.sparse.vstack(np.repeat(row, n_rows))
Post a Comment for "Tile Operation To Create A Csr_matrix From One Row Of Another Csr_matrix"