0% found this document useful (0 votes)
9 views

AIML (2)

The document outlines a series of experiments involving the implementation of various algorithms, including uninformed search algorithms (BFS, DFS), informed search algorithms (A*, memory-bounded A*), Naive Bayes models, Bayesian networks, regression models, decision trees, SVM models, and deep learning neural networks. Each experiment includes a specific aim, algorithm steps, program code, and results demonstrating successful implementation. The document serves as a comprehensive guide for developing and testing these algorithms in Python.

Uploaded by

mahalakshmi R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

AIML (2)

The document outlines a series of experiments involving the implementation of various algorithms, including uninformed search algorithms (BFS, DFS), informed search algorithms (A*, memory-bounded A*), Naive Bayes models, Bayesian networks, regression models, decision trees, SVM models, and deep learning neural networks. Each experiment includes a specific aim, algorithm steps, program code, and results demonstrating successful implementation. The document serves as a comprehensive guide for developing and testing these algorithms in Python.

Uploaded by

mahalakshmi R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

LIST OF EXPERIMENTS

1. Implementation of Uninformed search algorithms (BFS, DFS)

2. Implementation of Informed search algorithms (A*, memory-

bounded A*)
3. Implement naive Bayes models

4. Implement Bayesian Networks

5. Build Regression models

6. Build decision trees and random forests

7. Build SVM models

8. Implement ensembling techniques

9. Implement clustering algorithms

10.Implement EM for Bayesian networks

11.Build simple NN models

12. Build deep learning NN models


Ex. No. 1.A UNINFORMED SEARCH ALGORITHM - BFS

Date:

Aim:
To write a Python program to implement Breadth First Search (BFS).

Algorithm:

Step 1. Start
Step 2. Put any one of the graph’s vertices at the back of the queue.
Step 3. Take the front item of the queue and add it to the visited list.
Step 4. Create a list of that vertex's adjacent nodes. Add those which are not within the
visited list to the rear of the queue.
Step 5. Continue steps 3 and 4 till the queue is empty.
Step 6. Stop

Program:

graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}

visited = [] # List for visited nodes.


queue = [] #Initialize a queue

def bfs(visited, graph, node): #function for BFS


visited.append(node)
queue.append(node)

while queue: # Creating loop to visit each node


m = queue.pop(0)
print (m, end = " ")

for neighbour in graph[m]:


if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)

# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, '5') # function calling
OUTPUT:

Following is the Breadth-First Search


537248

Result:

Thus the Python program to implement Breadth First Search (BFS) was developed
successfully.
Ex. No.1.B UNINFORMED SEARCH ALGORITHM - DFS

Date:

Aim:
To write a Python program to implement Depth First Search (DFS).

Algorithm:

Step 1.Start
Step 2.Put any one of the graph's vertex on top of the stack.
Step 3.After that take the top item of the stack and add it to the visited list of the vertex.
Step 4.Next, create a list of that adjacent node of the vertex. Add the ones which aren't in the
visited list of vertexes to the top of the stack.
Step 5.Repeat steps 3 and 4 until the stack is empty.
Step 6.Stop

Program:

graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}

visited = set() # Set to keep track of visited nodes of graph.

def dfs(visited, graph, node): #function for dfs


if node not in visited:
print (node)
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)

# Driver Code
print("Following is the Depth-First Search")
dfs(visited, graph, '5')
OUTPUT:

Following is the Depth-First Search


5
3
2
4
8
7

Result:

Thus the Python program to implement Depth First Search (DFS) was developed
successfully.
Ex. No.2. A INFORMED SEARCH ALGORITHM

Date: A* SEARCH

Aim:
To write a Python program to implement A* search algorithm.

Algorithm:
1. Initialize the starting node with a cost of zero and add it to an open list.
2. While the open list is not empty:
a) Find the node with the lowest cost in the open list and remove it.
b) If this node is the goal node, return the path to this node.
c) Generate all successor nodes of the current node.
d) For each successor node, calculate its cost and add it to the open list.
3. If the open list is empty and the goal node has not been found, then there is no path from the
start node to the goal node.

Program:
from queue import PriorityQueue

v = 14
graph = [[] for i in range(v)]

