按列迭代稀疏矩阵 [英] 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. 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?
推荐答案
Scipy稀疏矩阵具有自己的sum
方法,可用于此目的.例如:
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.]]
返回的总和是一个密集的numpy.matrix
,您可以将其转换为比例因子:
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]]
这篇关于按列迭代稀疏矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!