PythonHMM

PythonHMM is a python implementation of the Hidden Markov Model.

References

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

This class is an implementation of the Hidden Markov Model.

The instance of this class can be created by passing the given states, symbols and optional probability matrices.

If any of the probability matrices are not given, the missing matrics will be set to the initial uniform probability.

decode(sequence)

Decode the given sequence.

This method is an implementation of the Viterbi algorithm.

emit_prob(state, symbol)

Return the emission probability for symbol associated with the state.

If either the state or the symbol are not contained in this model, 0 is returned.

evaluate(sequence)

Use the forward algorithm to evaluate the given sequence.

learn(sequence, smoothing=0)

Use the given sequence to find the best state transition and emission probabilities.

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

start_prob(state)

Return the start probability of the given state.

If state is not contained in the state set of this model, 0 is returned.

states()

Return the state set of this model.

states_number()

Return the number of states.

symbols()

Return the symbol set of this model.

symbols_number()

Return the number of symbols.

trans_prob(state_from, state_to)

Return the probability that transition from state state_from to state state_to.

If either the state_from or the state_to are not contained in the state set of this model, 0 is returned.

hmm.train(sequences, delta=0.0001, smoothing=0)

Use the given sequences to train a HMM model. This method is an implementation of the EM algorithm.

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 is used to avoid zero probability, see learn().

Usage Example

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

from hmm import Model

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

sequence = ['walk', 'shop', 'clean', 'clean', 'walk', 'walk', 'walk', 'clean']
model = Model(states, symbols, start_prob, trans_prob, emit_prob)

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

License

Copyright (c) 2012, Chi-En Wu
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the organization nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Contribute

Found a bug? Have a good idea for improving PythonHMM?

Head over to PythonHMM’s github page and create a new ticket or fork.

Fork me on GitHub