基础介绍,后5项为基础5元素

Q = ['q0', 'q1', 'q2', 'q3']              # 状态集合 States,共 N 种状态
V = ['v0', 'v1'] # 观测集合 Observations,共 M 种观测值
I = [ 'i{}'.format(i) for i in range(5) ] # 某个长度为 T 的状态序列,i_t 属于Q
O = [ 'o{}'.format(i) for i in range(5) ] # 状态序列对应的观测值序列,o_t 属于 V
A = [ a_ij ] # 转移概率 Transition Problity, a_ij = P( i_t+1 = q_j | i_t = q_i ) N*N
B = [ bj(o_t) ] # 发射概率 Emission Problity,b_ij = P( o_t = v_k | i_t = q_j ) N*M
Pi = [ P_i ] # 初识状态概率 P_i = P( i_1 = q_i )

基础5元素对应初始化

# Q = ['盒1', '盒2', '盒3']
Q = ['盒1', '盒2'] V = [ '红' , '黑' ]
# A = [ [ 0.2 , 0.3 , 0.5 ] ,
# [ 0 , 0.5 , 0.5 ] ,
# [ 0.4 , 0.2 , 0.2 ]]
A = [ [ 0.5 , 0.5 ] ,
[ 0.5 , 0.5 ]]
B = [ [ 0.3 , 0.7 ] ,
[ 0.5 , 0.5 ] ]
Pi = [ 0.5 , 0.5 ] def label_2_id(target):
dt = { v:k for k,v in enumerate(V)}
return [ dt[item] for item in target ]
# target = label_2_id( ['红','红','黑','红'] )
target = label_2_id( ['红','红'] )

BruteForce暴力算法,计算复杂度:

# 路径展示角度
def brute_force_algorithm( target = [] ,path = '' ,prob ='' , pre = -1):
ret = []
path_tmp = ''
prob_tmp = ''
for k,v in enumerate(Q):
path_tmp = '{}/{}'.format(path , v)
if prob == '':
prob_tmp = '{}/{},{}'.format(prob , Pi[k] , B[k][target[0]] )
else:
prob_tmp = '{}/{},{}'.format( prob , A[pre][k] , B[k][target[0]] )
if len(target) > 1:
tmp = brute_force_algorithm(target[1:] , path_tmp ,prob_tmp , pre = k )
ret.extend( tmp )
elif len(target) == 1:
ret.append([path_tmp , prob_tmp])
return ret
# 总概率展示角度
def brute_force_algorithm( target = [] ,path = '' ,prob = 0 , pre = -1):
ret = 0
for k,v in enumerate(Q):
prob_tmp = prob
path_tmp = '{}/{}'.format(path , v)
if pre == -1 :
prob_tmp += Pi[k] * B[k][target[0]] # joint 联合概率局部
else:
prob_tmp *= A[pre][k] * B[k][target[0]]
if len(target) > 1:
ret += brute_force_algorithm(target[1:] , path_tmp ,prob_tmp , pre = k )
elif len(target) == 1:
ret += prob_tmp
return ret

Forward 前向算法,时间复杂度:

def forward_algorithm( target = [] ):
prob = [ [ 0 for i in Q] for j in target ]
for t ,o in enumerate(target):
if t == 0 :
for i in range( len(Q) ):
prob[0][i] = Pi[i] * B[i][o]
else:
for id , q in enumerate(Q):
for k,v in enumerate(prob[t-1]):
print( v , A[k][id] , prob , prob[t][id] )
prob[t][id] += (v * A[k][id] * B[id][o] )
print(prob)
return prob

Backend后向算法,计算复杂度:

def backend_algorithm( target = [] ):
prob = [ [ 0.0 for i in Q] for j in target ]
length = len(target)
for t in range( length-1 , -1 , -1):
if t == length-1 :
for i in range( len(Q) ): # 后向计算有点问题
prob[t][i] = 1
else:
o = target[t+1]
for id , q in enumerate(Q):
if t == 0:
for k,v in enumerate(prob[t+1]):
prob[t][id] *= 1000
prob[t][id] += ( v * A[id][k] * B[k][o] ) * 1000
prob[t][id] /= 1000
else:
for k,v in enumerate(prob[t+1]):
prob[t][id] += v * A[id][k] * B[k][o]
for k,v in enumerate(prob[0]):
prob[0][k] = v * Pi[k] * B[k][target[0]]
return prob

