0% found this document useful (0 votes)
21 views38 pages

AI&ML Document

The document is a laboratory manual for the B.Tech Information Technology program at P.A. College of Engineering and Technology for the academic year 2024-2025. It includes a list of experiments related to Artificial Intelligence and Machine Learning, detailing algorithms and Python programs for various search algorithms, Naïve Bayes, and Bayesian networks. Each experiment outlines the aim, algorithm, program, output, and results of the implementations.

Uploaded by

mano haran
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)
21 views38 pages

AI&ML Document

The document is a laboratory manual for the B.Tech Information Technology program at P.A. College of Engineering and Technology for the academic year 2024-2025. It includes a list of experiments related to Artificial Intelligence and Machine Learning, detailing algorithms and Python programs for various search algorithms, Naïve Bayes, and Bayesian networks. Each experiment outlines the aim, algorithm, program, output, and results of the implementations.

Uploaded by

mano haran
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/ 38

P. A.

COLLEGE OF ENGINEERING AND


TECHNOLOGY
(An Autonomous Institution, Accredited by NAAC with `A` Grade)
PALLADAM ROAD, POLLACHI – 642 002

B.Tech Information Technology


(Accredited by NBA)
LABORATORY MANUAL

22ITPC502/ Artificial Intelligence and Machine


Learning Laboratory

SEMESTER V
ACADEMIC YEAR 2024 - 2025

Prepared By

Mrs. E.GAYATHIRI AP/IT


P. A. COLLEGE OF ENGINEERING AND
TECHNOLOGY
(An Autonomous Institution, Accredited by NAAC with `A` Grade)
PALLADAM ROAD, POLLACHI – 642 002

B.Tech Information Technology


(Accredited by NBA)
LABORATORY MANUAL

22ITPC502/ Artificial Intelligence and Machine


Learning Laboratory

SEMESTER V
ACADEMIC YEAR 2024 – 2025

Prepared By HOD Principal

(E.Gayathiri)
LIST OF EXPERIMENTS

PAGE
S.NO TITLE OF THE EXPERIMENT
NO
1.
Implement of Uninformed search algorithm (BFS,DFS)

2.
Implement 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 the clustering algorithms.

10.
Implement EM for Bayesian Networks

11.
Build simple NN Models

12
Build deep learning NN Models.
EX.NO:1A
DATE: Implementation of uninformed search algorithm(BFS)

AIM:

To implement the python program for breadth first search algorithm.

ALGORITHM:

step 1: start by putting any one of the graph’s vertices at the back of the queue.
step 2: Now take the front item of the queue and add it to the visited list.
step 3: 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 4: Keep continuing steps two and three till the queue is empty.
step 5: stop the program.

PROGRAM :

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

visited = []
queue = []
def bfs(visited, graph, node):
visited.append(node)
queue.append(node)
while queue:
m = queue.pop(0)
print (m, end = " ")
for neighbour in graph[m]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)
print("Following is the Breadth-First Search")
bfs(visited, graph, '5')
OUTPUT:

Following is the Breadth-First Search

537248

RESULT:

Thus the given python program for breadth first search was executed successfully
and the output was verified.

EX.NO:1B
DATE: Implementation of uninformed search algorithm(DFS)

AIM:

To implement the python program for depth first search algorithm.

ALGORITHM:

step 1:We will start by putting any one of the graph's vertex on top of the stack.
step 2:After that take the top item of the stack and add it to the visited list of the
vertex.
step 3: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 4:Lastly, keep repeating steps 2 and 3 until the stack is empty.
step 5:stop the program.
PROGRAM:

graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
visited = set()
def dfs(visited, graph, node):
if node not in visited:
print (node)
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour
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 given python program for depth first search was executed successfully and
the output was verified.

EX.NO:2A
DATE:
Implementation of informed search algorithm(A* SEARCH
ALGORITHM)

AIM:

To implement the python program for A* search algorithm.

ALGORITHM:

step 1:place the starting node in the open list.


step 2:check if the open list is empty or not,if the list is empty then return failure and
stops.
step 3:select the node from the open list which has the smallest value of evaluation
function (g+h),if node n is goal node,then return success and stop,otherwise.
step 4:expand node n and generate all of its successor,and put n into the closed
list.for each successor n',check whether n' is already in the open on closed list,if
not then compute evaluation function for n' and place into openlist .
step 5:else if node n' is already in open and closed,then it should be attached to the
back pointer which reflects the lowest g(n') value.
step 6:return to step 2.

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': 2,
'C': 3,
'D': 0
}
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 given python program for A* search algorithm was executed successfully
and output was verified.

EX.NO:2B
DATE:
Implementation of informed search algorithm(MEMORY
BOUNDED A* SEARCH ALGORITHM)

AIM:

To implement the python program for memory bounded A* search algorithm.

ALGORITHM:

