作者:tony2502877947 | 来源:互联网 | 2023-07-28 14:39
自然语言处理中EM算法抛硬币的实现:importnumpyimportmathdefem_single(theta,O):#对于当前的模型求对应的期望值(估算步
自然语言处理中EM算法抛硬币的实现:
import numpy
import math
def em_single(theta,O):#对于当前的模型求对应的期望值(估算步骤)
# 三个值分别为选择硬币c1的概率、抛c1硬币为正面的概率、抛c2硬币为正面的概率。pi&#61;theta[0]h1&#61;theta[1]h2&#61;theta[2]new_pi&#61;0new_h1&#61;0new_h2&#61;0PB&#61;[]head&#61;[]for o in O:t&#61;len(o)cnt &#61; o.sum()#正面的数量head.append(cnt)#head&#61;[3.0,3,0]for o in O:t&#61;len(o)heads &#61; o.sum()#正面的数量tails&#61;t-heads#反面的数量u&#61;(pi*math.pow(h1,heads)*math.pow(1-h1,tails))/(pi*math.pow(h1,heads)*math.pow(1-h1,tails)&#43;(1-pi)*math.pow(h2,heads)*math.pow(1-h2,tails))PB.append(u)l&#61;len(PB)new_pi&#61;sum(PB)/lfor i in range(l):new_h1&#43;&#61;PB[i]*head[i]/tnew_h2&#43;&#61;(1-PB[i])*head[i]/tnew_h1&#61;new_h1/sum(PB)new_h2&#61;new_h2/(l-sum(PB))#因为有c1 c2两种可能&#xff0c;总观测数减去c1的即为c2的return [new_pi,new_h1,new_h2]def em(theta,Y,tol):#最大化步骤j&#61;0while j<1000:new_theta&#61;em_single(theta,Y)change&#61;numpy.abs(new_theta[0] - theta[0])if change,,,}[0,0,0],[1,1,1],[0,0,0]])theta&#61;[0.3,0.3,0.6]t&#61;len(theta)print("初始参数为:")print(theta)theta&#61;em(theta,O,1e-15)
输出&#xff1a;
初始参数为:
[0.3, 0.3, 0.6]
迭代后的参数为:
[0.3737649610410474, 0.0680206318504191, 0.7578245253976398]
迭代后的参数为:
[0.4859367224237726, 0.0004438948678989641, 0.9722232981496766]
迭代后的参数为:
[0.49998864947922056, 8.997364138533166e-11, 0.9999772993837971]
迭代后的参数为:
[0.49999999999999417, 7.2837620757937955e-31, 0.9999999999999885]
迭代后的参数为:
[0.5, 3.864268131526994e-91, 1.0]