Skip to content Skip to sidebar Skip to footer

Iterating Over Scipy Sparse Matrix By Column

I'm trying to figure out how to iterate through a scipy sparse matrix by column. I'm trying to compute the sum of each column, then weight the members of that column by that sum.

Solution 1:

Scipy sparse matrices have their own sum method you can use for this. For example:

A=sp.lil_matrix((5,5))
b=1+np.arange(0,5)
A.setdiag(b[:-1],k=1)
A.setdiag(b)


print(A)
  (0, 0)        1.0
  (0, 1)        1.0
  (1, 1)        2.0
  (1, 2)        2.0
  (2, 2)        3.0
  (2, 3)        3.0
  (3, 3)        4.0
  (3, 4)        4.0
  (4, 4)        5.0

f=A.sum(axis=0)

print(f)   
[[1. 3. 5. 7. 9.]]

The returned sum is a dense numpy.matrix which you can convert into scaling factors:

print(A/f)
[[1.         0.33333333 0.         0.         0.        ]
 [0.         0.66666667 0.4        0.         0.        ]
 [0.         0.         0.6        0.42857143 0.        ]
 [0.         0.         0.         0.57142857 0.44444444]
 [0.         0.         0.         0.         0.55555556]]

Post a Comment for "Iterating Over Scipy Sparse Matrix By Column"