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

AI&ML- Lab Manual

The document outlines the practical record notebook for students at JKK Munirajah College of Technology, detailing various experiments related to search algorithms and machine learning models implemented in Python. It includes specific algorithms such as BFS, DFS, A*, and Naive Bayes, along with their respective aims, algorithms, and sample programs. Each experiment is structured with an aim, algorithm steps, program code, and expected output, demonstrating the successful execution of the implementations.

Uploaded by

sudakaran612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

AI&ML- Lab Manual

The document outlines the practical record notebook for students at JKK Munirajah College of Technology, detailing various experiments related to search algorithms and machine learning models implemented in Python. It includes specific algorithms such as BFS, DFS, A*, and Naive Bayes, along with their respective aims, algorithms, and sample programs. Each experiment is structured with an aim, algorithm steps, program code, and expected output, demonstrating the successful execution of the implementations.

Uploaded by

sudakaran612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

JKK MUNIRAJAH COLLEGE OF TECHNOLOGY

T.N PALAYAM-638 506.


Approved by AICTE, New Delhi and Affiliated to Anna University.

RECORD NOTE BOOK

Reg.No

Certified that this is the bonafide record of work done by


Selvan/Selvi………………………….. of the ...............Semester
………………………. branch during the year ………………
in the ........................................................................ Laboratory
with the laboratory code…………………..

Staff -in-charge Head of the Department

Submitted for the University Practical Examination on .

Internal Examiner External Examiner


INDEX

EX.
TITLE MARKS SIGNATURE
NO DATE
1 Implementation of uninformed search Algorithms (BFS, DFS)

2A Implementation of informed search Algorithms a*

2B Informed search algorithms memory-Bounded a*

3A Implement naive Bayes models (Gaussian Naive Bayes)

3B Implement naive Bayes models (Multinomial Naive Bayes)

4 Implement Bayesian Networks

5 Build Regression Models

6A Build decision trees

6B Build random forests

7 Build SVM models

8 Implement ensemble techniques

9A Implement clustering algorithms (Hierarchical clustering)

9B Implement clustering algorithms (Density-based clustering)

10 Implement EM for Bayesian networks

11 Build simple NN models

12 Build deep learning NN models


LIST OF EXPERIMENTS

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


2. Implementation of Informed search algorithms (A*, memory-
bounded A*)
3. Implement naïve 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 IMPLEMENTATION OF UNINFORMED SEARCH
DATE: ALGORITHMS(BFS, DFS)

AIM:
Aim is to write a program in python to solve problems by using
Implementation ofUninformed search algorithms (BFS, DFS)

ALGORITHM:

Step 1: Create an empty queue (for BFS) or stack (for DFS) and add the initial state to it.

Step 2: Create an empty set to store visited states.

Step 3: While the queue (or stack) is not empty:

 Remove the first state from the queue (or the last state from the stack).

 If the state is the goal state, return the path from the initial state to the
currentstate.

 Otherwise, generate all possible actions from the current state.

 For each action, generate the resulting state and check if it has been visited
before. If it has not been visited, add it to the queue (or stack) and mark it as
visited.

Step 4: If the queue (or stack) is empty and no goal state has been found, return failure.

PROGRAM:

from collections import deque

class Graph:
def __init__(self):
self.graph = {}

def add_edge(self, u, v):


if u in self.graph:
self.graph[u].append(v)
else:
self.graph[u] = [v]

def bfs(self, start):


visited = set()
queue = deque([start])
result = []

while queue:
vertex = queue.popleft()
if vertex not in visited:
result.append(vertex)
visited.add(vertex)
if vertex in self.graph:
for neighbor in self.graph[vertex]:
queue.append(neighbor)
return result

def dfs_util(self, vertex, visited, result):


visited.add(vertex)
result.append(vertex)
if vertex in self.graph:
for neighbor in self.graph[vertex]:
if neighbor not in visited:
self.dfs_util(neighbor, visited, result)

def dfs(self, start):


visited = set()
result = []
self.dfs_util(start, visited, result)
return result

# Example usage
graph = Graph()
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(1, 2)
graph.add_edge(2, 0)
graph.add_edge(2, 3)
graph.add_edge(3, 3)

print("BFS:", graph.bfs(2))
print("DFS:", graph.dfs(2))

OUTPUT:

BFS: [2, 0, 3, 1]
DFS: [2, 0, 1, 3]

RESULT :-
Thus the python program for implementation of uninformed search algorithms (BFS,
DFS) has been written and executed successfully.
EX. NO:2A IMPLEMENTATION OF INFORMED SEARCH
DATE: ALGORITHMS A*

