我有一组float(总是小于0)的值。我想把它分成直方图, 我,例如。直方图各条包含的值范围[0,0.150)Howto将一系列float值转换为Python中的直方图?
我的数据是这样的:
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])))
〜
好了, “你所期望的......”,如果有)定义为0,0.005范围(右开),示例[0.005,0.011) (关闭左侧) 那么输出应该是: [0,0.005)4 [0.005,0.011)1 etc ... – Gacek 2009-11-12 10:56:36
“似乎不工作?”任何具体的投诉?或者你是否期望每个人都必须运行它并尝试猜测你不喜欢输出的内容? – 2009-11-12 10:58:00
为避免重新发明轮子,尤其是在下一步绘制直方图时:应考虑使用处理所有这些的Matplotlib框架。 – RedGlyph 2009-11-12 12:13:23