统计学习方法李航版第十章部分课后习题python答案

如题。

习题10.1-10.2

python代码

# -*- coding: utf-8 -*-
"""
Created on Sun Oct 20 12:46:42 2019

@author: Administrator
"""
import numpy as np


def backword(A, B, PI, O):
    T  = O.shape[0]
    # initialization
    beta = np.ones(shape = PI.shape[0]) # zeros()
    # main
    for t in range(T - 1, 0, -1): # python 是 start end step!
        new_beta = np.ones(shape = PI.shape[0])
        for i in range(beta.size):
            new_beta[i] = np.dot(A[i,:] * B[:,O[t]], beta)                 
        beta = new_beta
#        print("beta = ", beta)
    return sum(PI * B[:,O[0]] * beta), beta


def forword(A, B, PI, O):
    T  = O.shape[0]
    alpha = PI * B[:,O[0]]
    for t in range(T - 1):
        new_alpha = np.zeros(3)
        for i in range(3):
            new_alpha[i] = sum(alpha*A[:,i])*B[i][O[t+1]]
        alpha = new_alpha
    return alpha


# const seting
    
A = np.array([0.5, 0.2, 0.3, 0.3, 0.5,0.2,0.2,0.3,0.5]).reshape(3,3)
B = np.array([0.5, 0.5, 0.4, 0.6,0.7,0.3]).reshape(3,2)
PI = np.array([0.2, 0.4, 0.4])
O = np.array([0, 1, 0, 1])
res, beta = backword(A, B, PI, O)
print('10.1题的答案: \n', res)
    
A = np.array([0.5, 0.1, 0.4, 0.3, 0.5,0.2,0.2,0.2,0.6]).reshape(3,3)
B = np.array([0.5, 0.5, 0.4, 0.6,0.7,0.3]).reshape(3,2)
PI = np.array([0.2, 0.3, 0.5])
O = np.array([0, 1, 0, 0, 1, 0, 1, 1])
print('\n10.2题的答案: ')
res, beta = backword(A, B, PI, O)
print('后向的结果 beta = ', beta)
alpha = forword(A, B, PI, O)
print('前向的结果 alpha = ', alpha)
gamma = alpha[-1] * beta[-1] / (sum(alpha * beta))
print(gamma)
'''
10.1题的答案: 
 0.06009079999999999

10.2题的答案: 
后向的结果 beta =  [0.00632569 0.00684706 0.00577855]
前向的结果 alpha =  [0.00132502 0.00121063 0.00094105]
0.2459610979231905
'''

习题10.3

维特比算法求最优路径

import numpy as np
# const setting
A = np.array([0.5, 0.2, 0.3, 0.3, 0.5,0.2,0.2,0.3,0.5]).reshape(3,3)
B = np.array([0.5, 0.5, 0.4, 0.6,0.7,0.3]).reshape(3,2)
PI = np.array([0.2, 0.4, 0.4])
T  = 4
O = np.array([0, 1, 0, 1])

# initialization
delta = np.zeros(shape=(O.shape[0], 3))
psis = np.zeros(shape=(O.shape[0], 3))

# main
delta[0,:] = PI * B[:,O[0]] 
psis[0,:] = np.zeros(3)
for t in range(1, T):
    new_delta = np.zeros(shape=3)
    old_delta = delta[t - 1, :]
    for i in range(3):
        temp = old_delta * A[:, i] * B[i, O[t]]
        new_delta[i] = temp.max()
        psis[t, i] = temp.argmax()
    delta[t, :] = new_delta
print('psis = \n', psis)
idx = []
idx.append(delta[-1,:].argmax())
idx.append(psis[-1, int(idx[-1])])
idx.append(psis[-2, int(idx[-1])])
idx.append(psis[-3, int(idx[-1])])
print(np.array(idx[::-1], dtype=int)+1) 
'''
输出:
[3 2 2 2]
'''
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值