# Function For Implementing Best First Search


# Gives output path having lowest cost

def best_first_search(actual_Src, target, n):


visited = [False] * n
pq = PriorityQueue()
pq.put((0, actual_Src))
visited[actual_Src] = True

while not pq.empty():


u = pq.get()[1]
# Displaying the path having lowest cost
print(u, end="")
if u == target:
break

for v, c in graph[u]:
if not visited[v]:
visited[v] = True
pq.put((heuristic(v, target), v))
print()

# Function for adding edges to graph


def addedge(x, y, cost):
graph[x].append((y, cost))
graph[y].append((x, cost))

# A simple heuristic function for demonstration purposes


def heuristic(node, target):
return abs(node - target)

# The nodes shown in above example(by alphabets) are # implemented using integers addedge(x,y,cost);
addedge(0, 1, 3)
addedge(0, 2, 6)
addedge(0, 3, 5)
addedge(1, 4, 9)
addedge(1, 5, 8)
addedge(2, 6, 12)
addedge(2, 7, 14)
addedge(3, 8, 7)
addedge(8, 9, 5)
addedge(8, 10, 6)
addedge(9, 11, 1)
addedge(9, 12, 10)
addedge(9, 13, 2)

source = 0
target = 9
best_first_search(source, target, v)

OUTPUT:

0389

Result:

Thus the python program for A* Search was developed and the output was verifiedsuccessfully.
Ex. No.2.B INFORMED SEARCH ALGORITHM

Date: MEMORY-BOUNDED A*

Aim:
To write a Python program to implement memory- bounded A* search algorithm.

Algorithm:
1. Initialize the starting node with a cost of zero and add it to an open list and a closed list.
2. While the open list is not empty:
a. Find the node with the lowest cost in the open list and remove it.
b. If this node is the goal node, return the path to this node.
c. Generate all successor nodes of the current node.
d. For each successor node, calculate its cost and add it to the open list if it is not in
the closed list.
e. If the open list is too large, remove the node with the highest cost from the open
list and add it to the closed list.
f. Add the current node to the closed list.
3. If the open list is empty and the goal node has not been found, then there is no
path from the start node to the goal node.

Program:
import numpy as np
import heapq

class Graph:
def init (self, adjacency_matrix):
self.adjacency_matrix = adjacency_matrix
self.num_nodes = len(adjacency_matrix)

def get_neighbors(self, node):


return [neighbor for neighbor, is_connected in enumerate(self.adjacency_matrix[node]) if
is_connected]

def memory_bounded_a_star(graph, start, goal, memory_limit):


visited = set()
priority_queue = [(0, start)]
memory_usage = 0

while priority_queue:
memory_usage = max(memory_usage, len(visited))
if memory_usage > memory_limit:
print("Memory limit exceeded!")
return None

cost, current_node = heapq.heappop(priority_queue)

if current_node == goal:
print("Goal found!")
return cost

if current_node not in visited:


visited.add(current_node)

for neighbor in graph.get_neighbors(current_node):


heapq.heappush(priority_queue, (cost + 1, neighbor))

print("Goal not reachable!")


return None

if name == " main ":


# Define the adjacency matrix of the graph
adjacency_matrix = np.array([
[0, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 0, 0],
[1, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 1, 1],
[0, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 0, 1],
[0, 0, 0, 1, 1, 1, 0]
])

graph = Graph(adjacency_matrix)
start_node = 0
goal_node = 6
memory_limit = 10 # Set the memory limit

result = memory_bounded_a_star(graph, start_node, goal_node, memory_limit)


print("Shortest path cost:", result)

OUTPUT:

Goal found!
Shortest path cost: 3

Result:

Thus the python program for memory-bounded A* search was developed and the
output was verified successfully.
Ex. No.3 NAIVE BAYES MODEL

Date:

Aim:
To write a python program to implement Naïve Bayes model.

Algorithm:

Step 1. Load the libraries: import the required libraries such as pandas, numpy, and
sklearn.
Step 2. Load the data into a pandas dataframe.
Step 3. Clean and preprocess the data as necessary. For example, you can handle missing
values, convert categorical variables into numerical variables, and normalize the
data.
Step 4. Split the data into training and test sets using the train_test_split function from
scikit-learn.
Step 5. Train the Gaussian Naive Bayes model using the training data.
Step 6. Evaluate the performance of the model using the test data and the accuracy_score
function from scikit-learn.
Step 7. Finally, you can use the trained model to make predictions on new data.

