Iterating over scipy sparse matrix by column

﹥>﹥吖頭↗ 提交于 2020-01-03 17:26:27

问题


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?


回答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]]


来源:https://stackoverflow.com/questions/10475457/iterating-over-scipy-sparse-matrix-by-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!