I have a large scipy sparse symmetric matrix which I need to condense by taking the sum of blocks to make a new smaller matrix.
For example, for a 4×4 sparse matrix A I will like to make a 2×2 matrix B in which B[i,j] = sum(A[i:i+2,j:j+2]).
Currently, I just go block by block to recreate the condensed matrix but this is slow. Any ideas on how to optimize this?
Update: Here is an example code that works fine, but is slow for a sparse matrix of 50.000×50.000 that I want to condense in a 10.000×10.000:
>>> A = (rand(4,4)<0.3)*rand(4,4)
>>> A = scipy.sparse.lil_matrix(A + A.T) # make the matrix symmetric
>>> B = scipy.sparse.lil_matrix((2,2))
>>> for i in range(B.shape[0]):
... for j in range(B.shape[0]):
... B[i,j] = A[i:i+2,j:j+2].sum()
First of all,
lilmatrix for the one your are summing up is probably really bad, I would tryCOOor maybeCSR/CSS(I don’t know which will be better, butlilis probably inherently slower for many of these operations, even the slicing might be much slower, though I did not test). (Unless you know that for examplediafits perfectly)Based on
COOI could imagine doing some tricking around. SinceCOOhasrowandcolarrays to give the exact positions:(I won’t guarantee that I didn’t confuse row and column somewhere and this only works for square matrices…)