块状直方图,如何在每个箱中取最大值 [英] Numpy histogram, how to take the maximum value in each bin

查看:140
本文介绍了块状直方图,如何在每个箱中取最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用上面的代码合并了一系列数字.是否可以在每个回收箱中返回最大数量?

I have a series of numbers that I bin with the code above. Is it possible to return the maximum number in each bin?

看看示例代码:

  from numpy import *


  a=array([1,4,5,6,7.8,9,3.4,5.,6,3.5,6,8,9,10])

  bins=arange(0,11,1)

  h=hist(a,bins=bins)

  h=hist(a,bins=bins,weights=a)

这就是退货

  (array([  0. ,   1. ,   0. ,   6.9,   4. ,  10. ,  18. ,   7.8,   8. ,  28. ]), array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

我想知道是否有可能在第四个存储箱中获得3.5(这是3到4之间的最大数字)而不是6.9.

I was wondering if it was possible to get 3.5 (which is the maximum number between 3 and 4) instead of 6.9 in the 4th bin.

推荐答案

这将为您提供bin中每个元素的最大值,如果bin中没有元素,则为0:

This will give you the maximum value of each element in a bin, and 0 if there are no elements in the bin:

print [max(a[(a>=(i))&(a<i+1)]) if a[(a>=(i))&(a<i+1)].size else 0 for i in bins]
[0, 1.0, 0, 3.5, 4.0, 5.0, 6.0, 7.7999999999999998, 8.0, 9.0, 10.0]

+1更改为垃圾箱大小,以使其有用.

Change +1 to your bin size, to make it useful.

这篇关于块状直方图,如何在每个箱中取最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