Step 1: Create a priority queue and push the starting node onto the queue.
Step 2: Create a set to store the visited nodes.
Step 3: Set a counter to keep track of the number of nodes expanded.
Step 4: Repeat the following steps until the queue is empty or the node counter
exceeds the
max_nodes:
4.1: Pop the node with the lowest cost + heuristic from the queue.
4.2: If the current node is the goal, return the path to the goal.
4.3: If the current node has already been visited, skip it.
4.4: Mark the current node as visited.
: Increment the node counter.
: Expand the current node and add its neighbors to the queue.
Step 5: If the queue is empty and the goal has not been found, return None (no path
found)
PROGRAM:
import heapq

class Node:
def __init__(self, state, parent, cost, heuristic):
self.state = state
self.parent = parent
self.cost = cost
self.heuristic = heuristic

def __lt__(self, other):


return (self.cost + self.heuristic) < (other.cost + other.heuristic)

def astar(start, goal, graph, max_nodes):


heap = []
heapq.heappush(heap, (0, Node(start, None, 0, 0)))
visited = set()
node_counter = 0

while heap and node_counter < max_nodes:


(cost, current) = heapq.heappop(heap)

if current.state == goal:
path = []
while current is not None:
path.append(current.state)
current = current.parent
return path[::-1]

if current.state in visited:
continue

visited.add(current.state)
node_counter += 1

for state, cost in graph[current.state].items():


if state not in visited:
heuristic = 0 # For now, heuristic is 0 (same as Dijkstra's)
heapq.heappush(heap, (cost, Node(state, current, current.cost + cost,
heuristic)))

return None

# Example usage
graph = {
'A': {'B': 1, 'C': 4},
'B': {'A': 1, 'C': 2, 'D': 5},
'C': {'A': 4, 'B': 2, 'D': 1},
'D': {'B': 5, 'C': 1}
}
start = 'A'
goal = 'D'
max_nodes = 10

result = astar(start, goal, graph, max_nodes)


print(result)

OUTPUT:
['A', 'B', 'C', 'D']
Result:
Thus the python program for memory-bounded A* search was developed and the
output was verified successfully

EX.NO:3
DATE: IMPLEMENTATION OF NAÏVE BAYES NETWORK

AIM:

To implement the python program for naïve bayes network algorithm.

ALGORITHM:

Step 1: start the program


Step 2: calculate the prior probability for the given class labels
Step 3:find likelihood probability with each attribute for each class
Step 4:put these values in bayes formula and calculate the posterior probability
Step 5:see which class has a higher probability,given the input belongs to the higher
probability class.
Step 6:stop the program

PROGRAM :

import numpy as np
import matplotlib.pyplot as plt
import networkx as nx

class NaiveBayesNetwork:
def __init__(self):
self.class_probabilities = {}
self.feature_probabilities = {}
self.class_feature_dependencies = {}

def train(self, X, y):


total_samples = len(y)
classes, counts = np.unique(y, return_counts=True)
for cls, count in zip(classes, counts):
self.class_probabilities[cls] = count / total_samples

num_features = X.shape[1]
for cls in classes:
class_samples = X[y == cls]
self.feature_probabilities[cls] = []
self.class_feature_dependencies[cls] = {}
for feature_idx in range(num_features):
feature_values = class_samples[:, feature_idx]
unique_values, value_counts = np.unique(feature_values,
return_counts=True)
feature_prob = {value: (count + 1) / (len(class_samples) +
len(unique_values))
for value, count in zip(unique_values, value_counts)}
self.feature_probabilities[cls].append(feature_prob)
self.class_feature_dependencies[cls][feature_idx] = None

def predict(self, X):


predictions = []
for sample in X:
max_prob = -1
predicted_class = None
for cls in self.class_probabilities:
class_prob = self.class_probabilities[cls]
feature_probs = self.feature_probabilities[cls]
likelihood = 1
for feature_idx, feature_value in enumerate(sample):
if feature_value in feature_probs[feature_idx]:
likelihood *= feature_probs[feature_idx][feature_value]
else:
likelihood *= 1 / (len(self.feature_probabilities[cls][feature_idx]) + 1)
posterior = class_prob * likelihood
if posterior > max_prob:
max_prob = posterior
predicted_class = cls
predictions.append(predicted_class)
return predictions

def visualize_network(self):
G = nx.DiGraph()
for cls, prob in self.class_probabilities.items():
G.add_node(f'Class: {cls}\nProbability: {prob:.2f}', color='lightblue',
style='filled')
for cls in self.class_probabilities:
for feature_idx, feature_probs in enumerate(self.feature_probabilities[cls]):
node_label = '\n'.join([f'{val}: {prob:.2f}' for val, prob in
feature_probs.items()])
G.add_node(f'Feature {feature_idx}\n{node_label}', color='lightgreen',
style='filled')
G.add_edge(f'Class: {cls}\nProbability: {self.class_probabilities[cls]:.2f}',
f'Feature {feature_idx}\n{node_label}')

pos = nx.spring_layout(G) # Layout for the graph nodes


