EM solution of mixed Gaussian model (mixtures of Gaussians) and Python implementation source code

Source: Internet
Author: User
Tags mixed

Today we bring the solution of EM derivation for mixed Gaussian model.








All codes are as follows!

def Ndimensiongaussian (X_vector,u_mean,covariancematrix): #X =numpy.mat (x_vector) x=x_vector D=numpy.shape (X) [0] #U =numpy.mat (U_mean) U=u_mean #CM =numpy.mat (covariancematrix) Cm=covariancematrix y=x-u temp=y.transpose ( ) * CM. I * Y result= (1.0/((2*NUMPY.PI) * * (D/2))) * (1.0/(Numpy.linalg.det (CM) **0.5)) *numpy.exp ( -0.5*temp) return resultdef Cal             Mean (x): D,n=numpy.shape (x) Meanvector=numpy.mat (Numpy.zeros ((d,1))) for D in range (d): for n in range (n): meanvector[d,0] + = X[d,n] meanvector[d,0]/= float (n) return meanvectordef calcovariance (X,MV): D , N=numpy.shape (X) Cov=numpy.mat (Numpy.zeros ((d,d))) for N in range (n): temp=x[:,n]-mv CoV + = Temp*temp . Transpose () CoV/= float (N) return covdef Calenergy (Xn,pik,uk,cov): D,n=numpy.shape (Xn) D_k,k=numpy.shape (U        k) if D!=d_k:print (' dimension not Equal, Break ') return energy=0.0 for N_iter in range (n):      Temp=0   For K_iter in range (k): temp + = pik[0,k_iter] * Ndimensiongaussian (Xn[:,n_iter],uk[:,k_iter],cov[k_iter]) Energy + = Numpy.log (temp) return float (energy) def Sequentialemformixgaussian (inputdata,k): #初始化piK pi_cof=nu Mpy.mat (Numpy.ones ((1,k)) * (1.0/float (K))) X=numpy.mat (Inputdata) X_mean=calmean (X) print (X_mean) X_cov=calcov Ariance (X,x_mean) print (X_cov) #初始化uK, where column K represents the mean vector #X为D维 of the K-Gaussian function, N sample points D,n=numpy.shape (X) print (d,n) UK =numpy.mat (Numpy.zeros ((d,k))) for D_iter in range (D): For K_iter in Range (K): uk[d_iter,k_iter] = X_ mean[d_iter,0] + ( -1) **k_iter + ( -1) **d_iter print (UK) #初始化k个协方差矩阵的列表 list_cov=[] for K_iter in range (k) : List_cov.append (Numpy.mat (Numpy.eye (x[:,0].size))) print (List_cov) list_cov_new=copy.deepcopy (List_cov ) Rznk=numpy.mat (Numpy.zeros ((n,k)) Denominator=numpy.mat (Numpy.zeros ((n,1))) Rznk_new=numpy.mat (Numpy.zeros (N, K)) Nk=0.5*numpy. MAT (Numpy.ones (1,k)) print (Nk) Nk_new=numpy.mat (Numpy.zeros ((1,k))) Uk_new=numpy.mat (Numpy.zeros ((d,k))) p I_cof_new=numpy.mat (Numpy.zeros ((1,k))) for N_iter in range (1,n): #rZnk =pi_k*gaussian (Xn|uk,cov_k)/sum (pi_j* Gaussian (Xn|uj,cov_j)) for K_iter in range (k): rznk_new[n_iter,k_iter] = pi_cof[0,k_iter] * Ndimensionga             Ussian (X[:,n_iter],uk[:,k_iter],list_cov[k_iter]) denominator[n_iter,0] + = Rznk_new[n_iter,k_iter] For K_iter in range (k): Rznk_new[n_iter,k_iter]/= denominator[n_iter,0] Print (' Rznk_new ', rznk_new [N_iter,k_iter], ' \ n ') for K_iter in range (k): nk_new[0,k_iter] = Nk[0,k_iter] + rznk_new[n_it Er,k_iter]-rznk[n_iter,k_iter] Print (' nk_new ', nk_new, ' \ n ') ############# #当前有 (n_iter+1) sample ######### ################## Pi_cof_new[0,k_iter] = nk_new[0,k_iter]/float (n_iter+1) print (' Pi_cof_new ', pi_           Cof_new, ' \ n ') Uk_new[:,k_iter] = Uk[:,k_iter] + ((Rznk_new[n_iter,k_iter]-rznk[n_iter,k_iter])/float (nk_new[0,k_iter)) * (X[:,n_i            Ter]-uk[:,k_iter]) print (' Uk_new ', uk_new, ' \ n ') Temp = X[:,n_iter]-Uk_new[:,k_iter] List_cov_new[k_iter] = List_cov[k_iter] + ((Rznk_new[n_iter,k_iter]-rznk[n_iter,k_iter])/float (Nk_new[0,k_iter])) * (Temp*temp.transpose ()-list_cov[k_iter]) print (' List_cov_new ', list_cov_new, ' \ n ') Rznk=co Py.deepcopy (rznk_new) pi_cof=copy.deepcopy (pi_cof_new) uk_new=copy.deepcopy (UK) List_cov=copy.deepcop Y (list_cov_new) print (Pi_cof,uk_new,list_cov) return pi_cof,uk_new,list_covdef Batchemformixgaussian (InputData,K,M    Axiter): #初始化piK Pi_cof=numpy.mat (Numpy.ones ((1,k)) * (1.0/float (K))) X=numpy.mat (Inputdata) X_mean=calmean (X) Print (X_mean) x_cov=calcovariance (x,x_mean) print (X_cov) #初始化uK, where the K column represents the mean vector #X为D维 of the K-Gaussian function, and N sample points D,n=num Py.shape (X) print (d,n)    Uk=numpy.mat (Numpy.zeros ((d,k))) for D_iter in range (D): For K_iter in Range (K): Uk[d_iter,k_iter ] = x_mean[d_iter,0] + ( -1) **k_iter + ( -1) **d_iter print (UK) #初始化k个协方差矩阵的列表 list_cov=[] for k_iter in RA Nge (K): List_cov.append (Numpy.mat (Numpy.eye (x[:,0].size))) print (List_cov) energy_new=0 Energy_old=ca                Lenergy (X,pi_cof,uk,list_cov) print (energy_old) currentiter=0 while true:currentiter + = 1 List_cov_new=[] Rznk=numpy.mat (Numpy.zeros (n,k)) Denominator=numpy.mat (Numpy.zeros ((n,1))) Nk=nump                Y.mat (Numpy.zeros ((1,k)) Uk_new=numpy.mat (Numpy.zeros ((d,k))) Pi_new=numpy.mat (Numpy.zeros ((1,K))) #rZnk =pi_k*gaussian (Xn|uk,cov_k)/sum (Pi_j*gaussian (Xn|uj,cov_j)) for N_iter in range (n): for k_i ter in range (K): rznk[n_iter,k_iter] = pi_cof[0,k_iter] * Ndimensiongaussian (x[:,n_iter],uk[:,k_iter],list_            Cov[k_iter])    denominator[n_iter,0] + = Rznk[n_iter,k_iter] for K_iter in range (k): Rznk[n_iter,k_iter                ]/= denominator[n_iter,0] #print ' rznk ', Rznk[n_iter,k_iter] #pi_new =sum (RZNK)            For K_iter in range (k): For N_iter in range (n): nk[0,k_iter] + = Rznk[n_iter,k_iter] Pi_new[0,k_iter] = nk[0,k_iter]/(float (N)) #print ' pi_k_new ', pi_new[0,k_iter] #uk_new = (1/sum ( RZNK)) *sum (RZNK*XN) for K_iter in range (k): For N_iter in range (n): Uk_new[:,k_iter]                    + = (1.0/float (Nk[0,k_iter])) *rznk[n_iter,k_iter]*x[:,n_iter] #print ' uk_new ', Uk_new[:,k_iter]                For K_iter in range (k): X_cov_new=numpy.mat (Numpy.zeros ((d,d))) for N_iter in range (n): Temp = X[:,n_iter]-Uk_new[:,k_iter] x_cov_new + = (1.0/float (nk[0,k_iter))) *rznk[n_iter,k_iter] * Tem    P * Temp.transpose ()        #print ' x_cov_new ', x_cov_new list_cov_new.append (x_cov_new) Energy_new=calenergy (x,pi_ne W,uk_new,list_cov) print (' Energy_new ', energy_new) #print pi_new #print uk_new #print List_cov _new if Energy_old>=energy_new or currentiter>maxiter:uk=copy.deepcopy (uk_new) pi_cof= Copy.deepcopy (pi_new) list_cov=copy.deepcopy (list_cov_new) Break Else:uk=copy.dee Pcopy (uk_new) pi_cof=copy.deepcopy (pi_new) list_cov=copy.deepcopy (list_cov_new) Energy_ol D=energy_new return Pi_cof,uk,list_cov


EM solution of mixed Gaussian model (mixtures of Gaussians) and Python implementation source code

Related Article

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.