How to get sum of each row and sum of each column in Scipy sparse matrices (csr_matrix and csc_matrix)?

Viewed 21015

I have a very large Scipy sparse matrix ( CSR_MATRIX ). I just want to know how i can compute the sum of values for each row and also the sum of values for each column of the matrix.

I have a code that does the same operation but it is using CSC_MATRIX. Is there anything different between these two regarding summing the rows and columns?

I thought maybe I can get a quick response that others can also use or else I can test it myself.

from scipy.sparse import *
from scipy import *
row = array([0,0,1,2,2,2])
col = array([0,2,2,0,1,2])
data = array([1,2,3,4,5,6])
csr_matrix( (data,(row,col)), shape=(3,3) ).todense()
rowsums = []
colsums = []
#compute rowsums and colsums

so rowsums should be [3, 3, 15] and colsum should be [5, 5, 11].

I know that i can use matrix.getrow(i) and matrix.getcol(i) to get each row and column and use sum() function to get the sum but my concern is performance. I need a more efficient solution.

1 Answers
Related