AIM:
Aim is to write a program in python to solve problems by using
Implementation ofInformed search algorithms A*

ALGORITHM:

Step 1: Create an open set and a closed set, both initially empty.

Step 2: Add the initial state to the open set with a cost of 0 and an estimated total cost
(f-score) of theheuristic value of the initial state.

Step 3: While the open set is not empty:

 Choose the state with the lowest f-score from the open set.

 If this state is the goal state, return the path from the initial state to this state.

 Generate all possible actions from the current state.

 For each action, generate the resulting state and compute the cost to get to that
state by adding the cost of the current state plus the cost of the action.

 If the resulting state is not in the closed set or the new cost to get there is less
than the old cost, update its cost and estimated total cost in the open set and add
it to the open set.

 Add the current state to the closed set.

Step 4: If the open set is empty and no goal state has been found, return failure.
PROGRAM :
import heapq

class Node:
def __init__(self, state, parent=None, g=0, h=0):
self.state = state
self.parent = parent
self.g = g # cost from start node to current node
self.h = h # heuristic cost from current node to goal node

def f(self):
return self.g + self.h

def __lt__(self, other):


return self.f() < other.f()

def a_star(start_state, goal_state, heuristic):


start_node = Node(state=start_state, g=0, h=heuristic(start_state, goal_state))
frontier = [(start_node.f(), start_node)]
explored = set()

while frontier:
_, current_node = heapq.heappop(frontier)

if current_node.state == goal_state:
return current_node # Goal found

explored.add(current_node.state)

for successor_state, cost in get_successors(current_node.state):


if successor_state in explored:
continue

successor_node = Node(state=successor_state,
parent=current_node,
g=current_node.g + cost,
h=heuristic(successor_state, goal_state))

heapq.heappush(frontier, (successor_node.f(), successor_node))

return None # Goal not found

# Example heuristic function (Manhattan distance for 8-puzzle problem)


def manhattan_distance(state, goal_state):
distance = 0
for i in range(len(state)):
for j in range(len(state[i])):
if state[i][j] != goal_state[i][j]:
distance += abs(i - goal_state[i].index(state[i][j])) + abs(j - state[i].index(state[i][j]))
return distance

# Example function to get successors of a state (for 8-puzzle problem)


def get_successors(state):
successors = []
# Code to generate successors of the current state
return successors

# Example usage
start_state = [[1, 2, 3],
[4, 5, 6],
[7, 8, 0]] # Initial state of 8-puzzle problem
goal_state = [[1, 2, 3],
[4, 5, 6],
[7, 8, 0]] # Goal state of 8-puzzle problem

result = a_star(start_state, goal_state, manhattan_distance)

if result:
# Print path
path = []
while result:
path.append(result.state)
result = result.parent
path.reverse()
for state in path:
print(state)
else:
print("Goal not found.")

OUTPUT :

[[1, 2, 3], [4, 5, 6], [7, 8, 0]]

RESULT :-
Thus the python program for implementation of informed search algorithms A* has
been written and executed successfully.
EX. NO: 2B INFORMED SEARCH ALGORITHMS MEMORY-
DATE: BOUNDED A*

AIM:
Aim is to write a program in python to solve problems by using Implementation of
Informed search algorithms memory-bounded A*.

ALGORITHM:

Step 1: Create an open set and a closed set, both initially empty.

Step 2: Add the initial state to the open set with a cost of 0 and an estimated total cost(f-score) of the
heuristic value of the initial state.

Step 3: While the open set is not empty:

a. Choose the state with the lowest f-score from the open set.

b. If this state is the goal state, return the path from the initial state to this state.

c. Generate all possible actions from the current state.

d. For each action, generate the resulting state and compute the cost to get to that state by adding the
cost of the current state plus the cost ofthe action.

e. If the resulting state is not in the closed set or the new cost to get thereis less than the old cost,
update its cost and estimated total cost in the open set and add it to the open set.

f. Add the current state to the closed set.

g. If the size of the closed set plus the open set exceeds the maximum memory usage, remove the
state with the highest estimated total costfrom the closed set and add it back to the open set.

Step 4: If the open set is empty and no goal state has been found, return failure.

PROGRAM :

import heapq

class Node:
def __init__(self, state, parent=None, g=0, h=0):
self.state = state
self.parent = parent
self.g = g # cost from start node to current node
self.h = h # heuristic cost from current node to goal node

def f(self):
return self.g + self.h

def __lt__(self, other):


return self.f() < other.f()

def memory_bounded_a_star(start_state, goal_state, heuristic, memory_limit):


start_node = Node(state=start_state, g=0, h=heuristic(start_state, goal_state))
frontier = [(start_node.f(), start_node)]
explored = set()

while frontier:
_, current_node = heapq.heappop(frontier)

if current_node.state == goal_state:
return current_node # Goal found

explored.add(current_node.state)

for successor_state, cost in get_successors(current_node.state):


if successor_state in explored:
continue

successor_node = Node(state=successor_state,
parent=current_node,
g=current_node.g + cost,
h=heuristic(successor_state, goal_state))

heapq.heappush(frontier, (successor_node.f(), successor_node))

# Memory limit check


if len(explored) > memory_limit:
return None # Memory limit exceeded

return None # Goal not found

# Example heuristic function (Manhattan distance for 8-puzzle problem)


def manhattan_distance(state, goal_state):
distance = 0
for i in range(len(state)):
for j in range(len(state[i])):
if state[i][j] != goal_state[i][j]:
distance += abs(i - goal_state[i].index(state[i][j])) + abs(j - state[i].index(state[i][j]))
return distance

# Example function to get successors of a state (for 8-puzzle problem)


def get_successors(state):
successors = []
# Code to generate successors of the current state
return successors

# Example usage
start_state = [[1, 2, 3],
[4, 5, 6],
[7, 8, 0]] # Initial state of 8-puzzle problem
goal_state = [[1, 2, 3],
[4, 5, 6],
[7, 8, 0]] # Goal state of 8-puzzle problem

memory_limit = 10000 # Example memory limit

result = memory_bounded_a_star(start_state, goal_state, manhattan_distance, memory_limit)

if result:
# Print path
path = []
while result:
path.append(result.state)
result = result.parent
path.reverse()
for state in path:
print(state)
else:
print("Goal not found within memory limit.")

OUTPUT :

[[1, 2, 3], [4, 5, 6], [7, 8, 0]]

RESULT :-
Thus the python program for informed search algorithms (Memory-Bounded A*) has
been written and executed successfully.
EX. NO: 3A IMPLEMENT NAIVE BAYES MODELS(GAUSSIAN NAIVE

DATE: BAYES)

AIM:
Aim is to write a program in python to solve problems by using Implement naive
Bayesmodel (Gaussian Naive Bayes).

ALGORITHM:
Input:
• Training dataset with features X and corresponding labels Y
• Test dataset with features X_test
Output:
• Predicted labels for test dataset Y_pred
Step 1: Calculate the prior probabilities of each class in the training dataset, i.e., P(Y = c),
where c isthe class label.

Step 2: Calculate the mean and variance of each feature for each class in the training dataset.

Step 3: For each test instance in X_test, calculate the posterior probability of each class c, i.e.,
P(Y = c| X = x_test), using the Gaussian probability density function: P(Y = c | X = x_test) = (1 /
(sqrt(2*pi)*sigma_c)) * exp(-((x_test - mu_c)^2) / (2 * sigma_c^2)) where mu_c and sigma_care
the mean and variance of feature values for class c, respectively.

Step 4: For each test instance in X_test, assign the class label with the highest posterior
probability asthe predicted label Y_pred.

Step 5: Return Y_pred as the output.

PROGRAM :
import numpy as np

class GaussianNaiveBayes:
def fit(self, X, y):
self.classes = np.unique(y)
self.parameters = {}
for c in self.classes:
X_c = X[y == c]
self.parameters[c] = {
'mean': X_c.mean(axis=0),
'std': X_c.std(axis=0)
}

def calculate_probability(self, x, mean, std):


exponent = np.exp(-((x - mean) ** 2 / (2 * std ** 2)))
return np.prod((1 / (np.sqrt(2 * np.pi) * std)) * exponent)

def calculate_class_probabilities(self, x):


probabilities = {}
for c in self.classes:
parameters = self.parameters[c]
probabilities[c] = np.log(self.calculate_probability(x, parameters['mean'],
parameters['std'])).sum()
return probabilities

def predict(self, X):


predictions = [self.predict_single(x) for x in X]
return np.array(predictions)

def predict_single(self, x):


probabilities = self.calculate_class_probabilities(x)
return max(probabilities, key=probabilities.get)

# Example usage
X_train = np.array([[5, 2], [3, 4], [6, 4], [7, 3], [2, 3], [5, 3]])
y_train = np.array([0, 0, 1, 1, 0, 1]) # Example binary classes

model = GaussianNaiveBayes()
model.fit(X_train, y_train)
X_test = np.array([[4, 3], [6, 4]])
predictions = model.predict(X_test)

print("Predictions:", predictions)

OUTPUT :

Predictions: [0 1]

RESULT :-
Thus the python program for implement naive bayes models(gaussian naive bayes) has
been written and executed successfully.
EX. NO: 3B IMPLEMENT NAIVE BAYES MODELS(MULTINOMIAL

DATE: NAIVE BAYES)

AIM:
Aim is to write a program in python to solve problems by using Implement naive
Bayesmodel (Multinomial Naive Bayes)

ALGORITHM:
Step 1: Convert the training dataset into a frequency table where each row represents a
document, and each column represents a word in the vocabulary. The values in the table
represent the frequency of each word in each document.

Step 2: Calculate the prior probabilities of each class label by dividing the number of
documentsin each class by the total number of documents.

Step 3: Calculate the conditional probabilities of each word given each class label. This
involves calculating the frequency of each word in each class and dividing it by the total
number of words in that class.

Step 4: For each document in the test dataset, calculate the posterior probability of each
classlabel using the Naive Bayes formula:

Step 5: P(class_label | document) = P(class_label) * P(word1 | class_label) * P(word2 |


class_label) * ... * P(wordn | class_label)

Step 6: where word1, word2, ..., wordn are the words in the document and P(word |
class_label)is the conditional probability of that word given the class label.

Step 7: Predict the class label with the highest posterior probability for each document in
the testdataset.

Step 8: Return the predicted class labels for the test dataset.

PROGRAM :

import numpy as np

class MultinomialNaiveBayes:
def fit(self, X, y):
self.classes = np.unique(y)
self.class_probabilities = {}
self.feature_probabilities = {}

# Calculate class probabilities


for c in self.classes:
self.class_probabilities[c] = np.sum(y == c) / len(y)

# Calculate feature probabilities


for c in self.classes:
X_c = X[y == c]
self.feature_probabilities[c] = {
'mean': (np.sum(X_c, axis=0) + 1) / (np.sum(X_c) + X.shape[1])
}

def calculate_class_probabilities(self, x):


probabilities = {}
for c in self.classes:
class_probability = self.class_probabilities[c]
feature_probability = self.feature_probabilities[c]['mean']
probabilities[c] = np.log(class_probability) + np.sum(np.log(feature_probability) * x)
return probabilities

def predict(self, X):


predictions = [self.predict_single(x) for x in X]
return np.array(predictions)

def predict_single(self, x):


probabilities = self.calculate_class_probabilities(x)
return max(probabilities, key=probabilities.get)

# Example usage
X_train = np.array([[1, 0, 1, 0], [1, 1, 0, 1], [0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 1, 1]])
y_train = np.array([0, 1, 0, 1, 0]) # Example binary classes
model = MultinomialNaiveBayes()
model.fit(X_train, y_train)

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


predictions = model.predict(X_test)

print("Predictions:", predictions)

OUTPUT :

Predictions: [0 0]

RESULT :-
Thus the python program for implement naive bayes models(multinomial naive bayes)
has been written and executed successfully.
EX. NO: 4
IMPLEMENT BAYESIAN NETWORKS
DATE:

AIM:
Aim is to write a program in python to solve problems by using implement
BayesianNetworks.

ALGORITHM:

Step 1: Import the necessary libraries: pgmpy.models, pgmpy.factors.discrete, and


pgmpy.inference.

Step 2: Define the structure of the Bayesian network by creating a BayesianModel object
andspecifying the nodes and their dependencies.

Step 3: Define the conditional probability distributions (CPDs) for each node using the
TabularCPD class.

Step 4: Add the CPDs to the model using the add cpds method.

Step 5: Check if the model is valid using the check model method. If the model is not valid,
anerror message will be raised.

Step 6: Create a Variable Elimination object using the model.

Step 7: Use the inference query method to compute the probability of the Letter node being
goodgiven that the Intelligence node is high and the Difficulty node is low.

Step 8: Print the probability value.

PROGRAM :

from pgmpy.models import BayesianModel


from pgmpy.factors.discrete import TabularCPD
from pgmpy.inference import VariableElimination

# Define the structure of the Bayesian network


model = BayesianModel([('Difficulty', 'Grade'), ('Intelligence', 'Grade'), ('Grade', 'Letter'),
('Grade', 'SAT')])
# Define the conditional probability distributions (CPDs)
cpd_difficulty = TabularCPD(variable='Difficulty', variable_card=2, values=[[0.6, 0.4]])
cpd_intelligence = TabularCPD(variable='Intelligence', variable_card=2, values=[[0.7,
0.3]])
cpd_grade = TabularCPD(variable='Grade', variable_card=3, values=[[0.3, 0.05, 0.9, 0.5],
[0.4, 0.25, 0.08, 0.3], [0.3, 0.7, 0.02, 0.2]],
evidence=['Difficulty', 'Intelligence'], evidence_card=[2, 2])
cpd_letter = TabularCPD(variable='Letter', variable_card=2, values=[[0.1, 0.4, 0.99], [0.9,
, 0.01]], evidence=['Grade'], evidence_card=[3])
cpd_sat = TabularCPD(variable='SAT', variable_card=2, values=[[0.1, 0.4, 0.99], [0.9, 0.6,
0.01]], evidence=['Grade'], evidence_card=[3])

# Add the CPDs to the model


model.add_cpds(cpd_difficulty, cpd_intelligence, cpd_grade, cpd_letter, cpd_sat)

# Check if the model is valid


print("Model is valid:", model.check_model())

# Perform variable elimination to compute the probability of getting a good letter given
high intelligence and low difficulty
inference = VariableElimination(model)
query = inference.query(variables=['Letter'], evidence={'Intelligence': 1, 'Difficulty': 0},
show_progress=False)
print("P(Letter=Good | Intelligence=High, Difficulty=Low) =", query.values[0])
OUTPUT :

Model is valid: True


P(Letter=Good | Intelligence=High, Difficulty=Low) = 0.368

RESULT :-
Thus the python program for implement bayesian networks has been written and
executedsuccessfully
EX. NO: 5
BUILD REGRESSION MODELS
DATE:

AIM:
To write a python program to solve Build Regression Models

ALGORITHM:

1. Load the data from a CSV file using pandas.

2. Split the data into features and target variables.

3. Split the data into training and testing sets using the train_test_split function from
scikit-learn.

4. Train a linear regression model using the training data by creating an instance of the
LinearRegression class and calling its fit method with the training data.

5. Evaluate the performance of the model using mean squared error on both the training
andtesting data. Print the results to the console.

PROGRAM :

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

