简体   繁体   中英

taking the average of two histograms in python

I have two histograms (freq dist) and I want to take the average of the frequencies in each corresponding bin of the two histograms and get one histogram that displays the average frequencies.

Can I do this with pylab?

a=[]
sites = [23,45,32,56,76,87,45,21,34,23,78,90,23,45,21,32,34,54,67,54,33,12,34]
import random
j=1
for i in range(1,len(sites)):
    r = random.choice([0,1])
    if r == 1:
        a.append(sites[i] - sites[i-j])
        j=1
    else:
        j+=1
import pylab
pylab.hist(a, bins=10)
pylab.title("hist")
pylab.xlabel("length")
pylab.ylabel("Count")
pylab.show()

the snippet code is run several times with different "sites" data to get a several histograms. I want to "average" these histograms in to one.

This is unreasonable unless both histograms have the same total number of elements, otherwise you must take the weighted average.

To do so, assuming your histograms have the same frequency bins, do this if the histograms are list-like:

[(x1+x2)/2 for x1,x2 in zip(h1,h2)]

If the histograms are dict-like:

def mergeBins(bin1, bin2):
    label1,value1 = bin1
    label2,value2 = bin2
    assert label1==label2
    return label1, (value1+value2)/2

dict(mergeBins() in bin1,bin2 zip(h1.items(), h2.items()))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM