Git Product home page Git Product logo

ex-4--aai's Introduction

NAME: Rakesh V

REGISTER NO: 212222110036

EX. NO.4

DATE: 11/03/2024

Implementation of Hidden Markov Model

Aim:

Construct a Python code to find the sequence of hidden states by the known sequence of observables using the 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 that we want to analyze.

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 the necessary packages

import numpy as np

Define the transition matrix,emission matrix,initial probabilitiesand observed sequence.

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]])

Initialize the alpha matrix

alpha=np.zeros((len(observed_sequence),len(initial_probabilities)))

Calculate the first row of the alpha matrix

alpha[0,:]=initial_probabilities*emisson_matrix[:,observed_sequence[0]]

Loop through the rest of the observed sequence and calculate the rest of the alpha matrix

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])

Calculate the probability of the observed sequence

probability=np.sum(alpha[-1,:])

Print the probability of the observed sequence

print("The probability of the observed sequence is:",probability)

Find the most likely sequence of weather states given the observed sequence

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

print("The most likely sequence of weather states is:",most_likely_sequence),

Output:

exp5

Result:

Thus, the Hidden Markov Model to identify the sequence of Hidden states is executed successfully

ex-4--aai's People

Contributors

lavanyajoyce avatar rakeshcoder2004 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.