维特比算法

统计学习方法 (李航) 维特比算法例题 的代码实现, (HMM 预测)

import numpy as np

num_hidden_states = 3
num_observations = 2 # 红, 黑
obs_map = {'红': 0, '白': 1}
# matrix[t-1, t] ===> t-1 --> t
transition_matrix = np.array([[.5, .2, .3],
                              [.3, .5, .2],
                              [.2, .3, .5]])

# state --> obs, prob
observation_matrix = np.array([[.5, .5],
                               [.4, .6],
                               [.7, .3]])

pi = np.array([.2, .4, .4])

# 红-->0, 白-->1
observation_sequence = ['红', '白', '红']

# viterbi
# initialize P(h1, o1) = p(h1) * p(o1|h1)
delta = pi * observation_matrix[:, obs_map[observation_sequence[0]]]
psi = np.zeros(shape=[len(observation_sequence), num_hidden_states],
               dtype=np.uint32)

for i in range(1, len(observation_sequence)):
    delta = np.reshape(delta, newshape=[-1, 1])
    P = delta * transition_matrix * np.reshape(
        observation_matrix[:, obs_map[observation_sequence[i]]],
        newshape=[1, -1])
    delta = np.max(P, axis=0)
    psi[i] = np.argmax(P, axis=0)

last_state = np.argmax(delta.reshape(-1))

# state 为 0, 1, 2, 所以打印出来是 2-->2-->2, 书中是 3-->3-->3
for t in reversed(range(len(observation_sequence))):
    print(last_state, end='-->')
    last_state = psi[t, last_state]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值