numpy的积累在另一个阵列使用数组索引 [英] Numpy accumulating one array in another using index array
本文介绍了numpy的积累在另一个阵列使用数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是关于我使用numpy的要前preSS特定阵列操作。
My question is about a specific array operation that I want to express using numpy.
我有float数组是W
和相同长度的指数 IDX
数组作为是W
,我想总结一下所有的是W
用相同的 IDX
价值并收集他们在一个数组 v
。
作为一个循环,这看起来是这样的:
I have an array of floats w
and an array of indices idx
of the same length as w
and I want to sum up all w
with the same idx
value and collect them in an array v
.
As a loop, this looks like this:
for i, x in enumerate(w):
v[idx[i]] += x
有没有办法用数组操作做到这一点?
我的猜测是 v [IDX] + = W
但是,这并不工作,因为 IDX
包含相同的索引多次
Is there a way to do this with array operations?
My guess was v[idx] += w
but that does not work, since idx
contains the same index multiple times.
谢谢!
推荐答案
numpy.bincount
中引入了这一目的:
tmp = np.bincount(idx, w)
v[:len(tmp)] += tmp
我觉得1.6也可以传递给MINLENGTH bincount
。
这篇关于numpy的积累在另一个阵列使用数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文