14

float に値を設定しました (常に 0 未満)。ヒストグラムにビン化したいもの、つまり。ヒストグラムの各バーには、値の範囲 [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.. 

私はこのコードでそのようなビニングをやろうとしました。しかし、うまくいかないようです。それを行う正しい方法は何ですか?

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

4

3 に答える 3

18

可能であれば、車輪を再発明しないでください。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]
于 2009-11-12T12:28:17.953 に答える
4
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()
于 2012-11-12T15:33:31.880 に答える
3

最初のエラーは次のとおりです。

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は 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

案の定、単一の値を の 2 番目の引数として使用しています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

ここで他に何をすればよいかわかりません。あなたが使用したいと考えているバケット化がよくわからないからです。バイナリパワーが関係しているようですが、私には意味がありません...

于 2009-11-12T12:19:45.380 に答える