Skip to content
/ hmmpy Public

Simple, fast, discrete hidden markov model modules for native python and cython.

Notifications You must be signed in to change notification settings

alkant/hmmpy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Summary

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.

Features

  • Supervised learning
  • Viterbi inference

Building the Cython module

Assuming a working installation of Cython, running:

python setup.py build_ext --inplace

will build the chmmpy module.

Basic usage

>>> 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)

About

Simple, fast, discrete hidden markov model modules for native python and cython.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages