本文介绍了numpy bincount-选择最大权重而不是所有权重之和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用np.bincount但获得最大值而不是权重之和?此处,索引为3bbb具有两个值,11.155.5.我想要的是55.5,而不是66.6.我怀疑我选择使用其他功能,但不能确定哪个功能适合此目的.

Is it possible to use np.bincount but get the max instead of sum of weights? Here, bbb at index 3 has two values, 11.1 and 55.5. I want to have 55.5, not 66.6. I doubt I choose use other function but not so sure which one is good for this purpose.

bbb = np.array([ 3, 7, 11, 13, 3])
weight = np.array([ 11.1, 22.2, 33.3, 44.4, 55.5])
print np.bincount(bbb, weight, minlength=15)

OUT >> [  0.    0.    0.   66.6   0.    0.    0.   22.2   0.    0.    0.   33.3   0.   44.4   0. ]

请注意,实际上bbbweight很大(大约5e6个元素).

Note that, in fact, bbb and weight are very large (about 5e6 elements).

推荐答案

您的2D问题解决方案也有效对于一维情况,因此您可以使用np.maxmimum.at

The solution to your 2D question is also valid for the 1D case, so you can use np.maxmimum.at

out = np.zeros(15)
np.maximum.at(out, bbb, weight)
# array([  0. ,   0. ,   0. ,  55.5,   0. ,   0. ,   0. ,  22.2,   0. ,
#          0. ,   0. ,  33.3,   0. ,  44.4,   0. ])

这篇关于numpy bincount-选择最大权重而不是所有权重之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 14:42