Program:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score

# Load the dataset (example: iris dataset)


from sklearn.datasets import load_iris
data = load_iris()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.Series(data.target)

# Split the data into training and test sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize Gaussian Naive Bayes model


model = GaussianNB()

# Train the model


model.fit(X_train, y_train)

# Predictions on the test data


y_pred = model.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

OUTPUT:

Accuracy: 0.9777777777777777

Result:

Thus the Python program for implementing Naïve Bayes model was developed and
the output was verified successfully.
Ex. No.4 BAYESIAN NETWORKS

Date:

Aim:

To write a python program to implement a Bayesian network for the Monty Hall
problem.

Algorithm:

1. Define the variables: The first step in implementing a Bayesian Network is to


define the variables that will be used in the model. Each variable should be clearly
defined and its possible states should be enumerated.
2. Determine the relationships between variables: The next step is to determine the
probabilistic relationships between the variables. This can be done by identifying
the causal relationships between the variables or by using data to estimate the
conditional probabilities of each variable given its parents.
3. Construct the Bayesian Network: The Bayesian Network can be constructed by
representing the variables as nodes in a directed acyclic graph (DAG). The edges
between the nodes represent the conditional dependencies between the variables.
4. Assign probabilities to the variables: Once the structure of the Bayesian
Network has been defined, the probabilities of each variable must be assigned. This
can be done by using expert knowledge, data, or a combination of both.
5. Inference: Inference refers to the process of using the Bayesian Network to
make predictions or draw conclusions. This can be done by using various
inference algorithms, such as variable elimination or belief propagation.
6. Learning: Learning refers to the process of updating the probabilities in the
Bayesian Network based on new data. This can be done using various learning
algorithms, such as maximum likelihood or Bayesian learning.
7. Evaluation: The final step in implementing a Bayesian Network is to evaluate its
performance. This can be done by comparing the predictions of the model to actual
data and computing various performance metrics, such as accuracy or precision.

Program:
import pandas as pd
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.models import BayesianNetwork
from pgmpy.inference import VariableElimination

data = pd.read_csv("E:/dataset/heart.csv.csv")
heart_disease = pd.DataFrame(data)
print(heart_disease)
model = BayesianNetwork([
('age', 'Lifestyle'),
('Gender', 'Lifestyle'),
('Family', 'heartdisease'),
('diet', 'cholestrol'),
('Lifestyle', 'diet'),
('cholestrol', 'heartdisease'),
('diet', 'cholestrol')
])

model.fit(heart_disease, estimator=MaximumLikelihoodEstimator)

HeartDisease_infer = VariableElimination(model)

print('For Age enter SuperSeniorCitizen:0, SeniorCitizen:1, MiddleAged:2, Youth:3, Teen:4')


print('For Gender enter Male:0, Female:1')
print('For Family History enter Yes:1, No:0')
print('For Diet enter High:0, Medium:1')
print('for LifeStyle enter Athlete:0, Active:1, Moderate:2, Sedentary:3')
print('for Cholesterol enter High:0, BorderLine:1, Normal:2')

q = HeartDisease_infer.query(variables=['heartdisease'], evidence={
'age': int(input('Enter Age: ')),
'Gender': int(input('Enter Gender: ')),
'Family': int(input('Enter Family History: ')),
'diet': int(input('Enter Diet: ')),
'Lifestyle': int(input('Enter Lifestyle: ')),
'cholestrol': int(input('Enter Cholestrol: '))
})

print(q)
Output:

Result:

Thus, the Python program for implementing Bayesian Networks was successfully
developed and the output was verified.
Ex. No. 5
REGRESSION MODEL
Date:

Aim:
To write a Python program to build Regression models

Algorithm:

Step 1. Import necessary libraries: numpy, pandas, matplotlib.pyplot, LinearRegression,


mean_squared_error, and r2_score.
Step 2. Create a numpy array for waist and weight values and store them in separate
variables.
Step 3. Create a pandas DataFrame with waist and weight columns using the numpy arrays.
Step 4. Extract input (X) and output (y) variables from the DataFrame.
Step 5. Create an instance of LinearRegression model.
Step 6. Fit the LinearRegression model to the input and output variables.
Step 7. Create a new DataFrame with a single value of waist.
Step 8. Use the predict() method of the LinearRegression model to predict the weight for the
new waist value.
Step 9. Calculate the mean squared error and R-squared values using mean_squared_error()
and r2_score() functions respectively.
Step 10. Plot the actual and predicted values using matplotlib.pyplot.scatter() and
matplotlib.pyplot.plot() functions.

Program:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# import sample data using pandas


waist = np.array([70, 71, 72, 73, 74, 75, 76, 77, 78, 79])
weight = np.array([55, 57, 59, 61, 63, 65, 67, 69, 71, 73])
data = pd.DataFrame({'waist': waist, 'weight': weight})

# extract input and output variables


X = data[['waist']]
y = data['weight']

# fit a linear regression model


model = LinearRegression()
model.fit(X, y)
# make predictions on new data
new_data = pd.DataFrame({'waist': [80]})
predicted_weight = model.predict(new_data[['waist']])
print("Predicted weight for new waist value:", int(predicted_weight))

#calculate MSE and R-squared


y_pred = model.predict(X)
mse = mean_squared_error(y, y_pred)
print('Mean Squared Error:', mse)
r2 = r2_score(y, y_pred)
print('R-squared:', r2)

# plot the actual and predicted values


plt.scatter(X, y, marker='*', edgecolors='g')
plt.scatter(new_data, predicted_weight, marker='*', edgecolors='r')
plt.plot(X, y_pred, color='y')
plt.xlabel('Waist (cm)')
plt.ylabel('Weight (kg)')
plt.title('Linear Regression Model')
plt.show()

OUTPUT:

Result:

Thus the Python program to build a simple linear Regression model was developed
successfully.
Ex. No. 6 DECISION TREE AND RANDOM FOREST

Date:

Aim:
To write a Python program to build decision tree and random forest.

Algorithm:

Step 1. Import necessary libraries: numpy, matplotlib, seaborn, pandas, train_test_split,


LabelEncoder, DecisionTreeClassifier, plot_tree, and RandomForestClassifier.
Step 2. Read the data from 'flowers.csv' into a pandas DataFrame.
Step 3. Extract the features into an array X, and the target variable into an array y.
Step 4. Encode the target variable using the LabelEncoder.
Step 5. Split the data into training and testing sets using train_test_split function.
Step 6. Create a DecisionTreeClassifier object, fit the model to the training data, and visualize
the decision tree using plot_tree.
Step 7. Create a RandomForestClassifier object with 100 estimators, fit the model to the
training data, and visualize the random forest by displaying 6 trees.
Step 8. Print the accuracy of the decision tree and random forest models using the score
method on the test data.

Program:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.ensemble import RandomForestClassifier

# read the data


data = pd.read_csv(“E:/dataset/flower.csv”)
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values

# encode the labels


le = LabelEncoder()
y = le.fit_transform(y)

# split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

# create and fit a decision tree model


tree = DecisionTreeClassifier().fit(X_train, y_train)
# visualize the decision tree
plt.figure(figsize=(10,6))
plot_tree(tree, filled=True)
plt.title("Decision Tree")
plt.show()

# create and fit a random forest model


rf = RandomForestClassifier(n_estimators=100, random_state=0).fit(X_train, y_train)

# visualize the random forest


plt.figure(figsize=(20,12))
for i, tree_in_forest in enumerate(rf.estimators_[:6]):
plt.subplot(2, 3, i+1)
plt.axis('off')
plot_tree(tree_in_forest, filled=True, rounded=True)
plt.title("Tree " + str(i+1))
plt.suptitle("Random Forest")
plt.show()

# calculate and print the accuracy of decision tree and random forest
print("Accuracy of decision tree: {:.2f}".format(tree.score(X_test, y_test)))
print("Accuracy of random forest: {:.2f}".format(rf.score(X_test, y_test)))

