当前位置:   article > 正文

《自然语言处理学习之路》07 隐马尔科夫模型HMM工具包实战_hmmlearn包

hmmlearn包

书山有路勤为径,学海无涯苦作舟

一、hmmlearn 工具包

1.1 状态建模

import numpy as np
import numpy as np
  • 1
  • 2

设置隐藏状态:3个盒子

states = ["box1","box2","box3"]
n_states = len(states)
  • 1
  • 2

设置观测状态:红白两种球

observations = ["red","white"]
n_observations = len(observations)
  • 1
  • 2

设置模型参数:

start_probability = np.array([0.2,0.4,0.4]) # 初始矩阵  π

transition_probability = np.array([
    [0.5,0.2,0.3],
    [0.3,0.5,0.2],
    [0.2,0.3,0.5]
])                      # A 矩阵 ,隐藏状态的状态转移矩阵

emission_probability = np.array([
    [0.5,0.5],
    [0.4,0.6],
    [0.7,0.3]
])                     #  B  隐藏状态生成观测矩阵
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

建模

# 用于离散观测状态
model = hmm.MultinomialHMM(n_components=n_states) #设置隐藏状态个数

model.startprob_ = start_probability         #设置π
model.transmat_ = transition_probability     #设置A
model.emissionprob_ = emission_probability   #设置B

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

1.2 维特比算法计算隐藏状态出现最大概率

维特比算法,观测序列是[0,1,0] (红,白,红) 求解隐藏序列的最大可能性

seen =np.array([[0,1,0]]).T

logprob,box = model.decode(seen,algorithm="viterbi")
  • 1
  • 2
  • 3

得到的结果和之前算的是一样的

print(np.array(states)[box])
  • 1
['box3' 'box3' 'box3']
  • 1

predict也可以

box2 = model.predict(seen)
print(np.array(states)[box2])
  • 1
  • 2
['box3' 'box3' 'box3']
  • 1

得到观测序列的概率:In0.13022=-2.0385

print(model.score(seen))
  • 1
-2.038545309915233
  • 1

1.3 EM算法求解模型参数

tol:停机阈值n_iter :最大迭代次数n_components :隐藏状态数目

