AI&ML Document
AI&ML Document
SEMESTER V
ACADEMIC YEAR 2024 - 2025
Prepared By
SEMESTER V
ACADEMIC YEAR 2024 – 2025
(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:
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:
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:
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:
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:
ALGORITHM:
PROGRAM:
if m in closed_list:
closed_list.remove(m)
open_list.add(m)
open_list.remove(n)
closed_list.add(n)
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:
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
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
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
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:
ALGORITHM:
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 = {}
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 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}')
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:
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:
RESULT:
Thus the given bayesian network program was executed successfully and Output
was verified.
EX NO: 5
DATE: BUILD REGRESSION MODEL
AIM:
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 :
Thus the given regression model program was executed successfully and Output
was verified.
EX NO: 6A
DATE : BUILD DECISION TREES
AIM:
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 :
# 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)
# Making predictions
y_pred = clf.predict(X_test)
# 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
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
# 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())
OUTPUT:
Accuracy: 0.96
Classification Report:
precision recall f1-score support
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:
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 :
OUTPUT :
Accuracy: 1.00
Classification Report:
precision recall f1-score support
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:
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
# Convert to DataFrame
df = pd.DataFrame(data)
y_pred_voting = voting_model.predict(X_test)
accuracy_voting = accuracy_score(y_test, y_pred_voting)
results['Voting'] = accuracy_voting
# Plot results
plt.figure(figsize=(10, 6))
sns.barplot(data=plot_df, x='Model', y='Accuracy',
hue='Model', palette='viridis')
plt.xlabel('Model')
plt.ylabel('Accuracy (%)')
plt.title('Model Accuracy Comparison')
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]
RandomForest: 40.00%
DecisionTree: 45.00%
SVC: 50.00%
Voting: 45.00%
Thus the given ensembling technique was executed successfully and the output
was verified.
EX NO: 9
DATE: IMPLEMENT THE CLUSTERING ALGORITHM
AIM:
ALGORITHM:
PROGRAM :
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
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:
ALGORITHM:
logL= −∑((x−mu[i]^2)/(2×(sigma[i]^2))
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:
AIM:
ALGORITHM:
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
# Plotting
plt.figure(figsize=(10, 6))
plt.contourf(xx, yy, Z, alpha=0.5, cmap='coolwarm') # Decision boundary
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k',
cmap='coolwarm') # Data points
plt.title('Decision Boundary and Data Points')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.show()
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
AIM:
ALGORITHM:
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
# Make predictions
predictions = model.predict(X_test)
predicted_classes = np.argmax(predictions, axis=1)
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