Sample flowers.csv
Sepal_length,Sepal_width,Petal_length,Petal_width,Flower
4.6,3.2,1.4,0.2,Rose
5.3,3.7,1.5,0.2,Rose
5,3.3,1.4,0.2,Rose
7,3.2,4.7,1.4,Jasmin
6.4,3.2,4.5,1.5,Jasmin
7.1,3,5.9,2.1,Lotus
6.3,2.9,5.6,1.8,Lotus
Output:

Accuracy of decision tree: 0.50


Accuracy of random forest: 1.00

Result:
Thus the Python program to build decision tree and random forest was developed
successfully.
Ex. No.7 SVM MODELS

Date:

Aim:
To write a Python program to build SVM model.

Algorithm:

Step 1.Import the necessary libraries (matplotlib.pyplot, numpy, and svm from sklearn).
Step 2.Define the features (X) and labels (y) for the fruit dataset.
Step 3.Create an SVM classifier with a linear kernel using svm.SVC(kernel='linear').
Step 4.Train the classifier on the fruit data using clf.fit(X, y).
Step 5.Plot the fruits and decision boundary using plt.scatter(X[:, 0], X[:, 1], c=colors),
where colors is a list of colors assigned to each fruit based on its label.
Step 6.Create a meshgrid to evaluate the decision function using
np.meshgrid(np.linspace(xlim[0], xlim[1], 100), np.linspace(ylim[0], ylim[1], 100)).
Step 7.Use the decision function to create a contour plot of the decision boundary and
margins using ax.contour(xx, yy, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--']).
Step 8.Show the plot using plt.show().

Program:

import matplotlib.pyplot as plt


import numpy as np
from sklearn import svm

# Define the fruit features (size and color)


X = np.array([[5, 2], [4, 3], [1, 7], [2, 6], [5, 5], [7, 1], [6, 2], [5, 3], [3, 6], [2, 7], [6, 3], [3, 3],
[1, 5], [7, 3], [6, 5], [2, 5], [3, 2], [7, 5], [1, 3], [4, 2]])

# Define the fruit labels (0=apples, 1=oranges)


y = np.array([0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0])

# Create an SVM classifier with a linear kernel


clf = svm.SVC(kernel='linear')

# Train the classifier on the fruit data


clf.fit(X, y)

# Plot the fruits and decision boundary


colors = ['red' if label == 0 else 'yellow' for label in y]
plt.scatter(X[:, 0], X[:, 1], c=colors)
ax = plt.gca()
ax.set_xlabel('Size')
ax.set_ylabel('Color')
xlim = ax.get_xlim()
ylim = ax.get_ylim()

# Create a meshgrid to evaluate the decision function


xx, yy = np.meshgrid(np.linspace(xlim[0], xlim[1], 100), np.linspace(ylim[0], ylim[1], 100))
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

# Plot the decision boundary and margins


ax.contour(xx, yy, Z, colors='k', levels=[-1, 0, 1], alpha=0.5, linestyles=['--', '-', '--'])
plt.show()

Output:

Result:

Thus, the Python program to build an SVM model was developed, and the output was
successfully verified.
IMPLEMENT ENSEMBLING TECHNIQUES
Ex. No.8

Date:

Aim:
The aim of ensembling is to combine the predictions of multiple individual
models, known as base models, in order to produce a final prediction that is more
accurate and reliable than any individual model. (Voting,Bagging,Boosting)

Algorithm:
1. Load the dataset and split it into training and testing sets.
2. Choose the base models to be included in the ensemble.
3. Train each base model on the training set.
4. Combine the predictions of the base models using the chosen ensembling
technique (voting, bagging, boosting, etc.).
5. Evaluate the performance of the ensemble model on the testing set.
6. If the performance is satisfactory, deploy the ensemble model for making
predictions on new data.

Program:

# import required libraries


from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression

#load sample dataset


iris=datasets.load_iris()

#split dataset into training and testing sets


X_train,X_test,y_train,y_test=train_test_split(iris.data,iris.target,test_size=0.3)

#build individual models


svc_model = SVC(kernel='linear', probability=True)
rf_model = RandomForestClassifier(n_estimators=10)
lr_model=LogisticRegression()

#create ensemble model


ensemble = VotingClassifier(estimators=[('svc', svc_model), ('rf', rf_model), ('lr',lr_model)], voting='soft')
# train ensemble model
ensemble.fit(X_train,y_train)

# make predictions on test set


y_pred=ensemble.predict(X_test)

#printensemble modelaccuracy
print("EnsembleAccuracy:",ensemble.score(X_test,y_test))

EnsembleAccuracy: 0.9333333333333333

Result:

Thus the program for implement ensemble techniques is executed successfully


and output is verified.
IMPLEMENT CLUSTERING ALGORITHMS
Ex. No.9

Date:

Aim:
The aim of clustering is to find patterns and structure in data that may not be
immediately apparent, and to discover relationships and associations between data points

Algorithm:

1. Data preparation: The first step is to prepare the data that we want to cluster. This may
involve data cleaning, normalization, and feature extraction, depending on the type and
quality of the data.
2. Choosing a distance metric: The next step is to choose a distance metric or similarity
measure that will be used to determine the similarity between data points. Common
distance metrics include Euclidean distance, Manhattan distance, and cosine similarity.
3. Choosing a clustering algorithm: There are many clustering algorithms available, each
with its own strengths and weaknesses. Some popular clustering algorithms include
KMeans, Hierarchical clustering, and DBSCAN.
4. Choosing the number of clusters: Depending on the clustering algorithm chosen, we
may need to specify the number of clusters we want to form. This can be done using
domain knowledge or by using techniques such as the elbow method or silhouette
analysis.
5. Cluster assignment: Once the clusters have been formed, we need to assign each data
point to its nearest cluster based on the chosen distance metric.
6. Interpretation and evaluation: Finally, we need to interpret and evaluate the results of
the clustering algorithm to determine if the clustering has produced meaningful and
useful insights.

Program:

from sklearn.datasets import make_blobs


from sklearn.cluster import KMeans, AgglomerativeClustering
import matplotlib.pyplot as plt

# Generate a random dataset with 100 samples and 4 clusters


X, y = make_blobs(n_samples=100, centers=4, random_state=42)

# Create a K-Means clustering object with 4 clusters


kmeans = KMeans(n_clusters=4, random_state=42)

# Fit the K-Means model to the dataset


kmeans.fit(X)
# Create a scatter plot of the data colored by K-Means cluster assignment
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_)
plt.title("K-Means Clustering")
plt.show()

