Search code examples
pythonmatrixsumscipysparse-matrix

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


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.


Solution

  • Use the axis argument of the sum method:

    In [2]: row = array([0,0,1,2,2,2])
    
    In [3]: col = array([0,2,2,0,1,2])
    
    In [4]: data = array([1,2,3,4,5,6])
    
    In [5]: a = csr_matrix((data, (row, col)), shape=(3,3))
    
    In [6]: a.A
    Out[6]: 
    array([[1, 0, 2],
           [0, 0, 3],
           [4, 5, 6]])
    
    In [7]: a.sum(axis=0)  # sum the columns
    Out[7]: matrix([[ 5,  5, 11]])
    
    In [8]: a.sum(axis=1)  # sum the rows
    Out[8]: 
    matrix([[ 3],
            [ 3],
            [15]])