Git Product home page Git Product logo

ex-4--aai's Introduction

Guna Sekhar.M

212221240014

EX. NO.4

DATE:

Implementation of Hidden Markov Model

Aim:

Construct a Python code to find the sequence of hidden states by the known sequence of observances using Hidden Markov Model. Consider two hidden states Sunny and Rainy with observable states,happy and sad.

Algorithm:

Step 1:Define the transition matrix, which specifies the probability of transitioning from one hidden state to another.
Step 2:Define the emission matrix, which specifies the probability of observing each possible observation given each hidden state.
Step 3:Define the initial probabilities, which specify the probability of starting in each possible hidden state.
Step 4:Define the observed sequence, which is the sequence of observations need to be analyzed.
Step 5:Initialize the alpha matrix with zeros, where each row represents a time step and each column represents a possible hidden state.
Step 6:Calculate the first row of the alpha matrix by multiplying the initial probabilities by the emission probabilities for the first observation.
Step 7:Loop through the rest of the observed sequence and calculate the rest of the alpha matrix by multiplying the emission probabilities by the sum of the product of the previous row of the alpha matrix and the corresponding row of the transition matrix.
Step 8:Calculate the probability of the observed sequence by summing the last row of the alpha matrix.
Step 9:Find the most likely sequence of hidden states by selecting the hidden state with the highest probability at each time step based on the alpha matrix.

Program:

import numpy as np
transition_matrix=np.array([[0.7,0.3],[0.4,0.6]])
initial_probabilities=np.array([0.5,0.5])
observed_sequence=np.array([1,1,1,0,0,1])
emisson_matrix=np.array([[0.1,0.9],[0.8,0.2]])
alpha=np.zeros((len(observed_sequence),len(initial_probabilities)))
alpha[0,:]=initial_probabilities*emisson_matrix[:,observed_sequence[0]]
for t in range(1,len(observed_sequence)):
  for j in range(len(initial_probabilities)):
    alpha[t,j]=emisson_matrix[j,observed_sequence[t]]*np.sum(alpha[t-1,:]*transition_matrix[:,j])
probability=np.sum(alpha[-1,:])
print("The probability of the observed sequence is:",probability)
most_likely_sequence=[]
for t in range(len(observed_sequence)):
  if(alpha[t,0] > alpha[t,1]):
    most_likely_sequence.append("sunny")
  else:
    most_likely_sequence.append('rainy')
print("The most likely sequence of weather states is:",most_likely_sequence)

Output:

probability of the observed sequence:

314364587-25e5e612-b52d-4c99-af0f-24f786d9a833

Print the most likely sequence of weather states:

314364657-edc63d0f-89e7-4611-a64e-6174cd226084

Result:

Thus Hidden Markov Model is implemented using python.

ex-4--aai's People

Contributors

gunasekhar159 avatar lavanyajoyce avatar

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.