# Create a Hierarchical clustering object with 4 clusters


hierarchical = AgglomerativeClustering(n_clusters=4)

# Fit the Hierarchical model to the dataset


hierarchical.fit(X)

# Create a scatter plot of the data colored by Hierarchical cluster assignment


plt.scatter(X[:, 0], X[:, 1], c=hierarchical.labels_)
plt.title("Hierarchical Clustering")
plt.show()

Output:

Result:

Thus the program is executed successfully and output is verified.


IMPLEMENT EM FOR BAYESIAN NETWORKS
Ex. No.10

Date:

Aim:
The aim of implementing EM for Bayesian networks is to learn the parameters of
the network from incomplete or noisy data. This involves estimating the conditional
probability distributions (CPDs) for each node in the network given the observed data.
The EM algorithm is particularly useful when some of the variables are hidden or
unobserved, as it can estimate the likelihood of the hidden variables based on the
observed data.

Algorithm:

1. Initialization: Initialize the parameters of the Bayesian network. This could involves randomly
initializing the CPDs or using some other initialization strategy.
2. Expectation(E): Given the current parameter estimates, compute the expected values of the hidden
variables using the observed data.
3. Maximization(M): Update the parameter estimates based on the observed data and the estimated
values of the hidden variables.\
4. Convergence Check: Repeat the E and M steps until the parameters converge or until a maximum
number of iterations is reached.

Program:

import numpy as np

# Toy Bayesian network structure: A -> B


# A is observed, B is hidden

# True parameters
true_prob_A = 0.7
true_prob_B_A = np.array([[0.9, 0.1],
[0.2, 0.8]])

# Generate observed data


np.random.seed(42)
observed_A = np.random.choice([0, 1], size=100, p=[1-true_prob_A, true_prob_A])

