Professional Documents
Culture Documents
Implementation of Bayesian-
Network
Aim :
Algorithm:
Step 1:Import necessary libraries: pandas, networkx, matplotlib.pyplot, Bbn,
Edge, EdgeType, BbnNode, Variable, EvidenceBuilder, InferenceController
Step 2:Set pandas options to display more columns
Step 3:Read in weather data from a CSV file using pandas
Step 4:Remove records where the target variable RainTomorrow has
missing values
Step 5:Fill in missing values in other columns with the column mean
Step 6:Create bands for variables that will be used in the model
(Humidity9amCat, Humidity3pmCat, and WindGustSpeedCat)
Step 7:Define a function to calculate probability distributions, which go into
the Bayesian Belief Network (BBN)
Step 8:Create BbnNode objects for Humidity9amCat, Humidity3pmCat,
WindGustSpeedCat, and RainTomorrow, using the probs() function to
calculate their probabilities
Step 9:Create a Bbn object and add the BbnNode objects to it, along with
edges between the nodes
Step 10:Convert the BBN to a join tree using the InferenceController
Step 11:Set node positions for the graph
Step 12:Set options for the graph appearance
Step 13:Generate the graph using networkx
Step 14:Update margins and display the graph using matplotlib.pyplot
1 of 4 06-11-2023, 11:22
Firefox https://github.com/VARSHINI22009118/Bayesian-Network
Program:
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
from pybbn.graph.dag import Bbn
from pybbn.graph.edge import Edge, EdgeType
from pybbn.graph.jointree import EvidenceBuilder
from pybbn.graph.node import BbnNode
from pybbn.graph.variable import Variable
from pybbn.pptc.inferencecontroller import InferenceController #Set
Pandas options to display more columns
pd.options.display.max_columns=50
df=pd.read_csv('weatherAUS.csv', encoding='utf-8')
df=df[pd.isnull(df['RainTomorrow'])==False]
df=df.fillna(df.mean())
df['WindGustSpeedCat']=df['WindGustSpeed'].apply(lambda x: '0.<=40'
if x<=40 else
'1.40-50' if
40<x<=50 else '2.>50')
df['Humidity9amCat' ]=df[ 'Humidity9am'].apply(lambda x: '1.>60' if
x>60 else '0.<=60')
df['Humidity3pmCat']=df['Humidity3pm'].apply(lambda x: '1.>60' if
x>60 else '0.<=60')
print(df)
2 of 4 06-11-2023, 11:22
Firefox https://github.com/VARSHINI22009118/Bayesian-Network
normalize='index').sort_index().to_numpy().reshape(-1).tolist()
else: print("Error in Probability Frequency Calculations")
return prob
H9am = BbnNode(Variable(0, 'H9am', ['<=60', '>60']), probs(df,
child='Humidity9amCat'))
H3pm = BbnNode(Variable(1, 'H3pm', ['<=60', '>60']), probs(df,
child= 'Humidity3pmCat', parent1='Humidity9amCat'))
W =BbnNode(Variable(2, 'W', ['<=40', '40-50', '>50']), probs(df,
child='WindGustSpeedCat'))
RT = BbnNode(Variable(3, 'RT', ['No', 'Yes']), probs(df,
child='RainTomorrow', parent1='Humidity3pmCat',
parent2='WindGustSpeedCat'))
bbn= Bbn() \
.add_node(H9am) \
.add_node(H3pm) \
.add_node(W) \
.add_node(RT) \
.add_edge(Edge(H9am,H3pm, EdgeType.DIRECTED)) \
.add_edge(Edge(H3pm, RT, EdgeType.DIRECTED)) \
.add_edge(Edge(W,RT, EdgeType.DIRECTED))
join_tree =InferenceController.apply(bbn)
pos={0: (-1,-2), 1: (-1, 0.5), 2: (1, 0.5), 3:(0,-1)}
options ={
"font_size": 16,
"node_size": 4000,
"node_color": "white",
"edgecolors": "blue",
"edge_color": "red",
"linewidths": 5,
"width": 5,
}
n,d=bbn.to_nx_graph()
nx.draw(n, with_labels=True,labels=d, pos=pos, **options)
ax=plt.gca()
ax.margins (0.20)
plt.axis("off")
plt.show()
3 of 4 06-11-2023, 11:22
Firefox https://github.com/VARSHINI22009118/Bayesian-Network
Output:
Result:
Thus a Bayesian Network is generated using Python
4 of 4 06-11-2023, 11:22
Firefox https://github.com/VARSHINI22009118/Bayes-Classifier
Bayes-Classifier
Aim:
To Construct a Bayes Classifier to classiy iris dataset using Python.
Algorithm:
Input:
X: the training data, where each row represents a sample and each column
represents a feature.
y: the target labels for the training data.
X_test: the testing data, where each row represents a sample and each
column represents a feature.
Output:
1 of 2 06-11-2023, 11:23
Firefox https://github.com/VARSHINI22009118/Bayes-Classifier
Program:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
class BayesClassifier:
def __init__(self):
self.clf = GaussianNB()
def fit(self, X, y):
self.clf.fit(X, y)
def predict(self, X):
return self.clf.predict(X)
iris=load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data,
iris.target, test_size=0.3, random_state = 38)
clf = BayesClassifier()
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: ",accuracy)
Output:
Result:
Hence, Bayes classifier for iris dataset is implemented successfully
2 of 2 06-11-2023, 11:23
Firefox https://github.com/VARSHINI22009118/Ex-No.-3--Implementation-of...
Aim:
To implement the inference Burglary P(B| j,⥗m) in alarm problem by using
Variable Elimination method in Python.
Algorithm:
Step 1: Define the Bayesian Network structure for alarm problem with 5
random variables, Burglary,Earthquake,John Call,Mary Call and Alarm.
Step 2: Define the Conditional Probability Distributions (CPDs) for each variable
using the TabularCPD class from the pgmpy library.
Step 3: Add the CPDs to the network.
Step 4: Initialize the inference engine using the VariableElimination class from
the pgmpy library.
Step 5: Define the evidence (observed variables) and query variables.
Step 6: Perform exact inference using the defined evidence and query
variables.
Step 7: Print the results.
Program :
1 of 2 06-11-2023, 11:24
Firefox https://github.com/VARSHINI22009118/Ex-No.-3--Implementation-of...
cpd_burglary=TabularCPD(variable='Burglary',variable_card=2,values=
[[0.999],[0.001]])
cpd_earthquake=TabularCPD(variable='Earthquake',variable_card=2,values=
[[0.998],[0.002]])
cpd_alarm=TabularCPD(variable='Alarm',variable_card=2,values=
[[0.999,0.71,0.06,0.05],[0.001,0.29,0.94,0.95]],evidence=
['Burglary','Earthquake'],evidence_card=[2,2])
cpd_john_calls=TabularCPD(variable='JohnCalls',variable_card=2,values=
[[0.95,0.1],[0.05,0.9]],evidence=['Alarm'],evidence_card=[2])
cpd_marry_calls=TabularCPD(variable='MarryCalls',variable_card=2,values=
[[0.99,0.3],[0.01,0.7]],evidence=['Alarm'],evidence_card=[2])
network.add_cpds(cpd_burglary,cpd_earthquake,cpd_alarm,cpd_john_calls,cpd_marry_calls
inference=VariableElimination(network)
evidence={'JohnCalls':1,'MarryCalls':0}
query_variable='Burglary'
result=inference.query(variables=[query_variable],evidence=evidence)
print(result)
Output :
Result :
Hence the implementation of Exact Inference Method of Bayesian Network Is
implemented Successfully.
2 of 2 06-11-2023, 11:24
Firefox https://github.com/VARSHINI22009118/Ex-No.-4--Implementation-of...
Aim :
To construct a python program to implement approximate inference using Gibbs
Sampling.
Algorithm:
Step 1: Bayesian Network Definition and CPDs:
(i) Define the Bayesian network structure using the BayesianNetwork class from
pgmpy.models.
(ii) Define Conditional Probability Distributions (CPDs) for each variable using the
TabularCPD class.
(iii) Add the CPDs to the network.
Step 2: Printing Bayesian Network Structure:
(i) Print the structure of the Bayesian network using the print(network) statement. Step
3: Graph Visualization:
(i)Import the necessary libraries (networkx and matplotlib).
(ii)Create a directed graph using networkx.DiGraph().
(iii)Define the nodes and edges of the graph.
(iv)Add nodes and edges to the graph.
(v) Optionally, define positions for the nodes.
(vi) Use nx.draw() to visualize the graph using matplotlib.
Step 4: Gibbs Sampling and MCMC:
(i) Initialize Gibbs Sampling for MCMC using the GibbsSampling class and provide the
Bayesian network.
(ii)Set the number of samples to be generated using num_samples.
Step 5: Perform MCMC Sampling:
(i)Use the sample() method of the GibbsSampling instance to perform MCMC sampling.
(ii)Store the generated samples in the samples variable.
Step 6: Approximate Probability Calculation:
(i) Specify the variable for which you want to calculate the approximate probabilities
(query_variable).
(ii)Use .value_counts(normalize=True) on the samples of the query_variable to calculate
1 of 5 06-11-2023, 11:26
Firefox https://github.com/VARSHINI22009118/Ex-No.-4--Implementation-of...
approximate probabilities.
Step 7:Print Approximate Probabilities: (i) Print the calculated approximate probabilities
for the specified query_variable.
Program :
Import the necessary libraries
network=BayesianNetwork([('Burglary','Alarm'),
('Earthquake','Alarm'),
('Alarm','JohnCalls'),
('Alarm','MaryCalls')])
cpd_burglay=TabularCPD(variable='Burglary',variable_card=2,values=[[0.999],
[0.001]])
cpd_earthquake=TabularCPD(variable='Earthquake',variable_card=2,values=
[[0.998],[0.002]])
cpd_alarm=TabularCPD(variable='Alarm',variable_card=2,values=
[[0.999,0.71,0.06,0.05],[0.001,0.29,0.94,0.95]],evidence=
['Burglary','Earthquake'],evidence_card=[2,2])
cpd_john_calls=TabularCPD(variable='JohnCalls',variable_card=2,values=
[[0.95,0.1],[0.05,0.9]],evidence=['Alarm'],evidence_card=[2])
cpd_mary_calls=TabularCPD(variable='MaryCalls',variable_card=2,values=
[[0.99,0.3],[0.01,0.7]],evidence=['Alarm'],evidence_card=[2])
network.add_cpds(cpd_burglay,cpd_earthquake,cpd_alarm,cpd_john_calls,cpd_mary_calls)
Print the Bayesian network structure
print("Bayesian Network Structure:")
print(network)
2 of 5 06-11-2023, 11:26
Firefox https://github.com/VARSHINI22009118/Ex-No.-4--Implementation-of...
G=nx.DiGraph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
Set the positions from nodes
pos={'Burglary':(0,0),'Earthquake':(2,0),'Alarm':(1,-2),'JohnCalls':
(0,-4),'MaryCalls':(2,-4)}
nx.draw(G,pos,with_labels=True,node_size=1500,node_color='skyblue',font_size=10,font_weight='b
plt.title("Bayesian Network:alarm Problem")
plt.show()
gibbs_sampler=GibbsSampling(network)
num_samples=10000
samples=gibbs_sampler.sample(size=num_samples)
3 of 5 06-11-2023, 11:26
Firefox https://github.com/VARSHINI22009118/Ex-No.-4--Implementation-of...
query_variable='Burglary'
query_result=samples[query_variable].value_counts(normalize=True)
[query_variable].value_counts(normalize=True)
Output :
4 of 5 06-11-2023, 11:26
Firefox https://github.com/VARSHINI22009118/Ex-No.-4--Implementation-of...
Result :
Thus, an Approximate method of interference computation is implemented using Gibbs
Sampling in Python
5 of 5 06-11-2023, 11:26
Firefox https://github.com/VARSHINI22009118/Experiment-3--Hidden-Mark...
Experiment-5 --Hidden-Markov-Model
Aim:
Construct a Python code to find the sequence of hidden states by the known sequence
of obsevances 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 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 numpy as np
transition_matrix=np.array([[0.7,0.3],[0.4,0.6]])
emission_matrix=np.array([[0.1,0.9],[0.8,0.2]])
initial_probabilities=np.array([0.5,0.5])
1 of 2 06-11-2023, 11:28
Firefox https://github.com/VARSHINI22009118/Experiment-3--Hidden-Mark...
observed_sequence=np.array([1,1,1,0,0,1])
alpha=np.zeros((len(observed_sequence),len(initial_probabilities)))
alpha[0,:]=initial_probabilities * emission_matrix[:,observed_sequence[0]]
for t in range(1,len(observed_sequence)):
for j in range(len(initial_probabilities)):
alpha[t,j]=emission_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:
Result:
Thus, the Hidden Markov Model to identify the sequence of Hidden states is executed
successfully
2 of 2 06-11-2023, 11:28
Firefox https://github.com/VARSHINI22009118/Experiment-4---Implementatio...
Aim:
To Construct a Python Code to implement the Kalman filter to predict the
position and velocity of an object.
Algorithm:
Step 1:Define the state transition model F, the observation model H, the
process noise covariance Q, the measurement noise covariance R, the initial
state estimate x0, and the initial error covariance P0.
Step 2: Create a KalmanFilter object with these parameters.
Step 3:Simulate the movement of the object for a number of time steps,
generating true states and measurements. Step 3:For each measurement,
predict the next state using kf.predict().
Step 4:Update the state estimate based on the measurement using
kf.update().
Step 5:Store the estimated state in a list.
Step 6:Plot the true and estimated positions.
Program:
import numpy as np
class KalmanFilter:
def __init__(self,F,H,Q,R,x0,P0):
self.F=F
self.H=H
self.Q=Q
self.R=R
1 of 3 06-11-2023, 11:29
Firefox https://github.com/VARSHINI22009118/Experiment-4---Implementatio...
self.X=x0
self.P=P0
def predict(self):
self.X=np.dot(self.F,self.X)
self.P=np.dot(np.dot(self.F,self.P),self.F.T)+self.Q
def update(self,z):
y=z-np.dot(self.H,self.X)
S=np.dot(np.dot(self.H,self.P),self.H.T)+self.R
K=np.dot(np.dot(self.P,self.H.T),np.linalg.inv(S))
self.X=self.X+np.dot(K,y)
self.P=np.dot(np.eye(self.F.shape[0])-np.dot(K,self.H),self.P)
dt=0.1
F=np.array([[1,dt],[0,1]])
H=np.array([[1,0]])
Q=np.diag([0.1,0.1])
R=np.array([[1]])
x0=np.array([0,0])
P0=np.diag([1,1])
kf=KalmanFilter(F,H,Q,R,x0,P0)
true_states=[]
measurements=[]
for i in range(100):
true_states.append([i*dt,1])
measurements.append(i*dt+np.random.normal(scale=1))
est_states=[]
for z in measurements:
kf.predict()
kf.update(np.array([z]))
est_states.append(kf.X)
2 of 3 06-11-2023, 11:29
Firefox https://github.com/VARSHINI22009118/Experiment-4---Implementatio...
Output:
Result:
Thus, Kalman filter to predict the position and velocity of an object is
implemented successfully.
3 of 3 06-11-2023, 11:29
Firefox https://github.com/VARSHINI22009118/Experiment-5--Implementation...
Experiment-7--Implementation-of-
Speech-Recognition
Aim:
Construct a python program to implement speech recognition.
EQUIPMENTS REQUIRED:
Hardware – PCs Anaconda – Python 3.7 Installation / Google Colab /Jupiter
Notebook
Algorithm:
Step 1:Import the speech_recognition module as sr.
Step 2:Assign a string variable "file" with the name of the audio file that you
want to transcribe.
Step 3:Create an instance of the Recognizer class called "r".
Step 4:Use the AudioFile() method of sr to create an AudioFile object with
the audio file name passed as an argument.
Step 5:Use the record() method of r to read the audio data from the
AudioFile object and store it in the variable "audio".
Step 6:Use the recognize_google() method of r to transcribe the audio data
stored in the "audio" variable.
Step 7:Print the transcribed text on the console if the transcribe process was
successful.
Step 8:Handle any potential errors during the transcribing process. If the
audio is not clear, print "not clear". If there's an error while trying to retrieve
the transcribed text from the Google speech recognizer, print "Couldnt get
results from google speech recognizer".
1 of 3 06-11-2023, 11:30
Firefox https://github.com/VARSHINI22009118/Experiment-5--Implementation...
Program:
import speech_recognition as sr
# Assign a string variable "file" with the name of the audio file
that you want to transcribe.
file = "speech.wav"
Output:
2 of 3 06-11-2023, 11:30
Firefox https://github.com/VARSHINI22009118/Experiment-5--Implementation...
Result:
Thus, we have implemented a program that will transcribe the audio file in
the file variable and print the transcribed text on the console, one line at a
time.
3 of 3 06-11-2023, 11:30
Firefox https://github.com/VARSHINI22009118/Experiment-6---Implementatio...
Experiment-8---Implementation-of-
Semantic-Analysis
Algorithm:
Install the required libraries.
Install nltk (Natural Language Toolkit) by running pip install nltk in your
terminal.
Process the text file and display the verbs and their synonyms.
Program:
# Reg.no: 212222100059
1 of 3 06-11-2023, 11:33
Firefox https://github.com/VARSHINI22009118/Experiment-6---Implementatio...
import nltk
from nltk.corpus import wordnet
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('wordnet')
def get_synonyms(word):
synonyms = set()
for syn in wordnet.synsets(word):
for lemma in syn.lemmas():
synonyms.add(lemma.name())
return synonyms
def process_text_file(file_path):
with open(file_path, 'r') as file:
text = file.read()
return text # Return the processed text
text = process_text_file('/content/text.txt')
# Extract verbs
verbs = [word for word, pos in pos_tags if pos.startswith('V')]
2 of 3 06-11-2023, 11:33
Firefox https://github.com/VARSHINI22009118/Experiment-6---Implementatio...
Output:
Result
Thus, we have successfully implemented a program for Natural Language
Processing.
3 of 3 06-11-2023, 11:33