node_colors = [node[1]['color'] for node in G.nodes(data=True)]
node_styles = [node[1]['style'] for node in G.nodes(data=True)]
nx.draw_networkx_nodes(G, pos, node_color=node_colors, node_size=700,
alpha=0.8)
nx.draw_networkx_edges(G, pos, width=1.0, alpha=0.5)
nx.draw_networkx_labels(G, pos, font_size=8, font_family='sans-serif',
font_weight='bold', verticalalignment='center')
plt.title('Naive Bayes Network')
plt.axis('off')
plt.show()
if __name__ == "__main__":
X_train = np.array([
['Sunny', 'Hot', 'High', 'Weak'],
['Sunny', 'Hot', 'High', 'Strong'],
['Overcast', 'Hot', 'High', 'Weak'],
['Rain', 'Mild', 'High', 'Weak'],
['Rain', 'Cool', 'Normal', 'Weak'],
['Rain', 'Cool', 'Normal', 'Strong'],
['Overcast', 'Cool', 'Normal', 'Strong'],
['Sunny', 'Mild', 'High', 'Weak'],
['Sunny', 'Cool', 'Normal', 'Weak'],
['Rain', 'Mild', 'Normal', 'Weak'],
['Sunny', 'Mild', 'Normal', 'Strong'],
['Overcast', 'Mild', 'High', 'Strong'],
['Overcast', 'Hot', 'Normal', 'Weak'],
['Rain', 'Mild', 'High', 'Strong']
])
y_train = np.array(['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes',
'Yes', 'Yes', 'No'])
nb_network = NaiveBayesNetwork()
nb_network.train(X_train, y_train)
nb_network.visualize_network()

OUTPUT:
RESULT:

Thus the given python program for naïve baiyes network was executed
successfully executed and output was verified.

EX NO: 4
DATE: BAYESIAN NETWORKS

AIM:

To implement the bayesian network algorithm using python program.

ALGORITHM:
Step 1: **Initialization:**
- Define probabilities for Burglary and Earthquake.
- Calculate complementary probabilities for no Burglary and no Earthquake.
Step 2: **Conditional Probabilities:**
- Define conditional probabilities for Alarm given different combinations of Burglary
and Earthquake.
- Define conditional probabilities for JohnCalls and MaryCalls given Alarm.
Step 3: **Joint Probability Calculation Function:**
- Create a function `calculate_probability` that computes the joint probability of
Alarm, JohnCalls, and MaryCalls given Burglary and Earthquake.
Step 4:
- `calculate_probability` function to compute specific joint probabilities:
Step 5: **Execution:**
- Print the results of each example to demonstrate the computation of joint
probabilities based on the Bayesian network structure.

PROGRAM:

P_Burglary = 0.001 # Probability of Burglary


P_NotBurglary = 1 - P_Burglary
P_Earthquake = 0.002 # Probability of Earthquake
P_NotEarthquake = 1 - P_Earthquake
# Conditional probabilities for Alarm given Burglary and Earthquake
P_Alarm_given_Burglary_Earthquake = {
(True, True): 0.95,
(True, False): 0.94,
(False, True): 0.29,
(False, False): 0.001
}
# Conditional probabilities for JohnCalls and MaryCalls given Alarm
P_JohnCalls_given_Alarm = {
True: 0.9,
False: 0.1
}
P_MaryCalls_given_Alarm = {
True: 0.7,
False: 0.3
}
# Function to calculate joint probability of Alarm, JohnCalls, MaryCalls
def calculate_probability(Burglary, Earthquake, Alarm, JohnCalls, MaryCalls):
if Burglary and Earthquake:
P_Alarm_given_BE = P_Alarm_given_Burglary_Earthquake[(True, True)]
elif Burglary and not Earthquake:
P_Alarm_given_BE = P_Alarm_given_Burglary_Earthquake[(True, False)]
elif not Burglary and Earthquake:
P_Alarm_given_BE = P_Alarm_given_Burglary_Earthquake[(False, True)]
elif not Burglary and not Earthquake:
P_Alarm_given_BE = P_Alarm_given_Burglary_Earthquake[(False, False)]
# Calculate P(JohnCalls | Alarm)
if Alarm:
P_JohnCalls_given_A = P_JohnCalls_given_Alarm[True]
else:
P_JohnCalls_given_A = P_JohnCalls_given_Alarm[False]
# Calculate P(MaryCalls | Alarm)
if Alarm:
P_MaryCalls_given_A = P_MaryCalls_given_Alarm[True]
else:
P_MaryCalls_given_A = P_MaryCalls_given_Alarm[False]
if JohnCalls and MaryCalls:
joint_probability = P_Alarm_given_BE * P_JohnCalls_given_A
*P_MaryCalls_given_A
elif JohnCalls and not MaryCalls:
joint_probability = P_Alarm_given_BE * P_JohnCalls_given_A * (1 -
P_MaryCalls_given_A)
elif not JohnCalls and MaryCalls:
joint_probability = P_Alarm_given_BE * (1 - P_JohnCalls_given_A)
*P_MaryCalls_given_A
else:
joint_probability = P_Alarm_given_BE * (1 - P_JohnCalls_given_A) * (1 -
P_MaryCalls_given_A)
return joint_probability
print("Example 1: P(Alarm=True, JohnCalls=True, MaryCalls=True |
Burglary=True,Earthquake=False)")
prob1 = calculate_probability(Burglary=True, Earthquake=False,
Alarm=True,JohnCalls=True, MaryCalls=True)
print(prob1)
print("\nExample 2: P(Alarm=False, JohnCalls=True, MaryCalls=False |
Burglary=False,Earthquake=True)")
prob2 = calculate_probability(Burglary=False, Earthquake=True,
Alarm=False,JohnCalls=True, MaryCalls=False)
print(prob2)
OUTPUT:

