阅览此博客自己实现的Python代码
https://blog.csdn.net/weixin_35479108/article/details/87894136
# this is for practice 9.1
import numpy as np
theta = np.array([0.4, 0.6, 0.7])
Y = np.array([1,1,0,1,0,0,1,0,1,1])
miu = np.zeros(Y.shape[0])
for j in range(2):
# E step
for i in range(Y.shape[0]):
temp = theta[0]*theta[1]**Y[i]*(1-theta[1])**(1-Y[i])
temp1 = (1 - theta[0])*theta[2]**Y[i]*(1-theta[2])**(1-Y[i])
miu[i] = temp / (temp + temp1)
# M step
theta[0] = 1.0 / Y.shape[0] * sum(miu)
theta[1] = sum(miu * Y) / sum(miu)
theta[2] = sum((1 - miu)*Y) / sum(1-miu)
print(theta)
'''
output
迭代 pi p q
1 [ 0.40641711 0.53684211 0.64324324]
2 [ 0.40641711 0.53684211 0.64324324]
'''
# this is for practice 9.3
import numpy as np
from scipy.stats import norm
y = np.array([-67,-48, 6, 8, 14, 16, 23, 24, 28, 29, 41, 49, 56, 60, 75])
K = 2 # 两个高斯
N = 15#y有15个数据
#参数初始化
mu = np.array([0.5, 0.5])
sg = np.array([1.0, 1.0]) * 10
al = np.array([0.5, 0.5])
for i in range(10):
gm = np.zeros((N,K))
# E 步
for j in range(N):
for k in range(K):
gm[j,k] = al[k] * norm(mu[k],sg[k]).pdf(y[j])
gm[j,:] /= sum(gm[j,:]) #gm[j,:] = gm[j,:] /sum(gm[j,:])
# M 步
mu2 = y.dot(gm) / sum(gm)
al2 = sum(gm) / N
sg2 = np.zeros((2,))
sg2[0] = sum(gm[:,0] * (y - mu[0])**2) / sum(gm[:,0])
sg2[1] = sum(gm[:,1] * (y - mu[1])**2) / sum(gm[:,1])
if sum((mu - mu2)**2 + (sg - sg2)**2 + (al - al2)**2) < 0.01:
break
mu = mu2
sg = sg2
al = al2
print("迭代次数: ",i+1,"\n","mu = ",mu,"\n","sg = ",sg,"\n","al = ",al,"\n")
'''
output
迭代次数: 1
mu = [ 20.93333333 20.93333333]
sg = [ 1747.18333333 1747.18333333]
al = [ 0.5 0.5]
迭代次数: 2
mu = [ 20.93333333 20.93333333]
sg = [ 1329.66222222 1329.66222222]
al = [ 0.5 0.5]
'''