From the course: Fundamentals of Dynamic Programming

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Implementing the Viterbi algorithm in Python

Implementing the Viterbi algorithm in Python - Python Tutorial

From the course: Fundamentals of Dynamic Programming

Implementing the Viterbi algorithm in Python

- [Narrator] Using a representation of a hidden Markov model that we created in model.py, we can now make inferences using the Viterbi algorithm. But, before jumping into the Viterbi algorithm, let's see how we would use the model to implement the greedy algorithm that just looks at each observation in isolation. We'll use this version as a comparison. Here, our greedy function takes in a hidden Markov model, and a list of observations. Then, we just go through each observation, finding the state that most likely produced that observation based only on the emission probabilities B. Notice that we don't incorporate the initial or transition probabilities, which is fundamentally why the greedy algorithm doesn't produce the correct results. Okay, now on to the Viterbi algorithm. For this algorithm, we need to store path probabilities, which are the values of our V function. But to reconstruct our optimal path, we also need…

Contents