# Code from Chapter 8 of Machine Learning: An Algorithmic Perspective # by Stephen Marsland (http://seat.massey.ac.nz/personal/s.r.marsland/MLBook.html) # You are free to use, change, or redistribute the code in any way you wish for # non-commercial purposes, but please maintain the name of the original author. # This code comes with no warranty of any kind. # Stephen Marsland, 2008 from pylab import * from numpy import * def GMM(): """ Fits two Gaussians to data using the EM algorithm """ N = 100 ion() y = 1.*zeros(N) # Set up data out1 = random.normal(6,1,N) out2 = random.normal(1,1,N) choice = random.rand(N) w = [choice>=0.5] y[w] = out1[w] w = [choice<0.5] y[w] = out2[w] clf() hist(y,fc='0.5') # Now do some learning # Initialisation mu1 = y[random.randint(0,N-1,1)] mu2 = y[random.randint(0,N-1,1)] s1 = sum((y-mean(y))**2)/N s2 = s1 pi = 0.5 # EM loop count = 0 gamma = 1.*zeros(N) nits = 20 ll = 1.*zeros(nits) while count