どのようにPythonでのヒストグラムへの浮動小数点値のビンシリーズに?
-
19-09-2019 - |
質問
私は、フロート(常に0未満の)に値を設定しています。私は、ヒストグラムにビンしたいです、 私は、E。ヒストグラムの各バーは、値の範囲を含む[0,0.150)
私が持っているデータは、次のようになります:
0.000
0.005
0.124
0.000
0.004
0.000
0.111
0.112
私のコードWhith私は
のように見える結果を得ることを期待を下回ります[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])))
〜
解決
可能な場合、車輪の再発明をしないでください。 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]
他のヒント
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()
最初のエラーがある:
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が必要なときになぜあなたは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
はintのリストのうち単一の値なので、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
する2番目の引数として単一の値を使用しています。
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
私は本当にあなたが使用することを望んでいるバケットを理解していないので、私は、ここで行うには他に何かわかりません。
...バイナリの力が関与しているようだが、私には意味を作っていません所属していません StackOverflow