CS3491-AIML Lab Manual

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

EX.

NO:1
UNIFORMED SEARCH ALGORITHMS
DATE:
AIM:

To implement uniformed search algorithms(BFS,DFS).

1. Implement Breadth First Search(BFS)

ALGORITHM:

1. Create a graph
2. Initialize a starting node
3. Send the graph and initial node as parameters to the bfs function.
4. Mark the initial node as visited and push it into the queue
5. Explore the initial node and add its neighbours to the queue and remove the initial
node from the queue
6. Check if the neighbour node of an neighbouring node is already visited
7. If not,visit the neighbouring node neighbours and mark them as visited
8. Repeat this process until all the nodes in a graph are visited and the queue becomes
empty
PROGRAM:
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
def BFS(self, s):
visited = [False] * (len(self.graph))
queue = []
queue.append(s)
visited[s] = True
while queue:
s = queue.pop(0)
print (s, end = " ")
for i in self.graph[s]:
if visited[i] == False:
queue.append(i)
visited[i] = True
g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
print ("Following is Breadth First Traversal"
" (starting from vertex 2)")
g.BFS(2)
Output
Following is Breadth First Traversal (starting from vertex 2)
2031
2. (DFS) ALGORITHM:

1. Start by putting anyone of the graph's vertices on top of a stack


2. Take the top item of the stack and add it to the visited list
3. Create a list of that vertex's adjacent nodes.Add the ones which aren't in the visited
list to the top of the stack
4. Keep repeating steps 2 and 3 until the stack is empty

PROGRAM:
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def addEdge(self, u, v):
self.graph[u].append(v)
def DFSUtil(self, v, visited):
visited.add(v)
print(v, end=' ')
for neighbour in self.graph[v]:
if neighbour not in visited:
self.DFSUtil(neighbour, visited)
def DFS(self, v):
visited = set()
self.DFSUtil(v, visited)
if __name__ == "__main__":
g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
print("Following is DFS from (starting from vertex 2)")
g.DFS(2)

Output

Following is DFS from (starting from vertex 2)


2013

RESULT:

Thus the above programs are executed and the outputs are verified.
EX.NO:2
INFORMED SEARCH ALGORITHMS
DATE:

AIM:

To implement informed search algorithms (A*,memory-bounded A*).

1. Implement A* algorithm

ALGORITHM:

1. Firstly,Place the starting node into OPEN and find its f(n) value
2. Then remove the node from OPEN,having the smallest f(n) value. If it is a goal
node, then stop and return to success
3. Else remove the node from OPEN, and find all its successors
4. Find the f(n) value of all the successors,place them into OPEN,and place the
removed node into CLOSE
5. Go to Step-2
6. Exit

PROGRAM:

from collections import deque


class Graph:
def __init__(self, adjacency_list):
self.adjacency_list = adjacency_list

def get_neighbors(self, v):


return self.adjacency_list[v]
def h(self, n):
H={
'A': 1,
'B': 1,
'C': 1,
'D': 1
}
return H[n]
def a_star_algorithm(self, start_node, stop_node):

open_list = set([start_node])
closed_list = set([])

g = {}

g[start_node] = 0
parents = {}
parents[start_node] = start_node

while len(open_list) > 0:


n = None

for v in open_list:
if n == None or g[v] + self.h(v) < g[n] + self.h(n):
n = v;

if n == None:
print('Path does not exist!')
return None

if n == stop_node:
reconst_path = []

while parents[n] != n:
reconst_path.append(n)
n = parents[n]

reconst_path.append(start_node)

reconst_path.reverse()

print('Path found: {}'.format(reconst_path))


return reconst_path

for (m, weight) in self.get_neighbors(n):

if m not in open_list and m not in closed_list:


open_list.add(m)
parents[m] = n
g[m] = g[n] + weight

else:
if g[m] > g[n] + weight:
g[m] = g[n] + weight
parents[m] = n

if m in closed_list:
closed_list.remove(m)
open_list.add(m)
open_list.remove(n)
closed_list.add(n)

print('Path does not exist!')


return None
adjacency_list = {
'A': [('B', 1), ('C', 3), ('D', 7)],
'B': [('D', 5)],
'C': [('D', 12)]
}
graph1 = Graph(adjacency_list)
graph1.a_star_algorithm('A', 'D')