# EM Algorithm
def expectation_step(prob_A, prob_B_A, observed_A):
# Compute posterior probabilities of B given A
posterior_B_A = prob_B_A[:, observed_A]
return posterior_B_A
def maximization_step(observed_A, posterior_B_A):
# Update parameter estimates
prob_A = np.mean(observed_A)
prob_B_A = np.dot(posterior_B_A, observed_A.T) / np.sum(posterior_B_A, axis=1, keepdims=True)
return prob_A, prob_B_A

# Initialize parameters
prob_A = 0.5
prob_B_A = np.array([[0.5, 0.5],
[0.5, 0.5]])

# EM iterations
max_iterations = 100
tolerance = 1e-6
for i in range(max_iterations):
# E-step
posterior_B_A = expectation_step(prob_A, prob_B_A, observed_A)
# M-step
new_prob_A, new_prob_B_A = maximization_step(observed_A, posterior_B_A)
# Check for convergence
if np.abs(new_prob_A - prob_A) < tolerance and np.all(np.abs(new_prob_B_A - prob_B_A) < tolerance):
print(f"Converged after {i+1} iterations.")
break
# Update parameters
prob_A = new_prob_A
prob_B_A = new_prob_B_A

print("Estimated parameters:")
print("P(A):", prob_A)
print("P(B|A):", prob_B_A)

Output:

Converged after 2 iterations.


Estimated parameters:
P(A): 0.66
P(B|A): [[0.66 0.66]
[0.66 0.66]]

Result:

Thus the program is executed successfully and output is verified.


Ex.No. 11 BUILD SIMPLE NN MODELS

Date:

Aim:
The aim of building simple neural network (NN) models is to create a basic architecture that can
learn patterns from data and make predictions based on the input. This can involve defining the
structure of the NN, selecting appropriate activation functions, and tuning the hyperparameters to
optimize the performance of the model.
Algorithm:
1. Data preparation: Preprocess the data to make it suitable for training the NN. This may involve
normalizing the input data, splitting the data into training and validation sets, and encoding the
output variables if necessary.
2. Define the architecture: Choose the number of layers and neurons in the NN, and define the
activation functions for each layer. The input layer should have one neuron per input feature, and the
output layer should have one neuron per output variable.
3. Initialize the weights: Initialize the weights of the NN randomly, using a small value to avoid
saturating the activation functions.
4. Forward propagation: Feed the input data forward through the NN, applying the activation
functions at each layer, and compute the output of the NN.
5. Compute the loss: Calculate the error between the predicted output and the true output, using a
suitable loss function such as mean squared error or cross-entropy.
6. Backward propagation: Compute the gradient of the loss with respect to the weights, using the
chain rule and backpropagate the error through the NN to adjust the weights.
7. Update the weights: Adjust the weights using an optimization algorithm such as stochastic
gradient descent or Adam, and repeat steps 4-7 for a fixed number of epochs or until the
performance on the validation set stops improving.
8. Evaluate the model: Test the performance of the model on a held-out test set and report the
accuracy or other performance metrics.

Program:
import tensorflow as tf
from tensorflow import keras

# Load the MNIST dataset


(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Normalize the input data


x_train = x_train / 255.0
x_test = x_test / 255.0

# Define the model architecture


model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dropout(0.2),
keras.layers.Dense(10)
])
# Compile the model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])

# Train the model


model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))

# Evaluate the model


test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print('Test accuracy:', test_acc)

Output:
Epoch 1/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.2949 - accuracy: 0.91
36 - val_loss: 0.1438 - val_accuracy: 0.9571
Epoch 2/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1455 - accuracy: 0.95
73 - val_loss: 0.0998 - val_accuracy: 0.9701
Epoch 3/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1089 - accuracy: 0.96
75 - val_loss: 0.0860 - val_accuracy: 0.9728
Epoch 4/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0883 - accuracy: 0.97
25 - val_loss: 0.0785 - val_accuracy: 0.9760
Epoch 5/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0751 - accuracy: 0.97
67 - val_loss: 0.0729 - val_accuracy: 0.9785
Epoch 6/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0654 - accuracy: 0.97
97 - val_loss: 0.0675 - val_accuracy: 0.9788
Epoch 7/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0577 - accuracy: 0.98
16 - val_loss: 0.0676 - val_accuracy: 0.9792
Epoch 8/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0548 - accuracy: 0.98
16 - val_loss: 0.0640 - val_accuracy: 0.9813
Epoch 9/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0475 - accuracy: 0.98
44 - val_loss: 0.0679 - val_accuracy: 0.9794
Epoch 10/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0449 - accuracy: 0.98
51 - val_loss: 0.0714 - val_accuracy: 0.9803
313/313 - 0s - loss: 0.0714 - accuracy: 0.9803 - 250ms/epoch - 799us/step
Test accuracy: 0.9803000092506409

Result:
Thus the program is executed successfully and output is verified.
Ex.No. 12 BUILD DEEP LEARNING NN MODELS

Date:

Aim:
The aim of building deep learning neural network (NN) models is to create a more complex
architecture that can learn hierarchical representations of data, allowing for more accurate
predictions and better generalization to new data. Deep learning models are typically characterized
by having many layers and a large number of parameters.

Algorithm:
1. Data preparation: Preprocess the data to make it suitable for training the NN. This may involve
normalizing the input data, splitting the data into training and validation sets, and encoding the
output variables if necessary.
2. Define the architecture: Choose the number of layers and neurons in the NN, and define the
activation functions for each layer. Deep learning models typically use activation functions such as
ReLU or variants thereof, and often incorporate dropout or other regularization techniques to
prevent overfitting.
3. Initialize the weights: Initialize the weights of the NN randomly, using a small value to avoid
saturating the activation functions.
4. Forward propagation: Feed the input data forward through the NN, applying the activation
functions at each layer, and compute the output of the NN.
5. Compute the loss: Calculate the error between the predicted output and the true output, using a
suitable loss function such as mean squared error or cross-entropy.
6. Backward propagation: Compute the gradient of the loss with respect to the weights, using the
chain rule and backpropagate the error through the NN to adjust the weights.
7. Update the weights: Adjust the weights using an optimization algorithm such as stochastic
gradient descent or Adam, and repeat steps 4-7 for a fixed number of epochs or until the
performance on the validation set stops improving.
8. Evaluate the model: Test the performance of the model on a held-out test set and report the
accuracy or other performance metrics. 9. Fine-tune the model: If necessary, fine-tune the model by
adjusting the hyperparameters or experimenting with different architectures.

Program:
import tensorflow as tf
from tensorflow import keras
# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Normalize the input data


x_train = x_train / 255.0
x_test = x_test / 255.0

# Define the model architecture


model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

# Train the model


model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))

# Evaluate the model


test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print('Test accuracy:', test_acc)
Output:
Epoch 1/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.2534 - accuracy: 0.92
69 - val_loss: 0.1419 - val_accuracy: 0.9576
Epoch 2/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.1115 - accuracy: 0.96
68 - val_loss: 0.1040 - val_accuracy: 0.9669
Epoch 3/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0767 - accuracy: 0.97
73 - val_loss: 0.0783 - val_accuracy: 0.9754
Epoch 4/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.0570 - accuracy: 0.98
25 - val_loss: 0.0757 - val_accuracy: 0.9768
Epoch 5/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0440 - accuracy: 0.98
65 - val_loss: 0.0690 - val_accuracy: 0.9782
Epoch 6/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0346 - accuracy: 0.98
94 - val_loss: 0.0813 - val_accuracy: 0.9763
Epoch 7/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.0272 - accuracy: 0.99
18 - val_loss: 0.0821 - val_accuracy: 0.9775
Epoch 8/10
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0219 - accuracy: 0.99
31 - val_loss: 0.0763 - val_accuracy: 0.9783
Epoch 9/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.0187 - accuracy: 0.99
42 - val_loss: 0.0817 - val_accuracy: 0.9778
Epoch 10/10
1875/1875 [==============================] - 3s 1ms/step - loss: 0.0150 - accuracy: 0.99
55 - val_loss: 0.0733 - val_accuracy: 0.9805
313/313 - 0s - loss: 0.0733 - accuracy: 0.9805 - 267ms/epoch - 853us/step
Test accuracy: 0.9804999828338623

Result:
Thus the program is executed successfully and output is verified.

You might also like