[英]How to Apply Viterbi Algorithm in Python
要在線嘗試維特比算法,您應該粘貼以下代碼
def viterbi(obs, states, start_p, trans_p, emit_p):
V=[{}]
for i in states:
V[0][i]=start_p[i]*emit_p[i][obs[0]]
# Run Viterbi when t > 0
for t in range(1, len(obs)):
V.append({})
for y in states:
(prob, state) = max((V[t-1][y0] * trans_p[y0][y] * emit_p[y][obs[t]], y0) for y0 in states)
V[t][y] = prob
for i in dptable(V):
print (i)
opt=[]
for j in V:
for x,y in j.items():
if j[x]==max(j.values()):
opt.append(x)
#the highest probability
h=max(V[-1].values())
print ('The steps of states are '+' '.join(opt)+' with highest probability of %s'%h)
#it prints a table of steps from dictionary
def dptable(V):
yield " ".join(("%10d" % i) for i in range(len(V)))
for y in V[0]:
yield "%.7s: " % y+" ".join("%.7s" % ("%f" % v[y]) for v in V)
states = ('Healthy', 'Fever')
observations = ('normal', 'cold', 'dizzy')
start_probability = {'Healthy': 0.6, 'Fever': 0.4}
transition_probability = {
'Healthy' : {'Healthy': 0.7, 'Fever': 0.3},
'Fever' : {'Healthy': 0.4, 'Fever': 0.6}
}
emission_probability = {
'Healthy' : {'normal': 0.5, 'cold': 0.4, 'dizzy': 0.1},
'Fever' : {'normal': 0.1, 'cold': 0.3, 'dizzy': 0.6}
}
viterbi(observations,
states,
start_probability,
transition_probability,
emission_probability)
到在線ide並在命令行python main.py
打印(如果您的文件名為main.py
)。 或者按編輯器上方的Execute
按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.