OUTPUT:
Path found: ['A', 'B', 'D']

RESULT:

Thus the above program is executed and the output is verified.


EX.NO:3
NAÏVE BAYES MODEL
DATE:

AIM:

To implement Gaussiannaïve Bayes model

ALGORITHM:

1. Importnecessarylibrariesandpackages
2. Load the dataset
3. Split the dataset into train data and test data
4. Load the Gaussian naïve Bayes algorithm
5. Train the algorithm with train data
6. Test the accuracy of the algorithm

PROGRAM:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
iris = load_iris()
X = iris.data
y=iris.target
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.4,random_state=1)
gnb = GaussianNB()
gnb.fit(X_train, y_train)
y_pred=gnb.predict(X_test)
print("GaussianNaiveBayesmodelaccuracy(in %):",metrics.accuracy_score(y_test, y_pred)*100)

OUTPUT:

Gaussian Naïve Bayes model accuracy (in %): 95.0

RESULT:

Thus the above program is executed and the output is verified.


EX.NO:4
BAYESIAN NETWORK
DATE:
AIM:

To implement Bayesian Network.

ALGORITHM:
1. Import necessary packages and modules.
2. Load the Bayesian model
3. Initialize the Computation of joint probability and conditional probability Functions
4. Execute the probability functions for each node in the Bayesian network
5. Print the Joint Probability

PROGRAM:
import numpy as np
from scipy.stats import norm
node1 = ("Node1", [], {(): 0.5})
node2 = ("Node2", ["Node1"], {(True,): 0.7, (False,): 0.2})
node3 = ("Node3", ["Node1"], {(True,): 0.1, (False,): 0.8})
def compute_joint_prob(nodes):
joint_prob = 1.0
for node in nodes:
name, parents, cpt = node
parents_val_tuple = tuple(node_val[parent] for parent in parents)
prob = cpt[parents_val_tuple]
joint_prob *= prob
return joint_prob
def compute_cond_prob(node, node_val):
name, parents, cpt = node
parents_val_tuple = tuple(node_val[parent] for parent in parents)
prob = cpt[parents_val_tuple]
return prob
def sample_node(node, node_val):
prob = compute_cond_prob(node, node_val)
sample = np.random.binomial(1, prob)
return sample
node_val = {}
for node in [node1, node2, node3]:
sample = sample_node(node, node_val)
node_val[node[0]] = sample
joint_prob = compute_joint_prob([node1, node2, node3])
print("The joint probability is", joint_prob)

OUTPUT:
The joint probability is 0.08000000000000002

RESULT:

Thus the above program is executed and the output is verified.


EX.NO:5
REGRESSION MODELS
DATE:
AIM:

To build regression models.


ALGORITHM:
1. Import the necessary packages and modules
2. Create the arrays that represent the values of the x and y axis
3. Create a function that uses the slope and intercept values to return a new value. This
new value represents where on the y-axis the corresponding x value will be placed
4. Run each value of the x array through the function. This will result in a new
array with new values for the y-axis
5. Draw the original scatter plot
6. Draw the line of linear regression
7. Display the diagram

PROGRAM:

Import matplotlib.pyplot as plt


from scipy import stats
x =[5,7,8,7,2,17,2,9,4,11,12,9,6]
y =[99,86,87,88,111,86,103,87,94,78,77,85,86]
slope,intercept,r,p,std_err=stats.linregress(x,y)
def myfunc(x):
return slope*x +intercept

mymodel=list(map(myfunc,x))
print(mymodel)
plt.scatter(x, y)
plt.plot(x,mymodel)
plt.show()
OUTPUT:

RESULT:

Thus the above program is executed and the output is verified.


EX.NO:6
DECISION TREES AND RANDOM FORESTS
DATE:

AIM:

To build decision trees and random forests

1. Build decision tree

ALGORITHM:

1. Import necessary packages and libraries


2. Load the dataset
3. Load the algorithm decision tree and train the algorithm using the dataset
4. Predict the category of new data
5. Print the graph for the decision tree.

PROGRAM:

From sklearn.datasets import load_iris