Example 1: P(Alarm=True, JohnCalls=True, MaryCalls=True |


Burglary=True,Earthquake=False)
0.5922

Example 2: P(Alarm=False, JohnCalls=True, MaryCalls=False |


Burglary=False,Earthquake=True)
0.0203

RESULT:
Thus the given bayesian network program was executed successfully and Output
was verified.

EX NO: 5
DATE: BUILD REGRESSION MODEL

AIM:

To implement the regression model algorithm using python program.

ALGORITHM:

Step 1: Create two lists: one for the number of hours studied and another for the
scores obtained.
Step 2: Change the shape of the hours studied list so that it can be used in the
model.
Step 3: Use a linear regression model from the sklearn library.
• Train this model with the hours studied as the input and the scores as the
output.
Step 4 :Choose a number of hours you want to predict the score for.
• Use the trained model to predict the score for that number of hours.
Step 5: Print out the predicted score for the given hours of study.
Step 6: Create a scatter plot to show the original data points (hours studied vs.
scores).
• Draw a line to show the model's predictions.
Step 7: Add a title, labels for the axes, a legend, and grid lines to make the plot
easier to understand.
Step 8: Show the plot to see how the study hours relate to the scores, with the
prediction line included.

PROGRAM :

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# Sample data - Hours studied vs. Scores obtained
hours_studied = np.array([2.5, 5.1, 3.2, 8.5, 3.5, 1.5, 9.2, 5.5, 8.3, 2.7])
scores_obtained = np.array([21, 47, 27, 75, 30, 20, 88, 60, 81, 25])
# Reshape the input data for sklearn
X = hours_studied.reshape(-1, 1)
y = scores_obtained
# Applying Linear Regression using sklearn
model = LinearRegression()
model.fit(X, y)
# Predicting scores for new data
hours_new = np.array([[6.5]]) # Example: Predict score for 6.5 hours studied
score_predicted = model.predict(hours_new)
print(f'Predicted score for {hours_new[0][0]} hours of study: {score_predicted[0]}')
# Plotting the data and the linear regression line
plt.figure(figsize=(8, 6))
plt.scatter(X, y, color='blue', label='Data points')
plt.plot(X, model.predict(X), color='red', linewidth=2, label='Linear Regression')
plt.title('Hours Studied vs. Scores Obtained')
plt.xlabel('Hours Studied')
plt.ylabel('Scores Obtained')
plt.legend()
plt.grid(True)
plt.show()

OUTPUT :

Predicted score for 6.5 hours of study: 61.4891638225256


RESULT :

Thus the given regression model program was executed successfully and Output
was verified.

EX NO: 6A
DATE : BUILD DECISION TREES

AIM:

To implement the decision trees algorithm using python program.

ALGORITHM:
Step 1: Import the necessary tools:
• Use pandas for managing data.
• Use train_test_split to divide data into training and testing sets.
• Use DecisionTreeClassifier to create a decision tree model.
• Use accuracy_score and classification_report to check how well the model
works..
Step 2: Create the data:
• Make a list of data with details like 'Age', 'Gender', 'Income', and 'Purchase'.
Step 3: Make a DataFrame:
• Turn the data list into a table format using pandas.
Step 4: Convert categories to numbers:
• Change the 'Gender' column from words ('Male', 'Female') to numbers (0, 1).
Step 5: Get the data ready for the model:
• Split the table into two parts: one for features (X) and one for the target (y).
Step 6: Split the data into training and testing sets:
• Use train_test_split to divide the data into two parts: 80% for training the
model and 20% for testing it.
Step 7: Build the decision tree model:
• Create a decision tree model with a random seed to ensure consistent results.
• Train the model using the training data.
Step 8: Predict outcomes:
• Use the trained model to guess the target values for the test data.
• Print the accuracy score.

PROGRAM :

# Importing necessary libraries


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, classification_report
import warnings

# Suppressing warnings for the purpose of this example


warnings.filterwarnings("ignore", category=UserWarning, module='sklearn')

