How to create specific Viterbi Algorithm in Python for Homework?

584 Views Asked by At

So Basically for this homework, we're trying to use the Viterbi Algorithm to solve a hidden Markov model, I tried to base mine on others I found online but upon getting a hint from the teacher I'm overthinking it. I'm really good at the mathematical part and understanding how to do the problem on paper I just am horrible at translating it into python.

Below is the code I have so far that he helped me with his hints, I'm not sure what the next step is.

    import numpy as np

    def viterbi(obs):
        tmatr = np.array([[numpy.log2(0.7), 
                numpy.log2(0.2),numpy.log2(0.1)], 
                [numpy.log2(0.2), numpy.log2(0.5), numpy.log2(0.3)],[ 
                numpy.log2(0.1), 
                numpy.log2(0.3), numpy.log2(0.6)])
        omatr = np.array([[ numpy.log2(0.8), numpy.log2(0.2), 
                numpy.log2(0.0)],[ numpy.log2(0.2), numpy.log2(0.7), 
                numpy.log2(0.1)], 
                [numpy.log2(0.2), numpy.log2(0.5), numpy.log2(0.3)])

        pathdict={
        0: numpy.log2(.7),
        1: numpy.log2(.2),
        2: numpy.log2(.1),
        }

        probdict={
        0: (numpy.log2(.7)+ omatr[0,obs_seq[0]]),
        1: (numpy.log2(.2)+ omatr[1,obs_seq[0]]),
        2: (numpy.log2(.1)+ omatr[2,obs_seq[0]]),
        }

        templist=[]

        for t in range(1, 63):
             for i in range(0,2):
                 for k in range(0,2):
                    templist.append(pathdict[k,i]+
0

There are 0 best solutions below