原文地址:expert search之一(em算法)作者:梁斌 以下是实验设计 设计一个一维的数据,14个数据,7个成一组,一个高斯分布,整体数据隐含了2个高斯分布. 系统最初给出第一个数属于z0的概率0.9,最后一个数属于在z1的概率0.9,其余数据不可判定. 迭代到最后,自动识别前7个数属于z0,后7个数属于z1. 实验代码 incl de "stdio.h" #incl de "stdlib.h" #incl de "stdint.h" #incl de "math.h" do ble pi = 3.; do ble ga ss(const do ble px,const do ble m ,const do ble csigma) { do ble val = sqrt(2*pi*csigma); val = 1val; val = val*exp(-pow(px-m ,2)(2*csigma)); ret rn val ; } int main(void) { do ble x[] = {1.5,1.8,1.9,2.0,2.1,2.2,2.5, 3.0,3.9,3.9,4.0,4.1,4.1,5.0}; int m = sizeof(x)sizeof(do ble); const int k = 2; do ble fei[k] ; do ble mean[k] ; do ble mao[k] ; do ble probability_x[][2]={{0.9,0.1},{0.5,0.5},{0.5,0.5},{0.5,0.5},{0.5,0.5},{0.5,0.5},{0.5,0.5},{0.5,0.5},{0.5,0.5},{0.5,0.5},{0.5,0.5},{0.5,0.5},{0.5,0.5},{0. 1,0.9}};  first make the 1.5 belong to z0 ,wihle 5.0 belong to z1 bool improve_large = false; int step = 0; do{ e-step for(int j=0;jk&&step!=0;j) { for(int i=0;im;i) { probability_x[i][j] = ga ss(x[i],mean[j],mao[j])*fei[j] ( ga ss(x[i],mean[0],mao[0])*fei[0]+ ga ss(x[i],mean[1],mao[1])*fei[1] ); } } m-step for(int j=0;jk;j) { fei[j] = 0.0; do ble s m_prob = 0.0; for(int i=0;im;i) { s m_prob += probability_x[i][j]; } fei[j] = s m_prob  m; } for(int j=0;jk;j) { mean[j] = 0.0; do ble weight = 0.0; do ble s m_prob = 0.0; for(int i=0;im;i) { weight += x[i]*probability_x[i][j]; s m_prob += probability_x[i][j]; } mean[j] = weights m_prob; } for(int j=0;jk;j) { mao[j] = 0.0; do ble weight = 0.0; do ble s m_prob = 0.0; for(int i=0;im;i) { weight += (x[i]-mean[j])*(x[i]-mean[j])*probability_x[i][j]; s m_prob += probability_x[i][j]; } mao[j] = weights m_prob; } printf("**%d*n",step); printf("%f,%f,%fn",fei[0],mean[0],mao[0]); printf("%f,%f,%fn",fei[1],mean[1],mao[1]); for(int i=0;im;i) { printf("%ft%fn", probability_x[i][0],probability_x[i][1]); } printf("**n"); }while(!improve_large && step100); ret rn 0;