# Sample data
data = {
'Age': [20, 25, 30, 35, 40],
'Gender': ['Male', 'Female', 'Male', 'Female', 'Male'],
'Income': [30000, 45000, 60000, 110000, 120000],
'Purchase': [0, 0, 1, 1, 1]
}

# Creating a DataFrame
df = pd.DataFrame(data)

# Encoding categorical variable 'Gender'


df['Gender'] = df['Gender'].map({'Male': 0, 'Female': 1})

# Splitting the dataset into features and target


X = df.drop('Purchase', axis=1)
y = df['Purchase']

# Splitting 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)

# Printing the DataFrame


print("\nDataset:")
print(df)

# Creating a decision tree classifier


clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)

# Making predictions
y_pred = clf.predict(X_test)

# Evaluating the model


accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')

# Classification report
print(classification_report(y_test, y_pred, zero_division='warn'))
OUTPUT :

Dataset:
Age Gender Income Purchase
0 20 0 30000 0
1 25 1 45000 0
2 30 0 60000 1
3 35 1 110000 1
4 40 0 120000 1

Accuracy: 1.00

precision recall f1-score support


1.00 1.00 1.00 1

accuracy 1.00 1
macro avg 1.00 1.00 1.00 1
weighted avg 1.00 1.00 1.00 1

RESULT :

Thus the given decision tree program was executed successfully and Output was
verified.

EX NO: 6b
DATE: BUILD RANDOM FORESTS

AIM:
To implement the random forests algorithm using python program

ALGORITHM:
Step 1: Import the necessary tools:
• Use pandas for managing data.
• Use train_test_split to divide data into training and testing sets.
• Use DecisionTreeClassifier to create a decision tree model.
• Use accuracy_score and classification_report to check how well the model
works..
Step 2: Create the data:
• Make a list of data with details like 'Age', 'income', 'familysize', and 'buy
product'.
Step 3: Make a DataFrame:
• Change Turn the data list into a table format using pandas.
Step 4: Get the data ready for the model:
• Split the table into two parts: one for features (X) and one for the target (y).
Step 5: Split the data into training and testing sets:
• Use train_test_split to divide the data into two parts: 80% for training the
model and 20% for testing it.
Step 6: Predict outcomes:
• Use the trained model to guess the target values for the test data.
• Print the accuracy score.

PROGRAM :

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

# Creating a hypothetical dataset


np.random.seed(0)
data_size = 1000

# Generate random data


age = np.random.randint(18, 70, data_size)
income = np.random.randint(20000, 100000, data_size)
family_size = np.random.randint(1, 10, data_size)

# Randomly generate whether the person buys the product or not


buy_product = (age + income / 1000 + family_size) > (np.mean(age) +
np.mean(income / 1000) + np.mean(family_size))

# Convert boolean to int (0 or 1)


buy_product = buy_product.astype(int)

# Create a DataFrame
data = pd.DataFrame({
'Age': age,
'Income': income,
'FamilySize': family_size,
'BuyProduct': buy_product
})
# Display the first few rows of the dataset
print(data.head())

# Split the data into features and target variable


X = data[['Age', 'Income', 'FamilySize']]
y = data['BuyProduct']

# Split the dataset into training and testing sets


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

# Create a Random Forest Classifier


model = RandomForestClassifier(n_estimators=100, random_state=0)

# Train the model


model.fit(X_train, y_train)

# Make predictions on the test set


y_pred = model.predict(X_test)

# Evaluate the model


accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy:.2f}')

# Print classification report


print('Classification Report:')
print(classification_report(y_test, y_pred))
# Print confusion matrix
print('Confusion Matrix:')
print(confusion_matrix(y_test, y_pred))

OUTPUT:

Age Income FamilySize BuyProduct


0 62 73478 3 1
1 65 65560 4 1
2 18 91592 5 1
3 21 88246 6 1
4 21 34591 9 0

Accuracy: 0.96

Classification Report:
precision recall f1-score support

0 0.96 0.95 0.95 137


1 0.96 0.96 0.96 163
accuracy 0.96 300
macro avg 0.96 0.96 0.96 300
weighted avg 0.96 0.96 0.96 300

Confusion Matrix:
[[130 7]
[ 6 157]]

RESULT :

Thus the given random forests program was executed successfully and Output
was verified.
EX NO: 7
DATE: BUILD SVM MODELS

AIM:

To implement the SVM model algorithm using python program.

ALGORITHM:
Step 1: Load the Iris Dataset:
• The code uses the Iris dataset, which has details about different iris flowers,
including the length and width of their petals and sepals.
• The goal is to figure out which of three species each flower belongs to.
Step 2: Prepare the Data:
• The dataset is divided into two parts: 70% for training the model and 30% for
testing it.
Step 3: Create the Model:
• The code creates an SVM (Support Vector Machine) model that uses a
straight line to separate the different flower species based on their features.
Step 4: Train the Model:
• The model learns from the training data how to classify the flowers.
Step 5: Make Predictions:
• The model predicts the species of the flowers in the test set.
Step 6: Evaluate the Model:
• The model's accuracy is measured, showing how many predictions were
correct.
• A report is generated to show how well the model did for each flower species.

