如何在Python中将浮点值系列分成直方图?

时间:2009-11-12 10:21:48

标签: python statistics histogram binning

我在float中设置了值(总是小于0)。我想把它分成直方图, I,E。直方图中的每个条形包含值范围[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.. 

我试着用我的这个代码做这样的binning。 但它似乎没有用。什么是正确的方法?

#! /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])))

3 个答案:

答案 0 :(得分:15)

如果可能,请勿重新发明轮子。 NumPy拥有您需要的一切:

#!/usr/bin/env python
import numpy as np

a = np.fromfile(open('file', 'r'), sep='\n')
# [ 0.     0.005  0.124  0.     0.004  0.     0.111  0.112]

# You can set arbitrary bin edges:
bins = [0, 0.150]
hist, bin_edges = np.histogram(a, bins=bins)
# hist: [8]
# bin_edges: [ 0.    0.15]

# Or, if bin is an integer, you can set the number of bins:
bins = 4
hist, bin_edges = np.histogram(a, bins=bins)
# hist: [5 0 0 3]
# bin_edges: [ 0.     0.031  0.062  0.093  0.124]

答案 1 :(得分:3)

from pylab import *
data = []
inf = open('pulse_data.txt')
for line in inf:
    data.append(float(line))
inf.close()
#binning
B = 50
minv = min(data)
maxv = max(data)
bincounts = []
for i in range(B+1):
    bincounts.append(0)
for d in data:
    b = int((d - minv) / (maxv - minv) * B)
    bincounts[b] += 1
# plot histogram

plot(bincounts,'o')
show()

答案 2 :(得分:2)

第一个错误是:

Traceback (most recent call last):
  File "C:\foo\foo.py", line 17, in <module>
    diffCounts[ str(getBin(diff)) ] += 1
TypeError: list indices must be integers

为什么在需要str时将int转换为str?解决这个问题,然后我们得到:

Traceback (most recent call last):
  File "C:\foo\foo.py", line 17, in <module>
    diffCounts[ getBin(diff) ] += 1
IndexError: list index out of range

因为你只制作了5个桶。我不明白你的分组方案,但让我们把它变成50桶,看看会发生什么:

6
Traceback (most recent call last):
  File "C:\foo\foo.py", line 21, in <module>
    maxBin = max(maxdiff)
TypeError: 'int' object is not iterable

maxdiff是您的整数列表中的单个值,那么max在这里做什么?删除它,现在我们得到:

6
Traceback (most recent call last):
  File "C:\foo\foo.py", line 28, in <module>
    print binStr + '\t' + '\t'.join(map(str, (diffCounts[i])))
TypeError: argument 2 to map() must support iteration

果然,您使用单个值作为map的第二个参数。让我们简化最后两行:

 binStr = '[' + str(lo) + ',' + str(hi) + ')'
 print binStr + '\t' + '\t'.join(map(str, (diffCounts[i])))

到此:

 print "[%f, %f)\t%r" % (lo, hi, diffCounts[i])

现在打印:

6
[0.000000, 1.000000)    3
[1.000000, 3.000000)    0
[3.000000, 7.000000)    2
[7.000000, 15.000000)   0
[15.000000, 31.000000)  0
[31.000000, 63.000000)  0
[63.000000, 127.000000) 3

我不确定这里还有什么可做的,因为我真的不明白你希望使用的那个。它似乎涉及二元权力,但对我来说没有意义......