from sklearn import tree
Import graph viz
iris =load_iris()
X, y = iris.data, iris.target
targets=iris.target_names
clf=tree.DecisionTreeClassifier() clf
= clf.fit(X, y)
X_pred= [6.7,3.0,5.2, 2.3]
y_pred= clf.predict([X_pred])
print("Predictionis: {}".format(targets[y_pred]))
dot_data=tree.export_graphviz(clf,out_file=None,feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True,rounded=True,
special_characters=True)
graph=graphviz.Source(dot_data)
graph
OUTPUT:

2. Build random

forest ALGORITHM:

1. Import necessary packages and libraries


2. Load the dataset
3. Load the algorithm Random Forest and train the algorithm using the dataset
4. Predict the category of new data

PROGRAM:

From sklearn.ensemble impor tRandom ForestClassifier


from sklearn.datasets import load_iris
iris =load_iris()
X, y = iris.data, iris.target
targets=iris.target_names
clf=RandomForestClassifier(random_state=100) clf
= clf.fit(X, y)
X_pred= [6.7,3.0,5.2, 2.3]
y_pred= clf.predict([X_pred])
print("Predictionis: {}".format(targets[y_pred]))

OUTPUT:

RESULT:

Thus the above programs are executed and the outputs are verified.
EX.NO:7
SVM MODEL
DATE:

AIM:

To build SVM(SupportVectorMachine) models

ALGORITHM:

1. Import necessary packages and libraries


2. Load the dataset
3. Load the algorithm Support Vector Machine and train the algorithm using the dataset
4. Predict the category of new data

PROGRAM:

From sklearn.datasets import load_iris


from sklearn.svm import SVC
iris=load_iris()
X_train = iris.datay_train=iris. target
targets=iris.target_namesprint(targets)
cls = SVC()
cls.fit(X_train,y_train)
X_pred= [5.1,3.2,1.5, 0.5]
y_pred= cls.predict([X_pred])
print("Predictionis: {}".format(targets[y_pred]))

OUTPUT:

RESULT:

Thus the above program is executed and the output is verified.


EX.NO:8
ENSEMBLE TECHNIQUES
DATE:

AIM:

To implement Max voting ensemble technique.

ALGORITHM:

1. Import the necessary modules and packages


2. Load the dataset
3. Load the models(SVM, RandomForest,Decisiontree)
4. Combine the models and train them using dataset
5. Predict the category of the new datapoint.

PROGRAM:

From sklearn.datasets import load_iris


from sklearn.svm import SVC
From sklearn.ensemble import RandomForestClassifier
from sklearn import tree
From sklearn.ensemble import VotingClassifier
iris=load_iris()
X_train = iris.datay_train=iris.target
targets=iris.target_namesprint(targets)
m1 =tree.DecisionTreeClassifier()
m2=RandomForestClassifier(random_state=100)
m3 = SVC()
final_model=VotingClassifier(estimators=[('dt',m1),('rf',m2),('svc',m3)],voting='hard')
final_model.fit(X_train, y_train)
X_pred= [6.7,3.0,5.2, 2.3]
y_pred = final_model.predict([X_pred])
print("Predictionis:{}".format(targets[y_pred]))

OUTPUT:

RESULT:

Thus the above program is executed and the output is verified.


EX.NO:9
CLUSTERING ALGORITHMS
DATE:

AIM:

To implement K-Nearest Neighbor clustering algorithm.

ALGORITHM:

1. Import necessary packages and libraries


2. Load the dataset
3. Load the algorithm k-Nearest Neighbor and train the algorithm using the dataset
4. Predict the category of new data

PROGRAM:

From sklearn.datasets import load_iris


From sklearn.neighbors import KNeighbors
Classifieriris=load_iris()
X_train = iris.datay_train=iris.target
targets=iris.target_namesprint(targets)
cls=KNeighborsClassifier(n_neighbors=5)
cls.fit(X_train, y_train)
X_pred= [6.7,3.0,5.2, 2.3]
y_pred=cls.predict([X_pred])
print("The Prediction is")
print("".join(targets[y_pred]))

OUTPUT:

RESULT:

Thus the above program is executed and the output is verified.


EX.NO:10
EM FOR BAYESIAN NETWORK
DATE:

AIM:

To implement EM for Bayesian networks.

ALGORITHM:

1. Import necessary libraries and packages


