Aiml Record
Aiml Record
AIM:
To implement uninformed search algorithms(BFS,DFS) in python.
ALGORITHM:
Breadth-First Search (BFS) Algorithm:
STEP1. Initialize an empty queue and a visited set.
STEP2. Enqueue the starting node into the queue.
STEP3. Mark the starting node as visited.
STEP4. While the queue is not empty, do the following:
- Dequeue a node from the front of the queue.
- Process the dequeued node (e.g., print or perform any required operations).
- Enqueue all the unvisited neighboring nodes of the dequeued node into the queue.
- Mark each neighboring node as visited.
STEP5. Repeat steps 4 until the queue becomes empty.
STEP6. If there are still unvisited nodes in the graph, choose another unvisited node as
the new starting point and repeat steps 2 to 5.
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
while queue:
path = queue.pop(0)
node = path[-1]
if node == goal:
return path
while stack:
path = stack.pop()
node = path[-1]
if node == goal:
return path
if node not in visited:
visited.add(node)
RESULT:
AIM:
To implement informed search algorithms(A*, memory-bounded A*) in python.
ALGORITHM:
A* Algorithm:
STEP1. Initialize open and closed lists.
STEP2. Add starting node to the open list.
STEP3. While open list is not empty:
- Select node with lowest cost from open list as current node.
- Move current node to closed list.
- If current node is the goal node, exit.
- Generate neighboring nodes.
- Calculate g and h values for each neighbor.
- If neighbor is in closed list, skip it.
- If neighbor is not in open list, add it.
- If neighbor is in open list, update if new g value is lower.
STEP4. If open list is empty and goal node not reached, no path exists.
PROGRAM:
import heapq
start = (0, 0)
goal = (5, 7)
# A* Algorithm
def astar(grid, start, goal):
open_list = [(0, start)]
closed_list = set()
g_score = {start: 0} # start node's g score is 0
h_score = {start: heuristic(start, goal)}
parent = {start: None}
while open_list:
current = heapq.heappop(open_list)[1]
if current == goal:
return reconstruct_path(parent, current)
closed_list.add(current)
new_g_score = g_score[current] + 1
if neighbor not in [item[1] for item in open_list]:
heapq.heappush(open_list, (new_g_score + heuristic(neighbor, goal),
neighbor))
elif new_g_score >= g_score[neighbor]:
continue
parent[neighbor] = current
g_score[neighbor] = new_g_score
h_score[neighbor] = heuristic(neighbor, goal)
return None
# Memory-Bounded A* Algorithm
# Memory-Bounded A* Algorithm
def memory_bounded_astar(grid, start, goal, memory_limit):
open_list = [(0, start)]
closed_list = set()
g_score = {start: 0} # start node's g score is 0
h_score = {start: heuristic(start, goal)} # start node's h score is estimated usin
g the heuristic function
parent = {start: None}
while open_list:
current = heapq.heappop(open_list)[1]
if current == goal:
return reconstruct_path(parent, current)
closed_list.add(current)
new_g_score = g_score[current] + 1
return None
return neighbors
memory_limit = 1000000000 # 1 GB
RESULT:
Date :
AIM:
To implement Naïve Bayes Model in python.
ALGORITHM
RESULT:
Thus the program to implement Naïve Bayes Model in python is executed successfully
and the required output is obtained.
Ex.No : 4 Implement Bayesian Networks
Date :
AIM:
To implement Bayesian Networks in python.
ALGORITHM:
RESULT:
Date :
AIM:
To build Regression models in python.
ALGORITHM:
PROGRAM:
# Calculate the mean squared error for the linear regression model
mse_lr = mean_squared_error(y_test, y_pred_lr)
print(f"Linear Regression Mean Squared Error: {mse_lr:.2f}")
# Calculate the mean squared error for the random forest regression model
mse_rf = mean_squared_error(y_test, y_pred_rf)
print(f"Random Forest Mean Squared Error: {mse_rf:.2f}")
OUTPUT:
RESULT:
Thus the program to build regression model in python is executed successfully and the
required output is obtained.
Ex.No : 6 Build decision trees and random forests
Date :
AIM:
To build decision trees and random forests in python.
ALGORITHM:
PROGRAM:
# Evaluate the performance of the random forest model on the testing data
rf_pred = rf_model.predict(X_test)
rf_acc = accuracy_score(y_test, rf_pred)
RESULT:
Thus the program to build decision tree and random forest in python is executed
successfully and the required output is obtained.
Ex.No : 7 Build SVM model
Date :
AIM:
To build a SVM model in python.
ALGORITHM:
PROGRAM:
RESULT:
Thus the program to build SVM model in python is executed successfully and the
required output is obtained.
Ex.No : 8 Implement ensembling technique
Date :
AIM:
To implement Ensembling Technique in python.
ALGORITHM:
PROGRAM:
# load dataset
iris = load_iris()
RESULT:
AIM:
To implement Clustering Algorithms in python.
ALGORITHM:
PROGRAM:
RESULT:
AIM:
To implement EM for Bayesian Networks in python.
ALGORITHM:
PROGRAM:
import numpy as np
from collections import defaultdict
class BayesianNetwork:
def init (self, structure):
self.structure = structure
self.nodes = sorted(list(self.structure.keys()))
self.parents = defaultdict(list)
for child, parents in self.structure.items():
for parent in parents:
self.parents[child].append(parent)
class EM:
def init (self, bn, data):
self.bn = bn
self.data = data
def run(self, num_iterations=100, tolerance=1e-4):
# Initialize parameters
pi = defaultdict(float)
theta = defaultdict(lambda: defaultdict(float))
for i in range(len(self.bn.nodes)):
node = self.bn.nodes[i]
pi[node] = np.mean(self.data[:, i])
for parent in self.bn.parents[node]:
parent_index = self.bn.nodes.index(parent)
parent_data = self.data[:, parent_index]
for parent_value in np.unique(parent_data):
parent_mask = parent_data == parent_value
theta[parent][parent_value, node] = np.mean(self.data[parent_mask, i])
# Run EM algorithm
for iteration in range(num_iterations):
# E-step: Compute expected sufficient statistics
pi_new = defaultdict(float)
theta_new = defaultdict(lambda: defaultdict(float))
for i in range(len(self.bn.nodes)):
node = self.bn.nodes[i]
pi_new[node] = np.mean(self.data[:, i])
for parent in self.bn.parents[node]:
parent_index = self.bn.nodes.index(parent)
parent_data = self.data[:, parent_index]
for parent_value in np.unique(parent_data):
parent_mask = parent_data == parent_value
theta_new[parent][parent_value, node] = np.mean(self.data[pare
nt_mask, i])
pi = pi_new
theta = theta_new
return pi, theta
# Example usage
structure = {0: [], 1: [0], 2: [0], 3: [1, 2]}
data = np.array([[0, 0, 0, 0], [0, 0, 0, 1], [1, 1, 0, 0], [1, 1, 1, 1]])
bn = BayesianNetwork(structure)
em = EM(bn, data)
pi, theta = em.run()
print("pi:", pi)
print("theta:", theta)
Output:
RESULT:
AIM:
To build simple NN models in python.
ALGORITHM:
PROGRAM:
model = Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
RESULT:
Thus the program to build simple NN Models in python is executed successfully and the
required output is obtained.
Ex.No : 12 Build deep learning NN models
Date :
AIM:
To build deep learning NN models in python.
ALGORITHM:
PROGRAM:
RESULT:
Thus the program to build deep learning NN Models in python is executed successfully
and the required output is obtained.