我有一组浮点值(总是小于 0)。我想将其分类为直方图,即 直方图中的每个条形包含值范围 [0,0.150)
我拥有的数据如下所示:
0.000
0.005
0.124
0.000
0.004
0.000
0.111
0.112
下面的代码我希望得到看起来像的结果
[0, 0.005) 5
[0.005, 0.011) 0
...etc..
我试图用我的这段代码来做这样的分箱。但这似乎不起作用。正确的方法是什么?
#! /usr/bin/env python
import fileinput, math
log2 = math.log(2)
def getBin(x):
return int(math.log(x+1)/log2)
diffCounts = [0] * 5
for line in fileinput.input():
words = line.split()
diff = float(words[0]) * 1000;
diffCounts[ str(getBin(diff)) ] += 1
maxdiff = [i for i, c in enumerate(diffCounts) if c > 0][-1]
print maxdiff
maxBin = max(maxdiff)
for i in range(maxBin+1):
lo = 2**i - 1
hi = 2**(i+1) - 1
binStr = '[' + str(lo) + ',' + str(hi) + ')'
print binStr + '\t' + '\t'.join(map(str, (diffCounts[i])))
~