替代方案bincount
is add.at
:
In [193]: mat
Out[193]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [194]: ixs
Out[194]: array([0, 2, 0, 0, 0, 1, 1])
In [195]: J = np.zeros(mat.shape[0],int)
In [196]: np.add.at(J, ixs, 1)
In [197]: J
Out[197]: array([4, 2, 1])
In [198]: np.dot(J, mat)
Out[198]: array([16, 23, 30, 37])
我认为,通过稀疏性,你的意思是ixs
可能不包括所有行,例如,ixs
没有 0:
In [199]: ixs = np.array([2,1,1])
In [200]: J=np.zeros(mat.shape[0],int)
In [201]: np.add.at(J, ixs, 1)
In [202]: J
Out[202]: array([0, 2, 1])
In [203]: np.dot(J, mat)
Out[203]: array([16, 19, 22, 25])
J
仍然有mat.shape[0]
形状。但是add.at
应按长度缩放ixs
.
稀疏解决方案看起来像:
制作一个稀疏矩阵ixs
看起来像:
In [204]: I
Out[204]:
array([[1, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 1],
[0, 1, 0, 0, 0, 0, 0]])
对行求和;稀疏通过矩阵乘法来实现这一点,例如:
In [205]: np.dot(I, np.ones((7,),int))
Out[205]: array([4, 2, 1])
然后做我们的点:
In [206]: np.dot(np.dot(I, np.ones((7,),int)), mat)
Out[206]: array([16, 23, 30, 37])
或者在稀疏代码中:
In [225]: J = sparse.coo_matrix((np.ones_like(ixs,int),(np.arange(ixs.shape[0]), ixs)))
In [226]: J.A
Out[226]:
array([[1, 0, 0],
[0, 0, 1],
[1, 0, 0],
[1, 0, 0],
[1, 0, 0],
[0, 1, 0],
[0, 1, 0]])
In [227]: J.sum(axis=0)*mat
Out[227]: matrix([[16, 23, 30, 37]])
sparse
,当转换自coo
to csr
对重复项求和。我可以利用这一点
In [229]: J = sparse.coo_matrix((np.ones_like(ixs,int), (np.zeros_like(ixs,int), ixs)))
In [230]: J
Out[230]:
<1x3 sparse matrix of type '<class 'numpy.int32'>'
with 7 stored elements in COOrdinate format>
In [231]: J.A
Out[231]: array([[4, 2, 1]])
In [232]: J*mat
Out[232]: array([[16, 23, 30, 37]], dtype=int32)