python HMM,向前向后的
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python HMM,向前向后的相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ===========================================
# 状態遷移行列
trans_matrix = {}
trans_matrix['init'] = {}
trans_matrix['init']['M'] = 0.8
trans_matrix['init']['F'] = 0.2
trans_matrix['M'] = {}
trans_matrix['M']['M'] = 0.1
trans_matrix['M']['F'] = 0.4
trans_matrix['M']['fin'] = 0.5
trans_matrix['F'] = {}
trans_matrix['F']['M'] = 0.1
trans_matrix['F']['F'] = 0.5
trans_matrix['F']['fin'] = 0.4
# 出力確率行列
obs_matrix = {}
obs_matrix['M'] = {}
obs_matrix['M']['H'] = 0.4
obs_matrix['M']['S'] = 0.45
obs_matrix['M']['A'] = 0.15
obs_matrix['F'] = {}
obs_matrix['F']['H'] = 0.13
obs_matrix['F']['S'] = 0.6
obs_matrix['F']['A'] = 0.27
# # horie's matrix
# 状態遷移行列
# trans_matrix = {}
# trans_matrix['init'] = {}
# trans_matrix['init']['M'] = 0.6
# trans_matrix['init']['F'] = 0.4
# trans_matrix['M'] = {}
# trans_matrix['M']['M'] = 0.15
# trans_matrix['M']['F'] = 0.3
# trans_matrix['M']['fin'] = 0.55
# trans_matrix['F'] = {}
# trans_matrix['F']['M'] = 0.2
# trans_matrix['F']['F'] = 0.3
# trans_matrix['F']['fin'] = 0.5
# # 出力確率行列
# obs_matrix = {}
# obs_matrix['M'] = {}
# obs_matrix['M']['H'] = 0.3
# obs_matrix['M']['S'] = 0.65
# obs_matrix['M']['A'] = 0.05
# obs_matrix['F'] = {}
# obs_matrix['F']['H'] = 0.17
# obs_matrix['F']['S'] = 0.63
# obs_matrix['F']['A'] = 0.2
# ===========================================
# forward(0.1, 0.1, S, M)
def forward(prob_prev_M, prob_prev_F, current_state, current_obs):
answer = (prob_prev_M * trans_matrix['M'][current_state] + prob_prev_F * trans_matrix['F'][current_state]) * obs_matrix[current_state][current_obs]
# print (answer)
return answer
def backward(prob_next_M, prob_next_F, current_state, current_obs):
answer = (prob_next_M * trans_matrix[current_state]['M'] + prob_next_F * trans_matrix[current_state]['F']) * obs_matrix[current_state][current_obs]
# print (answer)
return answer
if __name__ == '__main__':
# 前向きアルゴリズム
# 回答欄の並びは
# M initM ans1 ans3
# F initF ans2 ans4
# 最終確率 ans_fin
forward_initM = trans_matrix['init']['M'] * obs_matrix['M']['A']
forward_initF = trans_matrix['init']['F'] * obs_matrix['F']['A']
ans1 = forward(forward_initM, forward_initF, 'M', 'S')
ans2 = forward(forward_initM, forward_initF, 'F', 'S')
ans3 = forward(ans1, ans2, 'M', 'H')
ans4 = forward(ans1, ans2, 'F', 'H')
ans_fin = ans3 * trans_matrix['M']['fin'] + ans4 * trans_matrix['F']['fin']
print ([forward_initM, forward_initF, ans1, ans2, ans3, ans4, ans_fin])
# 後ろ向きアルゴリズム
# 回答欄の並びは
# M ans1 ans3 back_initM
# F ans2 ans4 back_initF
# 最終(初期)確率 ans_init
back_initM = trans_matrix['M']['fin'] * obs_matrix['M']['H']
back_initF = trans_matrix['F']['fin'] * obs_matrix['F']['H']
ans3 = backward(back_initM, back_initF, 'M', 'S')
ans4 = backward(back_initM, back_initF, 'F', 'S')
ans1 = backward(ans3, ans4, 'M', 'A')
ans2 = backward(ans3, ans4, 'F', 'A')
ans_init = ans1 * trans_matrix['init']['M'] + ans2 * trans_matrix['init']['F']
print ([ans_init, ans1, ans2, ans3, ans4, back_initM, back_initF])
以上是关于python HMM,向前向后的的主要内容,如果未能解决你的问题,请参考以下文章
javascript [Palindrome]找到回文(一个拼写相同向前和向后的单词)的脚本#vanilla #script