Hidden Markov Model test in python

i like this post (click again to cancel)
0
i dont like this post (click again to cancel)

hello! for winter holiday i bought a kinect from microsoft and prime sense. now i want to do some gesture recognition with hidden markov model in python (my best language). i've found ghmm that seems to be quite good library but no more manteined.

i've read that sage include also ghmm so i'm going to try to ask here.

i've found this code in c# in [1] and i want to rewrite this simple test in python with ghmm.

--------------- c# code

int[][] sequences = new int[][]
{
   new int[] { 0,1,1,1,1,0,1,1,1,1 },
   new int[] { 0,1,1,1,0,1,1,1,1,1 },
   new int[] { 0,1,1,1,1,1,1,1,1,1 },
   new int[] { 0,1,1,1,1,1         },
   new int[] { 0,1,1,1,1,1,1       },
   new int[] { 0,1,1,1,1,1,1,1,1,1 },
   new int[] { 0,1,1,1,1,1,1,1,1,1 },
};
// For us, it can be obvious to see that the system is outputting sequences
// that always start with a zero and have one or more ones at the end.
// But lets try to fit a Hidden Markov Model to predict those sequences.


// Creates a new Hidden Markov Model with 3 states for
//  an output alphabet of two characters (zero and one)
HiddenMarkovModel hmm = new HiddenMarkovModel(2, 3);

// Try to fit the model to the data until the difference in
//  the average likelihood changes only by as little as 0.0001
hmm.Learn(sequences, 0.0001);

// Calculate the probability that the given
//  sequences originated from the model
double l1 = hmm.Evaluate(new int[] { 0,1 });      // 0.9999
double l2 = hmm.Evaluate(new int[] { 0,1,1,1 });  // 0.9166

double l3 = hmm.Evaluate(new int[] { 1,1 });      // 0.0000
double l4 = hmm.Evaluate(new int[] { 1,0,0,0 });  // 0.0000

double l5 = hmm.Evaluate(new int[] { 0,1,0,1,1,1,1,1,1 }); // 0.0342
double l6 = hmm.Evaluate(new int[] { 0,1,1,1,1,1,1,0,1 }); // 0.0342

my first try in python code and GHMM library: --------------- python code

from ghmm import *

training2 = [
   [ 0,1,1,1,1,1,1 ],
   [ 0,1,1,1 ],
   [ 0,1,1,1,1 ],
   [ 0,1, ],
   [ 0,1,1 ] ]

sigma = IntegerRange(0,2)
# null or random values, parameters should be overwritten by baum-welch!
A = [[0, 0, 0],[0, 0, 0], [0,0, 0]]
efair = [1.0/2] * 3
eloaded = [0.1, 0.1, 0.1]
B = [[0,1],[0,1],[1,0]]
pi = [0.3333] * 3

m = HMMFromMatrices(sigma, DiscreteDistribution(sigma), A, B, pi)
print m

train = SequenceSet(sigma, training2)
print train

m.baumWelch(train)
print m

tests = [
    [0,1],
    [0,1,1,1],
    [1,1],
    [1,0,0,0],
    [0,1,0,1,1,1,1,1,1],
    [0,1,1,1,1,1,1,0,1]
]

for t in tests:
    s = SequenceSet(sigma, [t])
    #print '-------- loglik', m.loglikelihood(s)
    print '-------- forward ',m.forward(s)
    #print '-------- viterbi ',m.viterbi(s)

--------------- python code

i don't have the same result of c# and my python results seems to be weird (all 0 and 1 . some hints? thanks in advance!

[1] www.codeproject.com/Articles/69647/Hidden-Markov-Models-in-C

asked Jan 23 '12

this post is marked as community wiki

This post is a wiki. Anyone with karma >150 is welcome to improve it.

updated Jan 23 '12

nkint gravatar image nkint
1 1 1
i like this answer (click again to cancel)
0
i dont like this answer (click again to cancel)

We used to include ghmm, but William rewrote the HMM code a while ago so that we don't depend on ghmm anymore. Please see http://www.sagemath.org/doc/reference/sage/stats/hmm/hmm.html

I also put up a simple example here: http://sage.cs.drake.edu/home/pub/96/

link

answered Jan 24 '12

this post is marked as community wiki

This post is a wiki. Anyone with karma >150 is welcome to improve it.

updated Jan 24 '12

Jason Grout gravatar image Jason Grout
3225 7 27 72

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
[hide preview]

Question tools

Tags:

Stats:

Asked: Jan 23 '12

Seen: 430 times

Last updated: Jan 24 '12

powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.