Come ottenere i dati in un bidone istogramma
-
21-09-2019 - |
Domanda
Voglio ottenere un elenco dei dati contenuti in un bidone istogramma. Sto usando NumPy e Matplotlib. So come attraversare i dati e controllare i bordi bin. Tuttavia, voglio fare questo per un istogramma 2D e il codice per farlo è piuttosto brutto. Non NumPy avere costrutti per rendere questo più facile?
Per il caso 1D, posso usare searchsorted (). Ma la logica non è molto meglio, e non ho molta voglia di fare una ricerca binaria su ogni punto di dati quando non devo.
La maggior parte della logica brutto è dovuto alle regioni di confine bin. Tutte le regioni hanno confini come questo: [bordo sinistro, lato destro). Tranne l'ultimo bidone, che ha una regione come questa: [bordo sinistro, lato destro].
Ecco alcuni esempi di codice per il caso 1D:
import numpy as np
data = [0, 0.5, 1.5, 1.5, 1.5, 2.5, 2.5, 2.5, 3]
hist, edges = np.histogram(data, bins=3)
print 'data =', data
print 'histogram =', hist
print 'edges =', edges
getbin = 2 #0, 1, or 2
print '---'
print 'alg 1:'
#for i in range(len(data)):
for d in data:
if d >= edges[getbin]:
if (getbin == len(edges)-2) or d < edges[getbin+1]:
print 'found:', d
#end if
#end if
#end for
print '---'
print 'alg 2:'
for d in data:
val = np.searchsorted(edges, d, side='right')-1
if val == getbin or val == len(edges)-1:
print 'found:', d
#end if
#end for
Ecco alcuni esempi di codice per il caso 2D:
import numpy as np
xdata = [0, 1.5, 1.5, 2.5, 2.5, 2.5, \
0.5, 0.5, 0.5, 0.5, 1.5, 1.5, 1.5, 1.5, 1.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, \
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 3]
ydata = [0, 5,5, 5, 5, 5, \
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, \
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 30]
xbins = 3
ybins = 3
hist2d, xedges, yedges = np.histogram2d(xdata, ydata, bins=(xbins, ybins))
print 'data2d =', zip(xdata, ydata)
print 'hist2d ='
print hist2d
print 'xedges =', xedges
print 'yedges =', yedges
getbin2d = 5 #0 through 8
print 'find data in bin #', getbin2d
xedge_i = getbin2d % xbins
yedge_i = int(getbin2d / xbins) #IMPORTANT: this is xbins
for x, y in zip(xdata, ydata):
# x and y left edges
if x >= xedges[xedge_i] and y >= yedges[yedge_i]:
#x right edge
if xedge_i == xbins-1 or x < xedges[xedge_i + 1]:
#y right edge
if yedge_i == ybins-1 or y < yedges[yedge_i + 1]:
print 'found:', x, y
#end if
#end if
#end if
#end for
C'è un detergente / modo più efficiente per fare questo? Sembra che NumPy avrebbe qualcosa per questo.
Soluzione
digitize
, dal nucleo NumPy, vi darà l'indice del contenitore a cui ciascun valore nella vostra istogramma appartiene:
import numpy as NP
A = NP.random.randint(0, 10, 100)
bins = NP.array([0., 20., 40., 60., 80., 100.])
# d is an index array holding the bin id for each point in A
d = NP.digitize(A, bins)
Altri suggerimenti
come a qualcosa di simile:
In [1]: data = numpy.array([0, 0.5, 1.5, 1.5, 1.5, 2.5, 2.5, 2.5, 3])
In [2]: hist, edges = numpy.histogram(data, bins=3)
In [3]: for l, r in zip(edges[:-1], edges[1:]):
print(data[(data > l) & (data < r)])
....:
....:
[ 0.5]
[ 1.5 1.5 1.5]
[ 2.5 2.5 2.5]
In [4]:
con un po 'di codice per gestire i casi limite.
pyplot.hist in matplotlib crea un istogramma ( ma richiama anche allo schermo, che non si potrebbe desiderare). Per soli i bidoni, è possibile utilizzare numpy.histogram, come sottolineato in un'altra risposta.
Qui è un esempio confrontando pyploy.hist e numpy.histogram.