I have a sparce matrix (coo_matrix) like this:
type(matrix)
scipy.sparse.coo.coo_matrix
print(matrix)
(0, 14) 1.0
(1, 17) 1.0
(3, 6) 1.0
(6, 3) 1.0
(12, 15) 2.0
(12, 13) 3.0
(13, 16) 2.0
(13, 12) 3.0
(14, 0) 1.0
(15, 16) 1.0
(15, 12) 2.0
(16, 15) 1.0
(16, 13) 2.0
(17, 1) 1.0
(18, 19) 1.0
(19, 18) 1.0
I can sum this matrix, but the result is printed without row names, so it is unclear to which element this sum is belong:
print(matrix.sum(axis = 1))
[[1.]
[1.]
[0.]
[1.]
[0.]
[0.]
[1.]
[0.]
[0.]
[0.]
[0.]
[0.]
[5.]
[5.]
[1.]
[3.]
[3.]
[1.]
[1.]
[1.]]
Is it possible to set the row names for matrix to print result like this (so the row name will be corresponding to the sparce matrix element):
0: 1
1: 1
3: 1
6: 1
12: 5
13: 5
14: 1
15: 3
16: 3
17: 1
18: 1
19: 1
I find a solution like this:
Mc=matrix.tocoo()
Mc_sum = {k:v for k,v in zip(Mc.col, Mc.sum(axis = 1))}
It gives almost desired output, but still not the one I want:
{17: matrix([[63.]]), 11: matrix([[25.]]), 5: matrix([[105.]]), 20: matrix([[31.]])...}