Coder Social home page Coder Social logo

hmm's Introduction

hmm

An implementation of hidden Markov model in Python.
The Baum-welch and Viterbi algorithms for discrete emissions are implemented so far. It is a generic implementation, so one may need to write some wrapper to apply some real data.

Usage

Configure three parameters, which are transition, emission and initial probabilities. It is assumed that these are instances of numpy.array class. All emissions should be represented as integer, which are the indices of emission probability matrix.

import numpy as np
import hmm

t = np.array([[0.55, 0.45], [0.4, 0.6]])
e = np.array([[0.6, 0.3, 0.1], [0.1, 0.4, 0.5]])
i = np.array([0.4, 0.6])
h = hmm.HMM(t, e, i)

observations = np.array([[2, 2, 2, 2, 1, 0, 0, 1, 1, 1, 0, 2],
        [2, 2, 2, 1, 2, 1, 0, 0, 0, 1, 1, 2, 1, 2],
        [1, 0, 2, 2, 0, 0, 1, 1, 1, 0, 2, 0, 2, 0]])
h.baum_welch(observations, iter_limit=1000, 
        threshold=1e-5, pseudocounts=[0, 1e-4, 0])

path, l = h.viterbi([2, 2, 1, 0, 0, 2, 1, 1, 1, 0, 0])

Using multiprocessing module, you can speed up the calculation.

import numpy as np
import hmm_mp

... # Configure parameters t, e, i

h = hmm_mp.MultiProcessHMM(t, e, i, worker_num=4)

h.baum_welch(observations)

This module also offers importing an XML file of GHMM.

import hmm

(t, e, i) = hmm.load_ghmmxml('filename.xml')
h = hmm.MultiProcessHMM(t, e, i, 4)

Methods

  • baum_welch : A basic Baum-Welch algorithm. It requires a list of observations, where each observation is represented as a list of integers.

hmm's People

Contributors

choge avatar hankcs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.