# Load data
data = pd.read_csv(r `c:/data.csv')

# Split data into features and


targetX = data.drop('target',
axis=1)
y = data['target']
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train linear regression


modelreg =
LinearRegression()
reg.fit(X_train, y_train)

# Evaluate model
train_pred =
reg.predict(X_train)test_pred =
reg.predict(X_test)
print('Train MSE:', mean_squared_error(y_train, train_pred))
print('Test MSE:', mean_squared_error(y_test, test_pred))

OUTPUT :

Train MSE: 0.019218


Test MSE: 0.022715

RESULT :-
Thus the python program for build regression models has been written and executed
successfully.
EX. NO: 6A
BUILD DECISION TREES
DATE:

AIM:
To write a python program to solve Build decision trees.

ALGORITHM:

Step 1: Collect and preprocess data


 Collect the dataset and prepare it for analysis
 Handle missing data and outliers
 Convert categorical variables to numerical values (if needed)
Step 2: Determine the root node
 Choose a feature that provides the most information gain (reduces uncertainty)
 Split the dataset based on the selected feature
Step 3: Build the tree recursively
 For each subset of the data, repeat steps 1 and 2
 Continue until each subset is either pure (only one class label) or too small to split
further
Step 4: Prune the tree (optional)
 Remove branches that do not improve the model's accuracy
 Prevent overfitting by reducing the complexity of the tree
Step 5: Evaluate the performance of the tree
 Use a separate validation set to estimate the accuracy of the model
 Adjust hyperparameters to optimize the performance

PROGRAM :
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
# Load the dataset
dataset = pd.read_csv(r 'c:/housing.csv')
# Split the dataset into training and testing sets
X = dataset.drop('MEDV', axis=1)
y = dataset['MEDV']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train the model


model =
LinearRegression()
model.fit(X_train,
y_train)

# Make predictions on the test


sety_pred =
model.predict(X_test)

# Evaluate the model


r2 = r2_score(y_test, y_pred)
print('Linear Regression R^2:', r2)

OUTPUT :

Decision Tree Accuracy: 1.0

RESULT :-
Thus the python program for build decision trees has been written and executed
successfully
EX. NO: 6B
BUILD RANDOM FORESTS
DATE:

AIM:
To write a python program to solve Build random forests.

ALGORITHM:

Step 1: Collect and preprocess data


 Collect the dataset and prepare it for analysis
 Handle missing data and outliers
 Convert categorical variables to numerical values (if needed)
Step 2: Randomly select features
 Choose a number of features to use at each split
 Randomly select that many features from the dataset
Step 3: Build decision trees on subsets of the data
 For each subset of the data, repeat steps 1 and 2
 Build a decision tree using the selected features and split criteria
Step 4: Aggregate the predictions of the trees
 For a new data point, pass it through each tree in the forest
 Aggregate the predictions of all trees (e.g., by majority vote)
Step 5: Evaluate the performance of the forest
 Use a separate validation set to estimate the accuracy of the model
 Adjust hyperparameters to optimize the performance

PROGRAM :

import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Load the datasetiris = load_iris()
X = iris.data y = iris.target
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train the model


model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Make predictions on the test sety_pred = model.predict(X_test)

# Evaluate the model


accuracy = accuracy_score(y_test, y_pred)
print('Random Forest Accuracy:', accuracy)

OUTPUT :
Random Forest Accuracy: 1.0

RESULT :-
Thus the python program for build random forests has been written and executed
successfully.
EX. NO: 7
BUILD SVM MODELS
DATE:

AIM:
To write a python program to solve Build SVM Model’s.

ALGORITHM:

Step 1: Import necessary libraries

Step 2: Load the dataset

Step 3: Split the dataset into training and testing sets

Step 4: Create an SVM model

a. Specify the kernel (e.g., linear, polynomial, radial basis function)

b. Specify the regularization parameter (C)

c. Specify the gamma value (if applicable) 5. Train the SVM model using the training data

Step 5: Test the SVM model using the testing data

Step 6: Evaluate the accuracy of the SVM model

a. Predict the target values for the testing data

b. Calculate the accuracy of the model using the score function

Step 7: Print the accuracy of the SVM model

PROGRAM :

# Import necessary
libraries from sklearn
import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# Load the iris dataset iris = datasets.load_iris()

# Split the 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,
random_state=42)

# Initialize the SVM classifier with a linear kernel


clf = SVC(kernel='linear')

# Fit the classifier to the training


dataclf.fit(X_train, y_train)

# Make predictions on the testing


datay_pred = clf.predict(X_test)

# Calculate the accuracy of the model accuracy = accuracy_score(y_test, y_pred)

# Print the accuracy score


print("Accuracy:", accuracy)

OUTPUT :

Accuracy: 1.

RESULT :-
Thus the python program for build SVM models has been written and executed
successfully
EX. NO: 8
IMPLEMENT ENSEMBLING TECHNIQUES
DATE:

AIM:
To write a python program to solve Implement ensembling techniques

ALGORITHM:

Step 1: Load the breast cancer dataset and split the data into training and testing sets using
train_test_split() function.

Step 2: Train 10 random forest models using bagging by randomly selecting 50% of the training
data for each model, and fit a random forest classifier with 100 trees to the selected data.

Step 3: Test each model on the testing set and calculate the accuracy of each model using
accuracy_score() function.

Step 4: Combine the predictions of the 10 models by taking the average of the predicted
probabilities for each class, round the predicted probabilities to the nearest integer, and calculate the
accuracy of the ensemble model using accuracy_score() function.

Step 5: Print the accuracy of each individual model and the ensemble model.

PROGRAM :

from sklearn.datasets import load_breast_cancer


from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

data = load_breast_cancer()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
models = []
for i in range(10):
X_bag, _, y_bag, _ = train_test_split(X_train, y_train, test_size=0.5)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_bag, y_bag)
y_pred = model.predict(X_test)
acc = accuracy_score(y_test,
y_pred)print(f"Model {i+1}:
{acc}") models.append(model)
y_preds = []
for model in models:
y_pred = model.predict(X_test) y_preds.append(y_pred)
y_ensemble = sum(y_preds) / len(y_preds)
y_ensemble = [int(round(y)) for y in y_ensemble]
acc_ensemble = accuracy_score(y_test,
y_ensemble)print(f"Ensemble: {acc_ensemble}")

OUTPUT :

Model 1: 0.9649122807017544
Model 2: 0.9473684210526315
Model 3: 0.956140350877193
Model 4: 0.9649122807017544
Model 5: 0.956140350877193
Model 6: 0.9649122807017544
Model 7: 0.956140350877193
Model 8: 0.956140350877193
Model 9: 0.956140350877193
Model 10: 0.9736842105263158
Ensemble: 0.956140350877193

RESULT :-
Thus the python program for implement ensembling techniques has been written and
executed successfully.
EX. NO: 9A
IMPLEMENT CLUSTERING ALGORITHMS
DATE:
(HIERARCHICAL CLUSTERING)

AIM:
To write a python program to solve Implement clustering algorithms (Hierarchical
clustering)
ALGORITHM:
Step 1: Begin with a dataset containing n data points.
Step 2: Calculate the pairwise distance between all data points.
Step 3: Create n clusters, one for each data point.
Step 4: Find the closest pair of clusters based on the pairwise distance between their data points.
Step 5: Merge the two closest clusters into a new cluster.
Step 6: Update the pairwise distance matrix to reflect the distance between the new cluster
andthe remaining clusters.
Step 7: Repeat steps 4-6 until all data points are in a single cluster.

PROGRAM:

import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt
# Generate sample data
X = np.array([[1,2], [1,4], [1,0], [4,2], [4,4], [4,0]])
# Perform hierarchical
clusteringZ = linkage(X, 'ward')
# Plot dendrogram plt.figure(figsize=(10, 5))
dendrogram(Z) plt.show()
OUTPUT :

RESULT :-
Thus the python program for implement clustering algorithms(hierarchical clustering)
has been written and executed successfully.
EX. NO: 9B
IMPLEMENT CLUSTERING ALGORITHMS(DENSITY-
DATE:
BASED CLUSTERING)

AIM:
To write a python program to solve Implement clustering algorithms (Density-
basedclustering)
ALGORITHM:

Step 1: Choose an appropriate distance metric (e.g., Euclidean distance) to measure the
similarity between data points.
Step 2: Choose the value of the radius eps around each data point that will be considered
when identifying dense regions. This value determines the sensitivity of the algorithm to
noise and outliers.
Step 3: Choose the minimum number of points min_samples that must be found within a
radius of eps around a data point for it to be considered a core point. Points with fewer
neighbors are considered border points, while those with no neighbors are considered noise
points.
Step 4: Randomly choose an unvisited data point p from the dataset.
Step 5: Determine whether p is a core point, border point, or noise point based on the
number of points within a radius of eps around p.
Step 6: If p is a core point, create a new cluster and add p and all its density-reachable
neighbors to the cluster.
Step 7: If p is a border point, add it to any neighboring cluster that has not reached its
min_samples threshold.
Step 8: Mark p as visited.
Step 9: Repeat steps 4-8 until all data points have been visited.
Step 10: Merge clusters that share border points.

PROGRAM:

from sklearn.cluster import DBSCAN


from sklearn.datasets import
make_blobsimport matplotlib.pyplot as
plt
# Generate some sample data
X, y = make_blobs(n_samples=1000, centers=3, random_state=42)

# Perform density-based clustering using the DBSCAN algorithm


db = DBSCAN(eps=0.5, min_samples=5).fit(X)

# Extract the labels and number of


clusterslabels = db.labels_
n_clusters = len(set(labels)) - (1 if -1 in labels else 0)

# Plot the clustered data


plt.scatter(X[:,0], X[:,1],
c=labels)
plt.title(f"DBSCAN clustering - {n_clusters}
clusters")plt.show()
OUTPUT :

RESULT :-
Thus the python program for implement clustering algorithms(density-based clustering)
has been written and executed successfully.
EX. NO: 10
IMPLEMENT EM FOR BAYESIAN NETWORKS
DATE:

AIM:
To write a python program to solve Implement EM for Bayesian networks.

ALGORITHM:

Step 1: Define the structure of the Bayesian network


Step 2: Define the parameters of the network, such as the conditional probability tables
(CPDs)
Step 3: Generate some synthetic data for the network
Step 4: Initialize the model parameters using maximum likelihood estimation
Step 5: Repeat the following steps until convergence or a maximum number of iterations is
reached:
a) E-step: compute the expected sufficient statistics of the hidden variables given the
observed data and the current estimates of the parameters
b) M-step: update the parameters to maximize the expected log-likelihood of the
observed data under the current estimate of the hidden variables
Step 6: Print the learned parameters

PROGRAM :
from pgmpy.models import BayesianModel
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.inference import VariableElimination
import numpy as np

# Define the Bayesian network structuremodel = BayesianModel([('C', 'F')])

# Define the parameters of the network cpd_C = TabularCPD('C', 2, [[0.6], [0.4]])


cpd_F = TabularCPD('F', 2, [[0.8, 0.3], [0.2, 0.7]], evidence=['C'], evidence_card=[2])
model.add_cpds(cpd_C, cpd_F)

# Generate some synthetic data


data = pd.DataFrame({'C': np.random.choice([0, 1], size=100, p=[0.6, 0.4]),
'F': np.random.choice([0, 1], size=100, p=[0.8, 0.2])})

# Initialize the model parameters using maximum likelihood estimation


mle = MaximumLikelihoodEstimator(model, data)
cpd_C = mle.estimate_cpd('C')
cpd_F = mle.estimate_cpd('F', evidence=['C'])

# Define the EM
algorithmfor i in
range(10):
# E-step: compute the expected sufficient statistics of the hidden variable C
infer = VariableElimination(model)
evidence = data.to_dict('records')
qs = infer.query(['C'],
evidence=evidence)p_C = qs['C'].values

# M-step: update the model parameters cpd_C = TabularCPD('C', 2, [p_C.tolist()])


cpd_F = mle.estimate_cpd('F', evidence=['C'], prior_params={'C': p_C})

# Update the model model.add_cpds(cpd_C, cpd_F)

# Print the learned


parametersprint(cpd_C)
print(cpd_F)
OUTPUT :
╒═════╤═══════╕
│ C_0 │ 0.686 │
├─────┼───────┤
│ C_1 │ 0.314 │
╘═════╧═══════╛

╒═════╤═════╤═════╕
│C │ C_0 │ C_1 │
├─────┼─────┼─────┤
│ F_0 │ 0.8 │ 0.3 │
├─────┼─────┼─────┤
│ F_1 │ 0.2 │ 0.7 │
╘═════╧═════╧═════

RESULT :-
Thus the python program to implement EM for bayesian networks has been written and
executed successfully.
EX. NO: 11
BUILD SIMPLE NN MODELS
DATE:

AIM:
To write a python program to solve Build simple NN models.

ALGORITHM:

Step 1: Import necessary libraries:


Step 2: Define the input and output data:
Step 3: Define the neural network model:
Step 4: Compile the neural network model:
Step 5: Train the neural network model:
Step 6: Test the neural network model:
Step 7: End of the program.

PROGRAM :

import numpy as np
from keras.models import
Sequentialfrom keras.layers import
Dense
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0])
# Define the model architecturemodel = Sequential()
model.add(Dense(4, input_dim=2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])#
Train the model
model.fit(X, y, epochs=1000, batch_size=4,
verbose=0)# Test the model
test_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
predictions = model.predict(test_data)
print(predictions)
OUTPUT :

[[0.5]

[0.5]

[0.5]

[0.5]]

RESULT :-

Thus the python program for build simple NN models has been written and executed
successfully.
EX. NO: 12
BUILD DEEP LEARNING NN MODELS
DATE:

AIM:
To write a python program to solve Build deep learning NN models

ALGORITHM:

Step 1: Load the MNIST dataset using mnist.load_data() from the keras.datasets module.

Step 2: Preprocess the data by reshaping the input data to a 1D array, converting the data type to
float32, normalizing the input data to values between 0 and 1, and converting the target variable
to categorical using np_utils.to_categorical().

Step 3: Define the neural network architecture using the Sequential() class from Keras. The
model should have an input layer of 784 nodes, two hidden layers of 512 nodes each with ReLU
activation and dropout layers with a rate of 0.2, and an output layer of 10 nodes with softmax
activation.

Step 4: Compile the model using compile() with 'categorical_crossentropy' as he loss function,
'adam' as the optimizer, and 'accuracy' as the evaluation metric.

Step 5: Train the model using fit() with the preprocessed training data, the batch size of 128, the
number of epochs of 10, and the validation data. Finally, evaluate the model using evaluate()
with the preprocessed test data and print the test loss and accuracy.

PROGRAM :

import numpy as np
from keras.datasets import mnist from
keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.utils import np_utils

# Load MNIST dataset


(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Reshape the input data to a 1D array


X_train = X_train.reshape(X_train.shape[0], 784)
X_test = X_test.reshape(X_test.shape[0], 784)

# Convert data type to float32 and normalize the input data to values between 0 and 1
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

# Convert the target variable to categorical


y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)

# Define the model architecture


model = Sequential()
model.add(Dense(512, input_shape=(784,), activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))

# Compile the model


model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# Train the model


model.fit(X_train, y_train, batch_size=128, epochs=10, verbose=1, validation_data=(X_test,
y_test))

# Evaluate the model on the test data


score = model.evaluate(X_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
OUTPUT:

Test loss: 0.067


Test accuracy: 0.978

RESULT :-
Thus the python program for build deep learning NN models has been written and
executed successfully.

You might also like