model2 = hmm. MultinomialHMM(n_components=n_states,n_iter=20,tol=0.01)
X2 =np. array([[o, 1, 0,1],[o, 0, 0, 1],[1,0,1,1]])
mode12.fit(X2)
print ('startprob_' , mode12.startprob_)
print('----')
print('transmat_' , model2.transmat_)
print ('------------—--')
print (emissionprob_' , mode12. emissionprob_)
print('--------------------—-------------------')
print ('score' , model2.score(X2))


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

二、HMM模型中文分词

2.1 构建观测状态,隐藏状态

在这里插入图片描述
观测:这几个字就是观测结果
状态:画/ 是1,不花/ 是0 ,每个词要不要分开。

##{B(词开头),M(词中),E(词尾),S(独字词)}{0,1,2,3}

data=[{
u"我要吃饭": "SSBE"},
{
u"天气不错":"BEBE"},
{
u"谢天谢地":"BMME"}]

#O:观察对象的集合,这里是字的集合,{我要吃饭天气不错谢天地}
#S:隐藏状态集合,这里是{BMES}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

现在已经知道观测和状态序列,可以求解出来模型参数

2.2 中文分词实现

import numpy as np
import warnings
from hmmlearn.hmm import MultinomialHMM as mhmm
data=[{
 u"我要吃饭":"SSBE"},
{
u"天气不错" : "BEBE"},
{
u"谢天谢地" : "BMME"}]
def prints(s):
    pass
    print(s)
def get_startprob():
    """get BMES matrix   
    """
    c=0
    c_map={"B":0,"M":0,"E":0,"S":0}
    #caculate the count
    for v in data :
        for key in v :
            value=v[key]
        c=c+1
        prints("value[0] is "+value[0])
        c_map[value[0]]=c_map[value[0]] +1
        prints("c_map[value[0]] is "+str(c_map[value[0]]) )
    res=[]
    for i in "BMES":
         res.append( c_map[i] / float(c))
    return res

def get_transmat():
    """get transmat of status
    """
    c=0
    #record BE:1,BB:2
    c_map={}
    for v in data :
        for key in v :
            value=v[key]
        
        prints("value[0] is "+value[0])
        for v_i in range(len(value)-1):
            couple=value[v_i:v_i+2]
            c_couple_source = c_map.get(couple,0)
            c_map[couple]=c_couple_source+1
            c=c+1         
        #c_map[value[0]]=c_map[value[0]] +1
        #prints("c_map[value[0]] is "+str(c_map[value[0]]) )
    prints("get_transmat's c_map is "+str(c_map))
    res=[]
    for i in "BMES":
         col=[]
         col_count=0
         for j in "BMES":
             col_count=c_map.get(i+j,0)+col_count          
         for j in "BMES":      
             col.append( c_map.get(i+j,0) / float(col_count))
         res.append(col)
    return res
def get_words():
    return u"我要吃饭天气不错谢天地"
def get_word_map():
    words=get_words()
    res={}
    for i in range(len(words)):
        res[words[i]]=i
    return res
def get_array_from_phase(phase):
    word_map=get_word_map()
    res=[]
    for key in phase:
        res.append(word_map[key])
    return res
def get_emissionprob():
    #get emmissionprob of status and observers
    c=0
    #record Bc=0
    #record B我:1,B吃:2
    c_map={}
    for v in data :      
        for key in v :
            k=key
            value=v[key]
        prints("value[0] is "+value[0])
        for v_i in range(len(value)):
            couple=value[v_i]+k[v_i]
            prints("emmition's couple is " + couple)
            c_couple_source = c_map.get(couple,0)
            c_map[couple]=c_couple_source+1
            c=c+1
    res=[]
    prints("emmition's c_map is "+str(c_map))
    words=get_words()
    for i in "BMES":
         col=[]
         for j in words:
             col.append( c_map.get(i+j,0) / float(c))
         res.append(col)
    return res
if( __name__ == "__main__"):
    # print("startprob is ",get_startprob())
    # print("transmat is " ,get_transmat())
    print("emissionprob is " , get_emissionprob())
    print("word map is ",get_word_map())
    # coding=utf-8
    warnings.filterwarnings("ignore")
    # import matplotlib.pyplot as plt
    startprob = np.array(get_startprob())
    print("startprob is ", startprob)
    transmat = np.array(get_transmat())
    print("transmat is ", transmat)
    emissionprob = np.array(get_emissionprob())
    print("emmissionprob is ", emissionprob)
    mul_hmm = mhmm(n_components=4)
    mul_hmm.startprob_ = startprob
    mul_hmm.transmat_ = transmat
    mul_hmm.emissionprob_ = emissionprob
    phase = u"我要吃饭谢天谢地"
    X = np.array(get_array_from_phase(phase))
    X = X.reshape(len(phase), 1)
    print("X is ", X)
    Y = mul_hmm.predict(X)
    print("Y is ", Y)
    # {B(词开头),M(词中),E(词尾),S(独字词)} {0,1,2,3}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
value[0] is S
emmition's couple is S我
emmition's couple is S要
emmition's couple is B吃
emmition's couple is E饭
value[0] is B
emmition's couple is B天
emmition's couple is E气
emmition's couple is B不
emmition's couple is E错
value[0] is B
emmition's couple is B谢
emmition's couple is M天
emmition's couple is M谢
emmition's couple is E地
emmition's c_map is {'S我': 1, 'S要': 1, 'B吃': 1, 'E饭': 1, 'B天': 1, 'E气': 1, 'B不': 1, 'E错': 1, 'B谢': 1, 'M天': 1, 'M谢': 1, 'E地': 1}
emissionprob is  [[0.0, 0.0, 0.08333333333333333, 0.0, 0.08333333333333333, 0.0, 0.08333333333333333, 0.0, 0.08333333333333333, 0.08333333333333333, 0.0], [0.0, 0.0, 0.0, 0.0, 0.08333333333333333, 0.0, 0.0, 0.0, 0.08333333333333333, 0.08333333333333333, 0.0], [0.0, 0.0, 0.0, 0.08333333333333333, 0.0, 0.08333333333333333, 0.0, 0.08333333333333333, 0.0, 0.0, 0.08333333333333333], [0.08333333333333333, 0.08333333333333333, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
word map is  {'我': 0, '要': 1, '吃': 2, '饭': 3, '天': 9, '气': 5, '不': 6, '错': 7, '谢': 8, '地': 10}
value[0] is S
c_map[value[0]] is 1
value[0] is B
c_map[value[0]] is 1
value[0] is B
c_map[value[0]] is 2
startprob is  [0.66666667 0.         0.         0.33333333]
value[0] is S
value[0] is B
value[0] is B
get_transmat's c_map is {'SS': 1, 'SB': 1, 'BE': 3, 'EB': 1, 'BM': 1, 'MM': 1, 'ME': 1}
transmat is  [[0.   0.25 0.75 0.  ]
 [0.   0.5  0.5  0.  ]
 [1.   0.   0.   0.  ]
 [0.5  0.   0.   0.5 ]]
value[0] is S
emmition's couple is S我
emmition's couple is S要
emmition's couple is B吃
emmition's couple is E饭
value[0] is B
emmition's couple is B天
emmition's couple is E气
emmition's couple is B不
emmition's couple is E错
value[0] is B
emmition's couple is B谢
emmition's couple is M天
emmition's couple is M谢
emmition's couple is E地
emmition's c_map is {'S我': 1, 'S要': 1, 'B吃': 1, 'E饭': 1, 'B天': 1, 'E气': 1, 'B不': 1, 'E错': 1, 'B谢': 1, 'M天': 1, 'M谢': 1, 'E地': 1}
emmissionprob is  [[0.         0.         0.08333333 0.         0.08333333 0.
  0.08333333 0.         0.08333333 0.08333333 0.        ]
 [0.         0.         0.         0.         0.08333333 0.
  0.         0.         0.08333333 0.08333333 0.        ]
 [0.         0.         0.         0.08333333 0.         0.08333333
  0.         0.08333333 0.         0.         0.08333333]
 [0.08333333 0.08333333 0.         0.         0.         0.
  0.         0.         0.         0.         0.        ]]
X is  [[ 0]
 [ 1]
 [ 2]
 [ 3]
 [ 8]
 [ 9]
 [ 8]
 [10]]
Y is  [3 3 0 2 0 1 1 2]

Process finished with exit code 0

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/739244
推荐阅读
相关标签
  

闽ICP备14008679号