2. Define the Bayesian network
3. Generate the true probability distributions P for each node
4. Randomly initialize the estimated probability distributions P^ for each node
5. Perform the E-step and M-step for 32 epochs
6. Plot the log likelihood for each epoch

PROGRAM:

impor tnumpy as np
import time
graphNodes=["a","b", "c","d", "e","f","g", "h"]

graphNodeIndices={}
for idx,node in enumerate(graphNodes):
graphNodeIndices[node] = idx

graphNodeNumStates={ "a"
: 3,
"b": 4,
"c": 5,
"d": 4,
"e": 3,
"f": 4,
"g": 5,
"h": 4
}

nodesToUpdate=["a","b","c","d","e","f","g","h"]

nodeParents = {
"a": [],
"b": [],
"c": ["a"],
"d":["a","b"],
"e":["a","c"],
"f": ["b", "d"],
"g": ["e"],
"h": ["f"]
}

tensorNodeOrder = {}
for nodeingraphNodes:
tensorNodeOrder[node]=[node]+nodeParents[node]
def randomTensorGenerator(shape):
return np.random.uniform(0.0,1.0,shape)

def conditionNodeOnParents(probTensor,node,tensorNodeOrder):
assert(node in tensorNodeOrder)
inferredDimension=tensorNodeOrder.index(node)
probTensor=probTensor/
np.expand_dims(np.sum(probTensor,inferredDimension),inferredDimension)
return probTensor

np.random.seed(0)
p = {}
for node in graphNodes:
tensorDimensions=[graphNodeNumStates[x]forxintensorNodeOrder[node]] p[node] =
randomTensorGenerator(tensorDimensions)

fornodein p:
p[node]=conditionNodeOnParents(p[node],tensorNodeOrder[node]
[0],tensorNodeOrder[node])
print("p("+node+"|" +str(nodeParents[node])+")dimensions:"+str(p[node].shape))
np.random.seed(int(time.time()))

phat={}

for node in p:
phat[node]=randomTensorGenerator(p[node].shape)
phat[node]=conditionNodeOnParents(phat[node],tensorNodeOrder[node]
[0],tensorNodeOrder[node])
print("phat("+node+"|"+str(nodeParents[node])+")dimensions:"+str(phat[node].shape))
OUTPUT:

RESULT:

Thus the above program is executed and the output is verified.


EX.NO:11
NEURAL NETWORK MODEL
DATE:

AIM:

To build simple Neural network(NN) models.

ALGORITHM:

1. Import the necessary packages and libraries


2. Use numpy arrays to store inputs x and output y
3. Define the network model and its arguments.
4. Set the number of neurons/nodes for each layer
5. Compile the model and calculate its accuracy
6. Print the summary of the model

PROGRAM:

From keras.models import Sequential


From keras.layers importDense,Activation
import numpy as np
x=np.array([[0,0],[0,1],[1,0],[1,1]])
y=np.array([[0], [1],[1], [0]])
model = Sequential()
model.add(Dense(2,input_shape=(2,)))
model.add(Activation('sigmoid'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='mean_squared_error',optimizer='sgd',metrics=['accuracy'])
model.summary()
OUTPUT:

RESULT:

Thus the above program is executed and the output is verified.


EX.NO:12
DEEPLEARNINGNEURALNETWORK MODEL
DATE:

AIM:

To build deep learning NN(NeuralNetwork) models.

ALGORITHM:

1. Load the dataset


2. Split the dataset into input x and output y
3. Define the keras model
4. Compile the keras model
5. Train the keras model with the dataset
6. Make predictions using the model

PROGRAM:

From numpy import load txt


From tensorflow.keras.models importSequential
from tensorflow.keras.layers import Dense
dataset=loadtxt('https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-
diabetes.data.csv', delimiter=',')
X=dataset[:,0:8] y = dataset[:,8]
model =Sequential()
model.add(Dense(12,input_shape=(8,),activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(X, y, epochs=150, batch_size=10, verbose=0)
predictions=(model.predict(X)>0.5).astype(int) for i in range(5):
print('%s=>%d(expected%d)'% (X[i].tolist(),predictions[i],y[i]))

OUTPUT:

RESULT:

Thus the above program is executed and the output is verified.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy