Wie man ist Reihe von Float-Werten in Histogramm in Python?
-
19-09-2019 - |
Frage
Ich habe in Float der Wert (immer weniger als 0). Was ich zu bin in Histogramm möchten, i, e. Jeder Balken im Histogramm Wertebereich enthält [0,0.150)
Die Daten, die ich wie folgt aussieht haben:
0.000
0.005
0.124
0.000
0.004
0.000
0.111
0.112
Whith meinen Code unten Ich erwarte, dass Ergebnis zu erhalten, das aussieht wie
[0, 0.005) 5
[0.005, 0.011) 0
...etc..
habe ich versucht, so Binning mit diesem Code von mir zu tun zu tun. Aber es scheint nicht zu arbeiten. Was ist der richtige Weg, es zu tun?
#! /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])))
~
Lösung
Wenn möglich, nicht neu erfinden das Rad nicht. NumPy hat alles, was Sie brauchen:
#!/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]
Andere Tipps
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()
Der erste Fehler ist:
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
Warum konvertieren Sie einen int zu einem str, wenn ein str benötigt? Fix, dass, dann erhalten wir:
Traceback (most recent call last):
File "C:\foo\foo.py", line 17, in <module>
diffCounts[ getBin(diff) ] += 1
IndexError: list index out of range
, weil Sie nur 5 Eimer gemacht haben. Ich verstehe nicht, Ihre Bucketing Schema, aber wir machen es 50 Eimer und sehen, was passiert:
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
ist ein einzelner Wert aus der Liste der ints, so etwas max
hier tut? Entfernen Sie es, jetzt erhalten wir:
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
Sicher genug, sind Sie einen einzelnen Wert als das zweite Argument zu map
verwenden. Lassen Sie uns die letzten beiden Zeilen von dieser Vereinfachung:
binStr = '[' + str(lo) + ',' + str(hi) + ')'
print binStr + '\t' + '\t'.join(map(str, (diffCounts[i])))
folgt aus:
print "[%f, %f)\t%r" % (lo, hi, diffCounts[i])
Jetzt druckt:
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
Ich bin sicher nicht, was sonst hier zu tun, da ich nicht wirklich verstehen, die bucketing Sie verwenden hoffen. Es scheint, binäre Kräfte einzubeziehen, ist aber nicht für mich einen Sinn ...