HMM算法python实现的更多相关文章

  1. pageRank算法 python实现

    一.什么是pagerank PageRank的Page可是认为是网页,表示网页排名,也可以认为是Larry Page(google 产品经理),因为他是这个算法的发明者之一,还是google CEO( ...

  2. 常见排序算法-Python实现

    常见排序算法-Python实现 python 排序 算法 1.二分法     python    32行 right = length-  :  ]   ):  test_list = [,,,,,, ...

  3. kmp算法python实现

    kmp算法python实现 kmp算法 kmp算法用于字符串的模式匹配,也就是找到模式字符串在目标字符串的第一次出现的位置比如abababc那么bab在其位置1处,bc在其位置5处我们首先想到的最简单 ...

  4. KMP算法-Python版

                               KMP算法-Python版 传统法: 从左到右一个个匹配,如果这个过程中有某个字符不匹配,就跳回去,将模式串向右移动一位.这有什么难的? 我们可以 ...

  5. 压缩感知重构算法之IRLS算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  6. 压缩感知重构算法之OLS算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  7. 压缩感知重构算法之CoSaMP算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  8. 压缩感知重构算法之IHT算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  9. 压缩感知重构算法之SP算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

随机推荐

  1. scp复制发送文件夹到其他服务器上

    简述scp: scp是secure copy的简写,是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. 写法: scp [可选参数] 登录名@地址:源路径  目标路径. 举例:scp -r ...

  2. C语言可以在执行语句中间定义变量吗?

    C语言可以在执行语句中间定义变量吗? 例如:for(int i=0; i<5; i++){                                                     ...

  3. 【loj2538】 【PKUWC 2018】Slay the Spire dp

    我们不难发现,假设抽了x张攻击牌,y张强化牌,那么肯定是打出尽可能多张的强化牌后,再开始出攻击牌(当然最少要一张攻击牌) 我们设G(i,j)表示:所有(抽到的攻击牌牌数为i,打出的攻击牌牌数为j)的方 ...

  4. k8s驱逐篇(4)-kube-scheduler抢占调度驱逐

    介绍kube-scheduler抢占调度驱逐之前,先简单的介绍下kube-scheduler组件: kube-scheduler简介 kube-scheduler组件是kubernetes中的核心组件 ...

  5. [开源福利] FreeRedis 历时两年正式发布 v1.0 [C#.NET Redis Client]

    最近很多 .net QQ 群无故被封停,特别是 wpf 群几乎全军覆没.依乐祝的 .net6交流群,晓晨的 .net跨平台交流群,导致很多码友流离失所无家可归,借此机会使用一次召唤术,有需要的请加群: ...

  6. Logstash:Email output plugin 检查日志中是否还有某些错误信息并发送邮件报警

  7. 使用KVM的命令行方式安装centos7虚拟机

    前提条件 1.宿主机上已经安装KVM软件,参考网址:https://www.cnblogs.com/sanduzxcvbnm/p/15538881.html 2.已经上传centos7镜像到宿主机里 ...

  8. Solutions:安全的APM服务器访问

    转载自: https://blog.csdn.net/UbuntuTouch/article/details/105527468 APM Agents 访问APM server如果不做安全的设置,那么 ...

  9. 使用工具SecureCRT通过ssh远程连接Windows server 2019

    Windows Server 2019 开通SSH Server服务 在需要安裝的ws2019开启powershell,执行安装 openssh server 指令 Add-WindowsCapabi ...

  10. 轻松绕过waf,内网技术,Cobalt Strike4.4远控木马绕waf流量监控

    DNS隧道技术可以解决运控木马无法上线的问题,waf,防火墙对tcp,http,https等端口有流量检测,这个时候我们就可以使用隧道技术,让cs木马走DNS隧道,不仅可以检测不到而且也是一种反溯源的 ...