This is a simple implementation of discrete hidden markov models for Python > 2.5. Both pure Python and Cython implementations are maintained. The Cython module is 10x (learning) to 128x (inference) faster than the Python implementation, but you'll need to compile it. Also, the Cython module trades off memory usage for speed by caching the dynamic program array between runs.
- Supervised learning
- Viterbi inference
Assuming a working installation of Cython, running:
python setup.py build_ext --inplace
will build the chmmpy module.
>>> from hmmpy import hmm
>>> from chmmpy import hmm as chmm
>>> m=hmm(2, 2)
>>> cm=chmm(2,2)
>>> observations=[[0,0,0,0,0,1,1,1,1,1,0,1,0,0,0,1,0,1,1,1,1],[0,0,0,0,1,0,1,1,0,1,1,0,0,1,0,0,1,1,1,1,0,0,1,0,0]]
>>> ground_truths=[[0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1],[0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0]]
>>> m.learn(observations, ground_truths)
>>> cm.learn(observations, ground_truths)
>>> m.viterbi(observations[1])
([0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0], -21.944735122525493)
>>> cm.viterbi(observations[1])
([0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0], -21.944734573364258)