2009-11-12 53 views
8

我有一組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,0.005範圍(右開),示例[0.005,0.011) (關閉左側) 那麼輸出應該是: [0,0.005)4 [0.005,0.011)1 etc ... – Gacek 2009-11-12 10:56:36

+0

「似乎不工作?」任何具體的投訴?或者你是否期望每個人都必須運行它並嘗試猜測你不喜歡輸出的內容? – 2009-11-12 10:58:00

+0

爲避免重新發明輪子,尤其是在下一步繪製直方圖時:應考慮使用處理所有這些的Matplotlib框架。 – RedGlyph 2009-11-12 12:13:23

回答

13

如果可能,請勿重新發明輪子。 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] 
+0

如果你想要一個歸一化的直方圖,你可以添加下面的代碼: hist = hist * 1。0/sum(hist) – dval 2015-12-04 22:34:28

+0

如果你希望bin範圍內的積分爲1,使用['density = True'](http://docs.scipy.org/doc/numpy-1.10.1/reference/生成/ numpy.histogram.html)。 – unutbu 2015-12-05 02:01:00

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轉換爲海峽?解決這個問題,那麼我們得到:

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 

我不知道自己能做什麼在這裏,因爲我真的不明白你希望使用的bucketing。它似乎涉及二進制的權力,但對我來說沒有意義...

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()