Reputation: 41
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. What I want to do is basically:
for i=0 to #columns
for j=0 to #rows
sum=sum+matrix[i,j]
for j=0to #rows
matrix[i,j]=matrix[i,j]/sum
All of the iterators I've seen in examples iterate over the entire matrix at once instead of doing it per column. Is there a way to do what I'm trying to do?
Upvotes: 4
Views: 3383
Reputation: 72339
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]]
Upvotes: 3