PROGRAM :

# Import necessary libraries


import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report, accuracy_score

# Load the Iris dataset


iris = datasets.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.3,
random_state=42)
# Create an SVM model with a linear kernel
model = SVC(kernel='linear', C=1.0, random_state=42)
# Train the model
model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = model.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred, target_names=iris.target_names)
# Print results
print(f'Accuracy: {accuracy:.2f}')
print('Classification Report:')
print(report)

OUTPUT :

Accuracy: 1.00
Classification Report:
precision recall f1-score support

setosa 1.00 1.00 1.00 19


versicolor 1.00 1.00 1.00 13
virginica 1.00 1.00 1.00 13

accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45

RESULT :

Thus the given SVM model program was executed successfully and Output was
verified.

EX NO: 8
DATE: IMPLEMENT THE ENSEMBLING TECHNIQUES

AIM:

To implement the ensembling techniques using python program.


ALGORITHM:

Step 1: start the program.


Step 2: import the necessary packages
Step 3:create a synthetic dataset for an random variable.
Step 4:convert into dataframe and display the dataset details.
Step 5:define the base models and voting classifier.
Step 6:train and evaluate the voting classifier and the plot the results.
Step 7:stop the program.

PROGRAM :

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score

# Create a synthetic dataset


np.random.seed(42)
data = {
&#39;Age&#39;: np.random.randint(22, 60, 100),
&#39;Salary&#39;: np.random.randint(40000, 100000, 100),
&#39;Experience&#39;: np.random.randint(1, 30, 100),
&#39;Department_IT&#39;: np.random.randint(0, 2, 100),
&#39;Department_HR&#39;: np.random.randint(0, 2, 100),
&#39;Attrition&#39;: np.random.randint(0, 2, 100) # Target variable
}

# Convert to DataFrame
df = pd.DataFrame(data)

# Display dataset details


print(&quot;Dataset Details:&quot;)
print(df.head())
print(&quot;\nDataset Statistics:&quot;)
print(df.describe())

# Define features and target variable


