How to sum specific row values together in Sparse COO matrix to reshape matrix

Question:

I have a sparse coo matrix built in python using the scipy library. An example data set looks something like this:

>>> v.toarray()
array([[1, 0, 2, 4],
       [0, 0, 3, 1],
       [4, 5, 6, 9]])

I would like to add the 0th index and 2nd index together and the 1st index and the and 3rd index together so the shape would change from 3, 4 to 3, 2.

However looking at the docs their sum function doesn’t support slicing of some sort. So the only way I have thought of a way to do something like that would be to loop the matrix as an array then use numpy to get the summed values like so:

a_col = []
b_col = []
for x in range(len(v.toarray()):
    a_col.append(np.sum(v.toarray()[x, [0, 2]], axis=0))
    b_col.append(np.sum(v.toarray()[x, [1, 3]], axis=0))

Then use those values for a_col and b_col to create the matrix again.

But surely there should be a way to handle it with the sum method?

Asked By: mp252

||

Answers:

You can add the values with a simple loop and 2d slicing and than take the columns you want

v = np.array([[1, 0, 2, 4],
              [0, 0, 3, 1],
              [4, 5, 6, 9]])

for i in range(2):
    v[:, i] = v[:, i] + v[:, i+2]

print(v[:, :2])

Output

[[ 3  4]
 [ 3  1]
 [10 14]]
Answered By: Guy

You can use csr_matrix.dot with a special matrix to achieve the same,

csr = csr_matrix(csr.dot(np.array([[1,0,1,0],[0,1,0,1]]).T))
#csr.data
#[ 3,  4,  3,  1, 10, 14]
Answered By: V.M
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.