您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Python如何實(shí)現(xiàn)隱馬爾可夫模型的前向后向算法,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
循環(huán)方式
import numpy as np def hmm_forward(Q, V, A, B, pi, T, O, p): """ :param Q: 狀態(tài)集合 :param V: 觀測(cè)集合 :param A: 狀態(tài)轉(zhuǎn)移概率矩陣 :param B: 觀測(cè)概率矩陣 :param pi: 初始概率分布 :param T: 觀測(cè)序列和狀態(tài)序列的長(zhǎng)度 :param O: 觀測(cè)序列 :param p: 存儲(chǔ)各個(gè)狀態(tài)的前向概率的列表,初始為空 """ for t in range(T): # 計(jì)算初值 if t == 0: for i in range(len(Q)): p.append(pi[i] * B[i, V[O[0]]]) # 初值計(jì)算完畢后,進(jìn)行下一時(shí)刻的遞推運(yùn)算 else: alpha_t_ = 0 alpha_t_t = [] for i in range(len(Q)): for j in range(len(Q)): alpha_t_ += p[j] * A[j, i] alpha_t_t.append(alpha_t_ * B[i, V[O[t]]]) alpha_t_ = 0 p = alpha_t_t return sum(p) # 《統(tǒng)計(jì)學(xué)習(xí)方法》書上例10.2 Q = [1, 2, 3] V = {'紅':0, '白':1} A = np.array([[0.5, 0.2, 0.3], [0.3, 0.5, 0.2], [0.2, 0.3, 0.5]]) B = np.array([[0.5, 0.5], [0.4, 0.6], [0.7, 0.3]]) pi = [0.2, 0.4, 0.4] T = 3 O = ['紅', '白', '紅'] p = [] print(hmm_forward(Q, V, A, B, pi, T, O, p)) # 0.130218
遞歸方式
import numpy as np def hmm_forward_(Q, V, A, B, pi, T, O, p, T_final): """ :param T_final:遞歸的終止條件 """ if T == 0: for i in range(len(Q)): p.append(pi[i] * B[i, V[O[0]]]) else: alpha_t_ = 0 alpha_t_t = [] for i in range(len(Q)): for j in range(len(Q)): alpha_t_ += p[j] * A[j, i] alpha_t_t.append(alpha_t_ * B[i, V[O[T]]]) alpha_t_ = 0 p = alpha_t_t if T >= T_final: return sum(p) return hmm_forward_(Q, V, A, B, pi, T+1, O, p, T_final) Q = [1, 2, 3] V = {'紅':0, '白':1} A = np.array([[0.5, 0.2, 0.3], [0.3, 0.5, 0.2], [0.2, 0.3, 0.5]]) B = np.array([[0.5, 0.5], [0.4, 0.6], [0.7, 0.3]]) pi = [0.2, 0.4, 0.4] T = 0 O = ['紅', '白', '紅'] p = [] T_final = 2 # T的長(zhǎng)度是3,T的取值是(0時(shí)刻, 1時(shí)刻, 2時(shí)刻) print(hmm_forward_(Q, V, A, B, pi, T, O, p, T_final))
循環(huán)方式
import numpy as np def hmm_backward(Q, V, A, B, pi, T, O, beta_t, T_final): for t in range(T, -1, -1): if t == T_final: beta_t = beta_t else: beta_t_ = 0 beta_t_t = [] for i in range(len(Q)): for j in range(len(Q)): beta_t_ += A[i, j] * B[j, V[O[t + 1]]] * beta_t[j] beta_t_t.append(beta_t_) beta_t_ = 0 beta_t = beta_t_t if t == 0: p=[] for i in range(len(Q)): p.append(pi[i] * B[i, V[O[0]]] * beta_t[i]) beta_t = p return sum(beta_t) # 《統(tǒng)計(jì)學(xué)習(xí)方法》課后題10.1 Q = [1, 2, 3] V = {'紅':0, '白':1} A = np.array([[0.5, 0.2, 0.3], [0.3, 0.5, 0.2], [0.2, 0.3, 0.5]]) B = np.array([[0.5, 0.5], [0.4, 0.6], [0.7, 0.3]]) pi = [0.2, 0.4, 0.4] T = 3 O = ['紅', '白', '紅', '白'] beta_t = [1, 1, 1] T_final = 3 print(hmm_backward_(Q, V, A, B, pi, T, O, beta_t, T_final)) # 0.06009
遞歸方式
import numpy as np def hmm_backward(Q, V, A, B, pi, T, O, beta_t, T_final): if T == T_final: beta_t = beta_t else: beta_t_ = 0 beta_t_t = [] for i in range(len(Q)): for j in range(len(Q)): beta_t_ += A[i, j] * B[j, V[O[T+1]]] * beta_t[j] beta_t_t.append(beta_t_) beta_t_ = 0 beta_t = beta_t_t if T == 0: p=[] for i in range(len(Q)): p.append(pi[i] * B[i, V[O[0]]] * beta_t[i]) beta_t = p return sum(beta_t) return hmm_backward(Q, V, A, B, pi, T-1, O, beta_t, T_final) jpgQ = [1, 2, 3] V = {'紅':0, '白':1} A = np.array([[0.5, 0.2, 0.3], [0.3, 0.5, 0.2], [0.2, 0.3, 0.5]]) B = np.array([[0.5, 0.5], [0.4, 0.6], [0.7, 0.3]]) pi = [0.2, 0.4, 0.4] T = 3 O = ['紅', '白', '紅', '白'] beta_t = [1, 1, 1] T_final = 3 print(hmm_backward_(Q, V, A, B, pi, T, O, beta_t, T_final)) # 0.06009
關(guān)于“Python如何實(shí)現(xiàn)隱馬爾可夫模型的前向后向算法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。