X = df.drop(&#39;Attrition&#39;, axis=1)
y = df[&#39;Attrition&#39;]
# 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)

# Define base models


models = {
&#39;RandomForest&#39;: RandomForestClassifier(random_state=42),
&#39;DecisionTree&#39;: DecisionTreeClassifier(random_state=42),
&#39;SVC&#39;: SVC(kernel=&#39;linear&#39;, probability=True,
random_state=42) # SVC needs probability=True for
VotingClassifier
}

# Create a Voting Classifier


voting_model = VotingClassifier(
estimators=[(name, model) for name, model in models.items()],
voting=&#39;soft&#39; # Use soft voting to consider predicted probabilities
)

# Train and evaluate models


results = {}
for name, model in models.items():
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
results[name] = accuracy

# Train and evaluate Voting Classifier


voting_model.fit(X_train, y_train)

y_pred_voting = voting_model.predict(X_test)
accuracy_voting = accuracy_score(y_test, y_pred_voting)
results[&#39;Voting&#39;] = accuracy_voting

# Print accuracy results


print(&quot;\nModel Accuracy Results:&quot;)
for model_name, accuracy in results.items():
print(f&quot;{model_name}: {accuracy * 100:.2f}%&quot;)

# Pick a single test sample for prediction


single_test_sample = X_test.iloc[0:1]

# Get predictions from each model


predictions = {}
for name, model in models.items():
pred = model.predict(single_test_sample)
predictions[name] = pred[0]

# Get Voting Classifier prediction


voting_prediction = voting_model.predict(single_test_sample)
predictions[&#39;Voting&#39;] = voting_prediction[0]

# Display predictions for the single test sample


print(&quot;\nPredictions for the Single Test Sample:&quot;)
for model_name, prediction in predictions.items():
print(f&quot;{model_name}: {prediction}&quot;)

# Determine and display majority prediction


from collections import Counter
majority_vote = Counter(predictions.values()).most_common(1)[0][0]
print(f&quot;\nMajority Vote Prediction: {majority_vote}&quot;)

# Prepare data for plotting


model_names = list(results.keys())
accuracies = [accuracy * 100 for accuracy in results.values()]

# Create DataFrame for plotting

plot_df = pd.DataFrame({&#39;Model&#39;: model_names, &#39;Accuracy&#39;:


accuracies})

# Plot results
plt.figure(figsize=(10, 6))
sns.barplot(data=plot_df, x=&#39;Model&#39;, y=&#39;Accuracy&#39;,
hue=&#39;Model&#39;, palette=&#39;viridis&#39;)
plt.xlabel(&#39;Model&#39;)
plt.ylabel(&#39;Accuracy (%)&#39;)
plt.title(&#39;Model Accuracy Comparison&#39;)
plt.ylim(0, 100)
plt.show()

OUTPUT:

Dataset Details:
Age Salary Experience Department_IT Department_HR Attrition
0 50 45056 5 1 0 0
1 36 72482 17 1 0 1
2 29 87716 24 0 0 1
3 42 48110 17 0 1 1
4 40 53773 27 1 0 0

Dataset Statistics:
Age Salary ... Department_HR Attrition
count 100.000000 100.000000 ... 100.000000 100.000000
mean 40.060000 72238.460000 ... 0.560000 0.540000
std 10.688255 18027.025536 ... 0.498888 0.500908
min 22.000000 40206.000000 ... 0.000000 0.000000
25% 30.000000 57954.000000 ... 0.000000 0.000000
50% 41.500000 71135.500000 ... 1.000000 1.000000
75% 48.000000 89313.750000 ... 1.000000 1.000000
max 59.000000 99581.000000 ... 1.000000 1.000000

[8 rows x 6 columns]

Model Accuracy Results:

RandomForest: 40.00%
DecisionTree: 45.00%
SVC: 50.00%
Voting: 45.00%

Predictions for the Single Test Sample:


RandomForest: 0
DecisionTree: 0
SVC: 1
Voting: 0

Majority Vote Prediction: 0


RESULT:

Thus the given ensembling technique was executed successfully and the output
was verified.

EX NO: 9
DATE: IMPLEMENT THE CLUSTERING ALGORITHM

AIM:

To implement the clustering algorithm using python program.

ALGORITHM:

Step 1: start the program.


Step 2: import the necessary packages
Step 3:create a synthetic dataset for an random variable.
Step 4:create a dataset with ‘n”samples of datapoints and “n” of clusters.
Step 5:apply the “k” means of clustering
Step 6:plot the each cluster with an unique color and show the results.
Step 7:stop the program.

PROGRAM :

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs

# Generate synthetic data


n_samples = 300
n_features = 2
n_clusters = 4
random_state = 42

# Create a dataset with 'n_samples' data points and 'n_clusters' clusters


X, y = make_blobs(n_samples=n_samples, n_features=n_features,
centers=n_clusters, cluster_std=0.60, random_state=random_state)

# Apply K-Means clustering


kmeans = KMeans(n_clusters=n_clusters, random_state=random_state)
kmeans.fit(X)

# Get the cluster centers and labels


centers = kmeans.cluster_centers_
labels = kmeans.labels_

# Plot the data points and cluster centers


plt.figure(figsize=(8, 6))

# Plot each cluster with a unique color


for i in range(n_clusters):
plt.scatter(X[labels == i, 0], X[labels == i, 1], label=f'Cluster {i+1}')

# Plot the cluster centers


plt.scatter(centers[:, 0], centers[:, 1], s=300, c='red', label='Centroids', marker='X')

plt.title('K-Means Clustering')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.grid(True)
plt.show()

OUTPUT:
RESULT:

Thus the given clustering algorithm was executed successfully and the output was
verified.

EX NO: 10
DATE: Implement the EM for Bayesian Networks

AIM:

To implement the EM for Bayesian Networks using python program.

ALGORITHM:

Step 1: start the program.


Step 2: import the necessary packages
Step 3:initialise the sample data and determine the no.of data points.
Step 4:initialise the standard deviation and mean arrays and print the values.
Step 5:Determine the maximum value among the calculated log-likelihood values.
By using this formula:

logL= −∑((x−mu[i]^2)/(2×(sigma[i]^2))

Step 6:Print this maximum log-likelihood values.


Step 7:stop the program.

PROGRAM:

import numpy as np
import math
x = np.array([[1, 2, 3, 4, 5]])
n = np.shape(x)[1]
print("The sample data: \n", x)
print("The number of data present in x is:", n)
mu = np.array([[1, 2, 3, 2, 3]])
sigma = np.array([[1, 2, 2, 3, 3]])
print("\nThe mean is: ", mu)
print("The st. dev is: ", sigma)
print("\nMu Sigma logL")
print("----------------------")
logL_values = []

for i in range(5):
logL = -np.sum(np.square(x - mu[0, i]) / (2 * sigma[0, i] ** 2))
logL_values.append(logL)
print(f"{mu[0, i]:<5}{sigma[0, i]:<8}{logL:.3f}")

max_logL = max(logL_values)
print("\nMaximum logL:", f"{max_logL:.3f}")

OUTPUT:

The sample data:


[[1 2 3 4 5]]
The number of data present in x is: 5
The mean is: [[1 2 3 2 3]]
The st. dev is: [[1 2 2 3 3]]
Mu Sigma logL
----------------------
1 1 -15.000
2 2 -1.875
3 2 -1.250
2 3 -0.833
3 3 -0.553
Maximum logL: -0.556
EX NO: 11
DATE: Build Simple NN Models

AIM:

To implement the simple NN models using python program.

ALGORITHM:

Step 1: start the program.


Step 2: import the necessary packages
Step 3:create the synthetic data for 100 samples.
Step 4: split the datasets into training and testing sets
Step 5:build the neural network model and compile the model.
Step 6:create a mesh grid for plotting the decision boundary.
Step 7:stop the program.

PROGRAM:

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

# Generate synthetic data: 1000 samples, 2 features


np.random.seed(42)
X = np.random.rand(1000, 2) # 2 features
y = (X[:, 0] + X[:, 1] &gt; 1).astype(int) # Simple rule: classify if sum of features &gt; 1

# 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)

# Build the neural network model


model = keras.Sequential([
layers.Dense(5, activation=&#39;sigmoid&#39;, input_shape=(2,)), # Hidden layer
layers.Dense(1, activation=&#39;sigmoid&#39;) # Output layer for binary
classification
])

# Compile the model


model.compile(optimizer=&#39;adam&#39;,
loss=&#39;binary_crossentropy&#39;,
metrics=[&#39;accuracy&#39;])
# Train the model
model.fit(X_train, y_train, epochs=50, batch_size=10)

# Evaluate the model


loss, accuracy = model.evaluate(X_test, y_test)
print(f&quot;Test Accuracy: {accuracy * 100:.2f}%&quot;)

# Create a mesh grid for plotting decision boundary


xx, yy = np.meshgrid(np.arange(0, 1, 0.01), np.arange(0, 1, 0.01))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = (Z &gt; 0.5).astype(int).reshape(xx.shape)

# Plotting
plt.figure(figsize=(10, 6))
plt.contourf(xx, yy, Z, alpha=0.5, cmap=&#39;coolwarm&#39;) # Decision boundary
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors=&#39;k&#39;,
cmap=&#39;coolwarm&#39;) # Data points
plt.title(&#39;Decision Boundary and Data Points&#39;)
plt.xlabel(&#39;Feature 1&#39;)
plt.ylabel(&#39;Feature 2&#39;)
plt.show()

# Save the model


model.save(&#39;simple_nn_sigmoid_model.h5&#39;)

OUTPUT:

Epoch 1/50
80/80 [==============================] - 1s 3ms/step - loss: 0.6922 -
accuracy: 0.5000
Epoch 2/50
80/80 [==============================] - 0s 2ms/step - loss: 0.6900 -
accuracy: 0.5000
...
Epoch 49/50
80/80 [==============================] - 0s 2ms/step - loss: 0.6933 -
accuracy: 0.5075
Epoch 50/50
80/80 [==============================] - 0s 2ms/step - loss: 0.6925 -
accuracy: 0.5100

Test Accuracy: 51.00%


EX NO: 12
DATE: Build Deep Learning NN Models

AIM:

To implement the deep learning NN models using python program.

ALGORITHM:

Step 1: start the program.


Step 2: import the necessary packages
Step 3:load and preprocess the data.
Step 4: split the datasets into training and testing sets
Step 5:build the neural network model and compile the model.
Step 6:create a mesh grid for plotting the decision boundary and predict it.
Step 7:stop the program.

PROGRAM:

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris

# Load and preprocess data


data = load_iris()
X = data.data
y = data.target

# Normalize the features


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2,
random_state=42)

# Define the model


model = Sequential([
Dense(64, activation='relu', input_shape=(X_train.shape[1],)), # Input layer
Dropout(0.5), # Dropout layer for regularization
Dense(32, activation='relu'), # Hidden layer
Dense(3, activation='softmax') # Output layer for 3 classes
])

# Compile the model


model.compile(optimizer=Adam(),
loss='sparse_categorical_crossentropy', # For integer labels
metrics=['accuracy'])

# Summary of the model


model.summary()

# Train the model


history = model.fit(X_train, y_train, epochs=10, batch_size=32,
validation_split=0.2)

# Evaluate the model


loss, accuracy = model.evaluate(X_test, y_test)
print(f"Test Loss: {loss:.4f}")
print(f"Test Accuracy: {accuracy:.4f}")

# Make predictions
predictions = model.predict(X_test)
predicted_classes = np.argmax(predictions, axis=1)

print("Predicted classes:", predicted_classes)

OUTPUT :

Model Summary:
_________________________________________________________________
Layer (type) Output Shape Param #
==============================================================
dense (Dense) (None, 64) 320
dropout (Dropout) (None, 64) 0
dense_1 (Dense) (None, 32) 2080
dense_2 (Dense) (None, 3) 99
==============================================================
Total params: 2,499
Trainable params: 2,499
Non-trainable params: 0
_________________________________________________________________

Epoch 1/10
2/2 [==============================] - 1s 38ms/step - loss: 1.4237 -
accuracy: 0.3333 - val_loss: 1.3476 - val_accuracy: 0.4000
...
Epoch 10/10
2/2 [==============================] - 0s 25ms/step - loss: 0.5843 -
accuracy: 0.8000 - val_loss: 0.6347 - val_accuracy: 0.7000

Test Loss: 0.6347


Test Accuracy: 0.7000

Predicted classes: [0 1 2 1 0 0 1 2 0 2 ...]

You might also like