0 Aimlfinal
0 Aimlfinal
PROGRAM:
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
Output:
Ex.No : 2 .Implementation of Informed search algorithms (A*,
memory-bounded A*)
Date :
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
OUTPUT:
Ex.No : 3 Implement Naive Bayes model
Date :
PROGRAM:
OUTPUT:
Ex.No : 4 Implement Bayesian Networks
Date :
PROGRAM:
Output:
Ex.No : 5 Build Regression models
Date :
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:
Ex.No : 6 Build decision trees and random forests
Date :
PROGRAM:
# Evaluate the performance of the decision tree model on the testing data
dt_pred = dt_model.predict(X_test)
dt_acc = accuracy_score(y_test, dt_pred)
# 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)
OUTPUT
Ex.No : 7 Build SVM model
Date :
PROGRAM:
PROGRAM:
# load dataset
iris = load_iris()
Output:
Ex.No : 9 Implement clustering algorithms
Date :
PROGRAM:
Output:
Ex.No : 10 Implement EM for Bayesian networks
Date :
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
# 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
# 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:
Ex.No : 11 Implement EM for Bayesian networks
Date :
PROGRAM:
model = Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
PROGRAM:
Output: