In [1]:
import numpy as np

a = np.random.poisson(5, 100000)

In [2]:
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

count, bins, ignored = plt.hist(a, 14, normed=True)
plt.show()



In [40]:
def misra_gries(arr, k):
    m = arr.shape[0]
    frq = {}
    for x in arr:
        if x in frq:
            frq[x] += 1
        elif len(frq.keys())<k:
            frq[x] = 1
        else:
            for key in frq.keys():
                frq[key] -= 1
            frq = { key : val for key,val in frq.items() if k == 0 }

    return(frq)

## test
t = np.array([1,2,1,5,3,1,2,4,1,4,2,1,1,2])

misra_gries(t, 3)


Out[40]:
{1: 4, 2: 3, 4: 2}

In [41]:
misra_gries(a, 10)


Out[41]:
{1: 5, 2: 8, 3: 8, 4: 15, 5: 19, 6: 16, 7: 7, 8: 2, 9: 1, 10: 1}

In [43]:
res = misra_gries(a, 10)

x,y = list(res.keys()), list(res.values())

plt.plot(x, y)

plt.show()



In [ ]: