栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

HMM viterbi

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HMM viterbi

import numpy as np
states=np.array([0,1]) #rainy--0  sunny--1
obser=np.array([0,1,2]) # k walk-0 shop-1 clean-2
start=np.array([0.6,0.4])
transition_probability=np.array([[0.7,0.3],
                                 [0.4,0.6]])
                               
emission_probability=np.array([[0.1,0.4,0.5],
                               [0.6,0.3,0.1]])
                            
weather_sun=np.zeros([2,7])
weather_rain=np.zeros([2,7])

for i in range(0,2):
    weather_rain[i,0]=start[i]*emission_probability[i,0]
    weather_sun[i,0]=start[i]*emission_probability[i,0]

j=1
for k in range(1,2):
    for i in range(0,2):
        weather_rain[i,j]=weather_rain[i,j-1]*transition_probability[i,0]*emission_probability[0,k]
        weather_sun[i,j]=weather_sun[i,j-1]*transition_probability[i,1]*emission_probability[1,k]
    weather_rain[:,j+2]=np.max(weather_rain[:,j])
    weather_sun[:,j+2]=np.max(weather_sun[:,j])  

    if weather_rain[0,j]>weather_rain[1,j]:
        weather_rain[0,j+1]=weather_rain[0,j]#np.max(weather_rain[:,j])
        weather_rain[1,j+1]=0
    if weather_rain[0,j]weather_sun[1,j]:
        weather_sun[0,j+1]=np.max(weather_sun[:,j])
        weather_sun[1,j+1]=0
    if weather_sun[0,j]weather_rain[1,j]:
        weather_rain[0,j+1]=np.max(weather_rain[:,j])
        weather_rain[1,j+1]=0
    if weather_rain[0,j]weather_sun[1,j]:
        weather_sun[0,j+1]=np.max(weather_sun[:,j])
        weather_sun[1,j+1]=0
    if weather_sun[0,j]weather_sun[0,6]:
#     print("clean:rain")
#for j in range(5,-1,-3):

#clean shop
weather_day=np.where(weather_all==np.max(weather_all[:,5]))
a=weather_day[0][0]
#print(a)
if a==3 or a==2:
    print("clean:sunny")
    weather_all1=np.delete(weather_all,0,axis=0)
    weather_all2=np.delete(weather_all1,0,axis=0)
    print("shop:sunny")
if a==0 or a==1:
    print("clean:rainy")
    weather_all1=np.delete(weather_all,2,axis=0)
    weather_all2=np.delete(weather_all1,2,axis=0)
    print("shop:rainy")
else:
    print("error")

#walk
weather_day=np.where(weather_all==np.max(weather_all[:,0]))
a=weather_day[0][0]
if a==0:
    print("walk:rainy")
if a==1:
    print("walk:sunny")
else:
    print("error")

运行结果如下

clean:rainy
shop:rainy
walk:sunny
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/294104.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号