Giter VIP home page Giter VIP logo

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pythonhmm's Issues

math domain error

can you explain the problem for me ? I used the train function of the project, but it alway has math errors.
1
And I want to use the given state sequence and observation sequence to train the parameters of HMM.
My codes are as fellows:

# -*- coding: utf-8 -*-

import hmm

state1 = 'AAABNA'
state2= 'AAABNA'
state3= 'AAABNA'
symbol_list1 = [1,2,3,'a','.',1]
symbol_list2 = [1,3,2,'a','.',3]
symbol_list3 = [2,1,1,'a','.',1]
sequences = [(state1, symbol_list1),(state2, symbol_list2),(state3, symbol_list3)]
model = hmm.train(sequences)
print model._trans_prob

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.