GithubHelp home page GithubHelp logo

yuwenlidao / pythonhmm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jason2506/pythonhmm

0.0 2.0 0.0 181 KB

A simple Python implementation of the Hidden Markov Model.

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

pythonhmm's Introduction

PythonHMM

PythonHMM is a python implementation of the Hidden Markov Model.

Usage

To use PythonHMM, you must import the hmm module.

import hmm

Then, you can create an instance of Model by passing the states, symbols, and (optional) probability matrices.

states = ('rainy', 'sunny')
symbols = ('walk', 'shop', 'clean')

start_prob = {
    'rainy' : 0.5,
    'sunny' : 0.5
}

trans_prob = {
    'rainy': { 'rainy' : 0.7, 'sunny' : 0.3 },
    'sunny': { 'rainy' : 0.4, 'sunny' : 0.6 }
}

emit_prob = {
    'rainy': { 'walk' : 0.1, 'shop' : 0.4, 'clean' : 0.5 },
    'sunny': { 'walk' : 0.6, 'shop' : 0.3, 'clean' : 0.1 }
}

model = hmm.Model(states, symbols, start_prob, trans_prob, emit_prob)

Now, you can evaluate and decode the given sequence:

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

print model.evaluate(sequence)
print model.decode(sequence)

You can also using the given sequences (a list of (state list, symbol list) pair) to train a model:

sequences = [
    (state_list1, symbol_list1),
    (state_list2, symbol_list2),
    ...
    (state_listN, symbol_listN),
]

model = hmm.train(sequences)

The train function also has two optional arguments, delta and smoothing.

The delta argument (which is defaults to 0.0001) specifies that the learning algorithm will stop when the difference of the log-likelihood between two consecutive iterations is less than delta.

The smoothing argument (which is defaults to 0) is the smoothing parameter of the additive smoothing to avoid zero probability.

License

This project is BSD-licensed. See LICENSE file for more detail.

pythonhmm's People

Watchers

James Cloos avatar  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.