결국 YAHMM라이브러리로 사용하기로 결정~!!

아래에 자세한 설명이 있고,

https://pypi.python.org/pypi/yahmm/0.1.1


'패턴인식 - 오일식 저'의 평가, 디코딩, 학습에 대한 설명도 있다.

*평가, 디코딩, 학습에 대한 예제가 뭔지 모르는 사람은 wiki에도 있으니 확인 : http://en.wikipedia.org/wiki/Hidden_Markov_model#A_concrete_example

코드 설명 : https://github.com/jmschrei/yahmm/wiki


설치 방법도 쉽다.

그냥 

pip install yahmm

끝~!!


소스코드는 아래와 같다.

Baum-Welch 알고리즘도 구현이 됐다고 하는데 내가 책에서 봤던 것 처럼 최적화된 모델을 찾아준 건지는 모르겠다.

최적화된 모델을 찾았다면 그 모델에 대한  A, B, 초기확률 을 찾아줘야 할 것 같은데 그게 아니다.

이 부분에 대해서는 좀 더 공부를 해 봐야할 것 같다.


# rainy_sunny_hmm.py

# Contact: Jacob Schreiber

#   jmschreiber91@gmail.com


"""

Example rainy-sunny HMM using yahmm. Example drawn from the wikipedia HMM

article: http://en.wikipedia.org/wiki/Hidden_Markov_model describing what

Bob likes to do on rainy or sunny days.

"""


from yahmm import *

import random

import math


random.seed(0)


model = Model( name="Rainy-Sunny" )


# Emission probabilities

rainy = State( DiscreteDistribution({ 'walk': 0.1, 'shop': 0.4, 'clean': 0.5 }), name='rainy')

sunny = State( DiscreteDistribution({ 'walk': 0.6, 'shop': 0.3, 'clean': 0.1 }), name='sunny' )


# Add the states to the model

model.add_state( rainy )

model.add_state( sunny )


model.add_transition( model.start, rainy, 0.6 )

model.add_transition( model.start, sunny, 0.4 )


# Transition matrix, with 0.05 subtracted from each probability to add to

# the probability of exiting the hmm

model.add_transition( rainy, rainy, 0.7 )

model.add_transition( rainy, sunny, 0.3 )

model.add_transition( sunny, rainy, 0.4 )

model.add_transition( sunny, sunny, 0.6 )


# Finalize the model structure

model.bake( verbose=True )


# Lets call Bob every hour and see what he's doing!

# (aka build up a sequence of observations)

sequence = [ 'walk', 'walk', 'clean', 'shop']


print 'Evaluation : ', math.e**model.log_probability(sequence)

print 'Observation : ', sequence

logp, path = model.viterbi(sequence)


for i in range(4):

    if(i == 0):

        print 'Decoding : [', \

    

    if(path[i+1][0]==0):

        print "'Rainy'", \

        

    else:

        print "'Sunny'", \


print ']\n'


model.train( [sequence], algorithm='baum-welch' )

print math.e**model.log_probability(sequence)

print 'sequence : ', sequence

logp, path = model.viterbi(sequence)

print path


Posted by 공놀이나하여보세
,