<s> import numpy as np <EOL> import scipy . sparse as sp <EOL> def sparse_sum ( sparse_matrix1 , axis = <NUM_LIT:0> ) : <EOL> if not sp . isspmatrix_csr ( sparse_matrix1 ) : <EOL> sparse_matrix = sp . csr_matrix ( sparse_matrix1 ) <EOL> else : <EOL> sparse_matrix = sparse_matrix1 <EOL> m , n = sparse_matrix . shape <EOL> if <NUM_LIT:0> == axis : <EOL> result = np . zeros ( n , np . int32 ) <EOL> xy = sparse_matrix . nonzero ( ) <EOL> for i , j , v in zip ( xy [ <NUM_LIT:0> ] , xy [ <NUM_LIT:1> ] , np . asarray ( sparse_matrix . data ) ) : <EOL> result [ j ] += int ( v ) <EOL> return result <EOL> elif <NUM_LIT:1> == axis : <EOL> result = np . zeros ( m , np . int32 ) <EOL> xy = sparse_matrix . nonzero ( ) <EOL> for i , j , v in zip ( xy [ <NUM_LIT:0> ] , xy [ <NUM_LIT:1> ] , np . asarray ( sparse_matrix . data ) ) : <EOL> result [ i ] += int ( v ) <EOL> return result <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % axis ) </s>
