nev*_*int 10 python statistics histogram binning
我在float中设置了值(总是小于0).我希望将其分为直方图,即e.直方图中的每个条形包含值范围[0,0.150)
我的数据看起来像这样:
0.000
0.005
0.124
0.000
0.004
0.000
0.111
0.112
Run Code Online (Sandbox Code Playgroud)
我的代码如下,我希望得到的结果看起来像
[0, 0.005) 5
[0.005, 0.011) 0
...etc..
Run Code Online (Sandbox Code Playgroud)
我尝试用我的这个代码做这样的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])))
Run Code Online (Sandbox Code Playgroud)
〜
unu*_*tbu 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]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18326 次 |
| 最近记录: |