python - numpy bincount - 选择最大权重而不是对所有权重求和

标签 python python-2.7 numpy

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

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个元素)。

最佳答案

solution to your 2D question对于一维情况也有效,因此您可以使用 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. ])

关于python - numpy bincount - 选择最大权重而不是对所有权重求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47525773/

相关文章:

python - 带信号灯的异步/多线程

python - 重复 numpy 值并指定 dtype

python-3.x - 如何通过忽略字符串前面的特殊字符从文本中提取正确的字符串?

Python从列表中的子字符串打印字符串

python - 为什么 PIPE 文件的 readline() 如此慢?

python - 使用 python 类变量来管理全局列表(池)

python - 查找相同相邻元素的差异

python - 根据条件删除 pandas DataFrame 中的重复行

bash - Shell-Script Python 调用失败并出现语法错误

python - 具有固定峰值的抛物线拟合