EM algorithm for parameter estimation of Gaussian mixture model

Source: Internet
Author: User

1 #Coding:utf-82 ImportNumPy as NP3 4 defQQ (Y,alpha,mu,sigma,k,gama):#Calculate Q function5gsum=[]6n=Len (y)7      forKinchRange (K):8Gsum.append (Np.sum ([gama[j,k] forJinchrange (n)]))9     returnNp.sum ([G*np.log (AK) forG,akinchZip (Gsum,alpha)]) +TenNp.sum ([[[Np.sum] (gama[j,k]* (Np.log (1/np.sqrt (2*NP.PI))-np.log (Np.sqrt (Sigma[k])) -1/2/sigma[k]* (Y[j]-mu[k]) **2)) One                      forJinchRange (n)] forKinchRange (K)])#the formula in "statistical learning method" is 9.29 wrong A  - defPhi (Mu,sigma,y):#Calculate Phi -     return1/(Np.sqrt (2*np.pi*sigma) *np.exp (-(Y-MU) **2/2/Sigma)) the  - defGama (ALPHA,MU,SIGMA,I,K):#Calculate Gama -Sumak=np.sum ([[A*phi (M,s,i)] forA,m,sinchzip (alpha,mu,sigma)]) -     returnAlpha[k]*phi (mu[k],sigma[k],i)/Sumak +  - defDatan (length,k):#Generate Data +Y=[np.random.normal (5*j,j+5,length/k) forJinchrange (k)] A     returny at  - defEM (y,k,iter=1000):#Kmeans Algorithm -n =Len (y) -sigma=[10]*K -mu=Range (K) -Alpha=np.ones (K) inqqold,qqnew=0,0 -      forItinchRange (ITER): toGama2=Np.ones ((n,k)) +          forKinchRange (K): -              forIinchrange (n): thegama2[i,k]=Gama (alpha,mu,sigma,y[i],k) *          forKinchRange (K): $Sum_gama=np.sum ([Gama2[j,k] forJinchrange (n)])Panax NotoginsengMu[k]=np.sum ([Gama2[j,k]*y[j] forJinchRange (n)])/Sum_gama -Sigma[k]=np.sum ([gama2[j,k]* (Y[j]-mu[k]) **2 forJinchRange (n)])/Sum_gama thealpha[k]=sum_gama/N +qqnew=QQ (Y,ALPHA,MU,SIGMA,K,GAMA2) A         ifABS (qqold-qqnew) <0.000001: the              Break +Qqold=qqnew -     returnAlpha,mu,sigma $  $N = 500 -k=2 -Data=Datan (n,k) theY=np.reshape (data, (1, N)) -A,b,c =EM (y[0], k)Wuyi PrintA,b,c the #iter=180 - #[ 0.57217609 0.42782391] [4.1472879054766887, 0.72534713118155769] [44.114682884921415, 24.676116557533351] Wu  -Sigma = 6#data on the Web AboutMIU1 = 40 $MIU2 = 20 -X = Np.zeros ((1, N)) -  forIinchxrange (N): -     ifNp.random.random () > 0.5: AX[0, I] = NP.RANDOM.RANDN () * Sigma +miu1 +     Else: theX[0, I] = NP.RANDOM.RANDN () * Sigma +MIU2 -A,b,c =EM (x[0], k) $ PrintA,b,c the #iter=114 the #[ 0.44935959 0.55064041] [40.561782615819361, 21.444533254494189] [33.374144230703514, 51.459622219329155]

EM algorithm for parameter estimation of Gaussian mixture model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.