Dhi Ai Lab
Dhi Ai Lab
lOMoARcPSD|18803293
Program:
# Breadth-First Search (BFS) algorithm
graph = { '5' : ['3','7'], '3' : ['2', '4'], '7' : ['8'], '2' : [], '4' : ['8'], '8' : []
}
# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, '5')
lOMoARcPSD|18803293
EXP.NO: 1A
IMPLEMENTING BREADTH-FIRST SEARCH (BFS)
DATE:
AIM:
The aim of implementing Breadth-First Search (BFS) algorithms is to
traverse a graph or a tree data structure in a systematic way, visiting all
nodes and edges in the structure in a particular order, without revisiting
any node twice.
ALGORITHM:
O
u
t
p
u
lOMoARcPSD|18803293
t
:
Following is the
Breadth-First Search
5
R
e
s
u
l
t
:
1
Dhirajlal Gandhi College of Technology Department of Information
Technology
Thus the uninformed search algorithms Breadth-First Search (BFS) have been
executed successfully and the output got verified.
Viva Questions:
1. Breadth First Search is equivalent to which of the traversal in the Binary Trees?
a) Pre-order Traversal b) Post-order
Traversal c) Level-order Traversal d) In-order
Traversal
2. The Data structure used in standard implementation of Breadth First Search is?
a) Stack b) Queue c) Linked List d) Tree
6. Regarding implementation of Breadth First Search using queues, what is the maximum
distance between two nodes present in the queue? (considering each edge length 1)
a) Can be anything b) 0 c) At most 1 d) Insufficient Information
8. A person wants to visit some places. He starts from a vertex and then wants to visit
every place connected to this vertex and so on. What algorithm he should use?
a) Depth First Search b) Breadth First
Search c) Trim’s algorithm d)Kruskal’s
algorithm
9. The Breadth First Search algorithm has been implemented using the queue data
structure. One possible order of visiting the nodes of the following graph is
10. Time Complexity of Breadth First Search is? (V – number of vertices, E – number of
edges)
a) O(V + E) b) O(V) c) O(E) d) O(V*E)
Practice Exercise:
1. Develop a code by implementing the Uninformed search algorithm- BFS
2. Develop a code by implementing the 8 puzzles using the BFS.
3. Implementation of Breadth First Search for Tic-Tac-Toe Problem
4. Write a program to implement Towers of Hanoi problem.
5. A---B
|\ |
| \ |
| \|
C---D
Write a Python program to perform a Breadth-First Search on the above
graph starting from vertex ‘A’.
6. A
/ \
B C
/ \ \
D E F
Write a Python program to perform a Breadth-First Search on this graph starting
from vertex ‘A’.
7. Write a python program to implement BFS the graph is implemented as an
adjacency list.
8. Write a program to implement the Uninformed strategy – Breadth-First
Search considering the following graph
graph = {'Q': ['P', 'C'], 'R':['D'], 'C':[], 'P':[]}
9. Write a program to implement the Uninformed strategy – Uniform Search
3
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Aim: Credits
1. B) IMPLEMENTING DEPTH-FIRST
SEARCH (DFS)
The aim of implementing Depth-First Search (DFS) algorithms is to
traverse a graph or a tree data structure in a systematic way, visiting all
nodes and edges in the structure in a particular order, without revisiting
any node twice.
P
r
o
g
r
a
m
:
# Depth-First Search
(DFS) algorithm
graph = { '5' : ['3','7'], '3' : ['2', '4'], '7' : ['8'], '2' : [],
'4' : ['8'], '8' : [] }
D
r
i
v
e
r
C
o
d
e
print("Following is the
Depth-First Search")
dfs(vis
ited,
graph,
'5')
O
u
t
p
u
t
:
Following is the
Depth-First Search
5
3
2
4
8
7
R
e
s
u
l
t
:
Thus the uninformed search algorithms Depth-First Search
(DFS) have been executed successfully and the output got verified.
4
Dhirajlal Gandhi College of Technology Department of Information
Technology
Viva Questions:
1. Depth First Search is equivalent to which of the traversal in the Binary
Trees?
a) Pre-order Traversal b) Post-order
Traversal c) Level-order Traversal d) In-order
Traversal
5. A person wants to visit some places. He starts from a vertex and then wants to visit
every vertex till it finishes from one vertex, backtracks and then explore other vertex from
same vertex. What algorithm he should use?
a) Depth First Search b) Breadth First
Search c) Trim’s algorithm d) Kruskal’s
Algorithm
10. Is following statement true/false If a DFS of a directed graph contains a back edge, any
other DFS of the same graph will also contain at least one back edge.
a) True b) False
5
Dhirajlal Gandhi College of Technology Department of Information
Technology
Practice Exercise:
1. Develop a code by implementing the Uninformed search algorithm- DFS
2. Develop a code to implement the DFS of a large dataset using the maximum
recursion by using DFS
3. Implementation of Depth First Search for Water Jug Problem
4. Write a Python program to implement Depth-First Search using a tree.
5. Write a Python program to perform a DFS traversal starting in a graph and show
the order of visited vertices.
6. Write a Python program to find the articulation points of the graph using Depth-First
Search.
7. Given the following adjacency matrix:
0110
1001
1001
0110
Perform a Depth-First Search on this graph starting from vertex ‘0’.
8. Write a program to implement the Uninformed strategy – Depth First
Search considering the following graph
graph = {'A': ['B', 'C'], 'B':['D'], 'C':[], 'D':[]}
9. Write a Program to Implement Monkey Banana Problem using Python.
10.
6
Dhirajlal Gandhi College of Technology Department of Information
Technology
7
Dhirajlal Gandhi College of Technology Department of Information
Technology
2.
8
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Credits
2. A) IMPLEMENTING INFORMED SEARCH
ALGORITHMS - A*
Aim:
The aim of a C program for implementing informed search algorithms like
A* and memory- bounded A* is to efficiently find the shortest path
between two points in a graph or
network. The A* algorithm is a heuristic-based search algorithm that
finds the shortest
path between two points by evaluating the cost function of each possible
path.
Program:
from queue
import
PriorityQueue v
=14
graph =[[] for i in range(v)]
9
Dhirajlal Gandhi College of Technology Department of Information
Technology
print()
source =0
target =9
best_first_search(source, target, v)
OUTPUT:
A* :
0
1
3
2
8
9
Viva Questions:
1. Which data structure is typically used to implement the open and closed lists in the A*
search algorithm?
A. Queue B. Stack C. Set D. Priority Queue
2. Which of the following best describes the heuristic function in the A* search
algorithm? A. A function that assigns weights to the edges of the graph.
B. A function that estimates the cost from the current node to the goal node.
C. A function that selects the next node to expand based on the lowest path cost.
10
Dhirajlal Gandhi College of Technology Department of Information
Technology
6. Which of the following conditions can lead to an optimal path in the A* search
algorithm? A. The heuristic function is admissible, but not consistent.
B. The heuristic function is both admissible and
consistent. C. The heuristic function is consistent, but not
admissible.
D. The heuristic function is neither admissible nor consistent.
8. Which of the following scenarios can cause the A* search algorithm to return
a suboptimal path?
A. The heuristic function is admissible but not
consistent. B. The heuristic function is consistent but not
admissible.
C. The search space contains cycles.
D. The heuristic function is neither admissible nor consistent.
9. Which of the following techniques can be used to improve the efficiency of the A*
search algorithm?
A. Increasing the size of the open list.
B. Using an effective heuristic function.
11
Dhirajlal Gandhi College of Technology Department of Information
Technology
Practice Exercise:
1. Develop a code by implementing the Informed search algorithm- A*
2. Develop a code using the repository of UCI Dataset and perform the Informed
search algorithm- A*
3. Write the program to find the shortest path from `start` to `goal` in a `graph`
by means of A* algorithm.
4. Write a program to implement the A* algorithm to find the shortest path
from source to all vertices.
5. Write a program to implement Hill Climbing algorithm.
12
Dhirajlal Gandhi College of Technology Department of Information
Technology
13
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Credits
A
i
m
:
The aim of a C program for implementing informed search algorithms
like memory- bounded A* is to efficiently find the shortest path
between two points in a graph or network. The memory bounded A*
algorithm is a variant of the A* algorithm that uses a limited amount of
memory and is suitable for large search spaces.
Algorithm for
memory-bounded A*
1. Initialize the starting node with a cost of zero and add it to an open list
and a closed list.
2. While the open
list is not empty:
3. Find the node with the lowest cost in the open
list and remove it.
4. If this node is the goal node, return the
path to this node.
5. Generate all successor nodes of
the current node.
6. For each successor node, calculate its cost and add it to the open list if
it is not in the closed list. e. If the open list is too large, remove the node
with the highest cost from the
open list and add it to the closed list.
7. Add the current node
to the closed list.
8. If the open list is empty and the goal node has not been found, then
there is no path from
9. the start node
to the goal node.
P
r
o
g
r
a
m
:
#Memo
ry
Bounde
dA*
c
l
a
s
s
G
r
a
p
h
:
def init (self, graph, heuristicNodeList, startNode): #instantiate
graph object with graph topology, heuristic values, start node
self.graph
= graph
self.H=he
uristicNo
deList
self.s
tart=
start
Nod
e
self.
pare
nt={
}
self.status={}
self.solutionGraph={}
14
Dhirajlal Gandhi College of Technology Department of Information
Technology
return self.status.get(v,0)
def setStatus(self,v, val): # set the status of a given
node self.status[v]=val
def printSolution(self):
print("FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE
START NODE:",self.start)
print("------------------------------------------------------------")
print(self.solutionGraph)
print("------------------------------------------------------------")
15
Dhirajlal Gandhi College of Technology Department of Information
Technology
16
Dhirajlal Gandhi College of Technology Department of Information
Technology
G2 = Graph(graph2, h2, 'A') # Instantiate Graph object with graph, heuristic values
and start Node
G2.applyAOStar() # Run the AO* algorithm
G2.printSolution() # Print the solution graph as output of the AO* algorithm search
Output:
Graph - 1
HEURISTIC VALUES : {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J':
1} SOLUTION GRAPH : {}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
10 ['B', 'C']
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J':
1} SOLUTION GRAPH : {}
PROCESSING NODE : B
-----------------------------------------------------------------------------------------
6 ['G']
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J':
1} SOLUTION GRAPH : {}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
10 ['B', 'C']
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J':
1} SOLUTION GRAPH : {}
PROCESSING NODE : G
-----------------------------------------------------------------------------------------
8 ['I']
HEURISTIC VALUES : {'A': 10, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 7, 'J':
1} SOLUTION GRAPH : {}
PROCESSING NODE : B
-----------------------------------------------------------------------------------------
8 ['H']
HEURISTIC VALUES : {'A': 10, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 7, 'J':
1} SOLUTION GRAPH : {}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
12 ['B', 'C']
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 7, 'J':
1} SOLUTION GRAPH : {}
PROCESSING NODE : I
-----------------------------------------------------------------------------------------
0 []
17
Dhirajlal Gandhi College of Technology Department of Information
Technology
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 8, 'H': 7, 'I': 0, 'J':
1} SOLUTION GRAPH : {'I': []}
PROCESSING NODE : G
-----------------------------------------------------------------------------------------
1 ['I']
HEURISTIC VALUES : {'A': 12, 'B': 8, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J':
1} SOLUTION GRAPH : {'I': [], 'G': ['I']}
PROCESSING NODE : B
-----------------------------------------------------------------------------------------
2 ['G']
HEURISTIC VALUES : {'A': 12, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J':
1} SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
6 ['B', 'C']
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J':
1} SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : C
-----------------------------------------------------------------------------------------
2 ['J']
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J':
1} SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
6 ['B', 'C']
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J':
1} SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G']}
PROCESSING NODE : J
-----------------------------------------------------------------------------------------
0 []
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J':
0} SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G'], 'J': []}
PROCESSING NODE : C
-----------------------------------------------------------------------------------------
1 ['J']
HEURISTIC VALUES : {'A': 6, 'B': 2, 'C': 1, 'D': 12, 'E': 2, 'F': 1, 'G': 1, 'H': 7, 'I': 0, 'J':
0} SOLUTION GRAPH : {'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J']}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
5 ['B', 'C']
FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE: A
------------------------------------------------------------
{'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J'], 'A': ['B', 'C']}
------------------------------------------------------------
Graph - 2
18
Dhirajlal Gandhi College of Technology Department of Information
Technology
HEURISTIC VALUES : {'A': 1, 'B': 6, 'C': 12, 'D': 10, 'E': 4, 'F': 4, 'G': 5, 'H':
7} SOLUTION GRAPH : {}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
11 ['D']
HEURISTIC VALUES : {'A': 11, 'B': 6, 'C': 12, 'D': 10, 'E': 4, 'F': 4, 'G': 5, 'H':
7} SOLUTION GRAPH : {}
PROCESSING NODE : D
-----------------------------------------------------------------------------------------
10 ['E', 'F']
HEURISTIC VALUES : {'A': 11, 'B': 6, 'C': 12, 'D': 10, 'E': 4, 'F': 4, 'G': 5, 'H':
7} SOLUTION GRAPH : {}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
11 ['D']
HEURISTIC VALUES : {'A': 11, 'B': 6, 'C': 12, 'D': 10, 'E': 4, 'F': 4, 'G': 5, 'H':
7} SOLUTION GRAPH : {}
PROCESSING NODE : E
-----------------------------------------------------------------------------------------
0 []
HEURISTIC VALUES : {'A': 11, 'B': 6, 'C': 12, 'D': 10, 'E': 0, 'F': 4, 'G': 5, 'H':
7} SOLUTION GRAPH : {'E': []}
PROCESSING NODE : D
-----------------------------------------------------------------------------------------
6 ['E', 'F']
HEURISTIC VALUES : {'A': 11, 'B': 6, 'C': 12, 'D': 6, 'E': 0, 'F': 4, 'G': 5, 'H':
7} SOLUTION GRAPH : {'E': []}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
7 ['D']
HEURISTIC VALUES : {'A': 7, 'B': 6, 'C': 12, 'D': 6, 'E': 0, 'F': 4, 'G': 5, 'H':
7} SOLUTION GRAPH : {'E': []}
PROCESSING NODE : F
-----------------------------------------------------------------------------------------
0 []
HEURISTIC VALUES : {'A': 7, 'B': 6, 'C': 12, 'D': 6, 'E': 0, 'F': 0, 'G': 5, 'H':
7} SOLUTION GRAPH : {'E': [], 'F': []}
PROCESSING NODE : D
-----------------------------------------------------------------------------------------
2 ['E', 'F']
HEURISTIC VALUES : {'A': 7, 'B': 6, 'C': 12, 'D': 2, 'E': 0, 'F': 0, 'G': 5, 'H':
7} SOLUTION GRAPH : {'E': [], 'F': [], 'D': ['E', 'F']}
PROCESSING NODE : A
-----------------------------------------------------------------------------------------
3 ['D']
19
Dhirajlal Gandhi College of Technology Department of Information
Technology
FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE: A
------------------------------------------------------------
{'E': [], 'F': [], 'D': ['E', 'F'], 'A': ['D']}
------------------------------------------------------------
Viva Questions:
1. What is the other name of informed search strategy?
a) Simple search b) Heuristic search c) Online search d) None of the mentioned
3. Which search uses the problem specific knowledge beyond the definition of the problem?
a) Informed search b) Depth-first search
c) Breadth-first search d) Uninformed
search
4. Which function will select the lowest expansion node at first for evaluation?
a) Greedy best-first search b) Best-first search
c) Depth-first search d) None of the mentioned
10. Which search method will expand the node that is closest to the goal?
a) Best-first search b) Greedy best-first
search c) A* search d) None of the
mentioned
Practice Exercise:
20
Dhirajlal Gandhi College of Technology Department of Information
Technology
21
Dhirajlal Gandhi College of Technology Department of Information
Technology
22
Dhirajlal Gandhi College of Technology Department of Information
Technology
23
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Credits
3. IMPLEMENT
NAÏVE BAYES
Aim:
The aim of the Naïve Bayes algorithm is to classify a given set of data points
into different classes based on the probability of each data point
belonging to a particular class. This
algorithm is based on the Bayes theorem, which states that the
probability of an event
occurring given the prior knowledge of another event can be calculated
using conditional probability.
Algorithm:
1. Collect the dataset: The first step in using Naïve Bayes is to
collect a dataset that contains a set of data points and their
corresponding classes.
4. Prepare the data: The next step is to preprocess the data and prepare it
for the Naïve
Bayes algorithm. This involves removing any unnecessary features or
attributes and normalizing the data.
2. Compute the prior probabilities: The prior probabilities of each class can
be computed
by calculating the number of data points belonging to each class and
dividing it by the total number of data points.
3. Compute the likelihoods: The likelihoods of each feature for each
class can be computed by calculating the conditional probability of the
feature given the class. This
involves counting the number of data points in each class that have the
feature and dividing it by the total number of data points in that class.
5. Compute the posterior probabilities: The posterior probabilities of each
class can be computed by multiplying the prior probability of the
class with the product of the
likelihoods of each feature for that class.
6. Make predictions: Once the posterior probabilities have been
computed for each class, the Naïve Bayes algorithm can be used to
make predictions by selecting the class with
the highest probability.
7. Evaluate the model: The final step is to evaluate the performance of the
Naïve Bayes model. This can be done by computing various
performance metrics such as accuracy, precision, recall, and F1 score.
Data
Set: 5.1 3.5 1.4 0.2 Iris-setosa
4.9 3 1.4 0.2 Iris-setosa
4.7 3.2 1.3 0.2 Iris-setosa
4.6 5 3.6 1.4 0.2 Iris-setosa
3.1 5.4 3.9 1.7 0.4 Iris-setosa
1.5 4.6 3.4 1.4 0.3 Iris-setosa
0.2 5 3.4 1.5 0.2 Iris-setosa
Iris- 4.4 2.9 1.4 0.2 Iris-setosa
setosa
24
Dhirajlal Gandhi College of Technology Department of Information
Technology
Program:
# load the iris dataset
from sklearn.datasets import load_iris
iris = load_iris()
# comparing actual response values (y_test) with predicted response values (y_pred)
from sklearn import metrics
print("Gaussian Naive Bayes model accuracy(in %):",
metrics.accuracy_score(y_test, y_pred)*100)
Output:
Gaussian Naive Bayes model accuracy(in %): 95.0
Result:
Thus the program for Navy Bayes is verified successfully and output is verified.
Viva Questions:
1. Which of the following statements best describes Naive Bayes Algorithm?
a) It is a supervised learning algorithm used for classification.
b) It is an unsupervised learning algorithm used for
clustering.
c) It is a reinforcement learning algorithm used for decision making.
d) It is a dimensionality reduction algorithm used for feature extraction.
2. What assumption does Naive Bayes Algorithm make regarding the independence
of features?
a) Conditional independence b) Mutual independence c) Dependence d) None of the above
25
Dhirajlal Gandhi College of Technology Department of Information
Technology
3. Which probability distribution is commonly used for modeling the likelihood in Naive
Bayes Algorithm?
a) Normal distribution b) Uniform distribution
c) Poisson distribution d) Bernoulli distribution
8. What problem can occur in Naive Bayes Algorithm if a particular feature has
zero probability in the training dataset for a certain class?
a) Overfitting b) Underfitting c) Zero-frequency problem d) Class imbalance problem
9. Which evaluation metric is commonly used to assess the performance of Naive Bayes
Algorithm for classification tasks?
a) Mean Absolute Error (MAE) b) Root Mean Squared Error
(RMSE)
c) F1 score d) R-squared (R^2) score
Practice Exercise:
1. Develop a code by implementing the Analyzation of data set using naïve Bayes
models
26
Dhirajlal Gandhi College of Technology Department of Information
Technology
2. Develop a code to implement the Gaussian naïve Bayes models for the spam filtering
process.
3. Assuming a set of documents that need to be classified, use the naïve Bayesian
Classifier model to perform this task. Built-in Java classes/API can be used to write the
program.
Calculate the accuracy, precision, and recall for your data set.
4. Write a program to implement the naïve Bayesian classifier for a sample training data
set stored as a .CSV file. Compute the accuracy of the classifier, considering few test
data
sets.
5. Write a python program to implement a Naive Bayes classifier using scikit-learn library
6. Write a python program to implement Gaussian naïve bayes models
7. Write a python program to implement Bernoulli naïve bayes models
8. Write a python program to implement Multinomial naïve bayes models
9. Write a program to implement Naive Bayes models for the following problem Assume
we have to find the probability of the randomly picked card to be king given that it is a
face card.
27
Dhirajlal Gandhi College of Technology Department of Information
Technology
28
Dhirajlal Gandhi College of Technology Department of Information
Technology
29
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Credits
4. IMPLEMENT BAYESIAN
NETWORKS
Aim:
The aim of implementing Bayesian Networks is to model the
probabilistic relationships between a set of variables. A Bayesian Network
is a graphical model that represents the
conditional dependencies between different variables in a probabilistic
manner. It is a
powerful tool for reasoning under uncertainty and can be used for a
wide range of applications, including decision making, risk analysis, and
prediction.
Algorithm:
1. Define the variables: The first step in implementing a Bayesian Network
is to define the variables that will be used in the model. Each variable
should be clearly defined and its possible states should be enumerated.
Database: 0 1 2 3 4 Total
30
Dhirajlal Gandhi College of Technology Department of Information
Technology
Attribute Information:
Data Set:
rest
trestbp thalac old tha heart
age sex exang s chol fbs ecg h peak slope ca l disease
cp
63 1 0 1 145 233 1 2 150 2.3 3 0 6 0
67 1 1 4 160 286 0 2 108 1.5 2 3 3 2
67 1 1 4 120 229 0 2 129 2.6 2 2 7 1
37 1 0 3 130 250 0 0 187 3.5 3 0 3 0
41 0 0 2 130 204 0 2 172 1.4 1 0 3 0
56 1 0 2 120 236 0 0 178 0.8 1 0 3 0
62 0 0 4 140 268 0 2 160 3.6 3 2 3 3
57 0 1 4 120 354 0 0 163 0.6 1 0 3 0
31
Dhirajlal Gandhi College of Technology Department of Information
Technology
Program:
#install
!pip install pgmpy
# Load 'heart.csv'
import numpy as np
import pandas as pd
import csv
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.models import BayesianNetwork
from pgmpy.inference import VariableElimination
heartDisease = pd.read_csv('heart.csv')
heartDisease = heartDisease.replace('?',np.nan)
model= BayesianNetwork([('age','heartdisease'),('sex','heartdisease'),
('exang','heartdisease'),('cp','heartdisease'),('heartdisease','restecg'),
('heartdisease','chol')])
print('\nLearning CPD using Maximum likelihood estimators')
model.fit(heartDisease,estimator=MaximumLikelihoodEstimator)
32
Dhirajlal Gandhi College of Technology Department of Information
Technology
print(q2)
Output:
oldpeak slope ca thal heartdisease
0 2.3 3 0 6 0
1 1.5 2 3 3 2
2 2.6 2 2 7 1
3 3.5 3 0 3 0
4 1.4 1 0 3 0
33
Dhirajlal Gandhi College of Technology Department of Information
Technology
Result:
Thus the program to implement a Bayesian Networks in the given heart disease
dataset have been executed successfully and the output got verified.
Viva Questions:
5. How the entries in the full joint probability distribution can be calculated?
a) Using variables b) Using information
c) Both Using variables & information d) None of the mentioned
34
Dhirajlal Gandhi College of Technology Department of Information
Technology
10. What is the consequence between a node and its predecessors while creating
bayesian network?
a) Functionally dependent b) Dependant
c) Conditionally independent d) Both Conditionally dependant & Dependant
Practice Exercise:
1. Write a program to implement Bayesian Network that will model the performance of
a student on an exam.
2. Write a program to construct a Bayesian network considering medical data. Use this
model to demonstrate the diagnosis of heart patients using standard Heart Disease
Data
Set. You can use Python ML library classes/API
3. Write a python program to create a simple Bayesian network using pgmpy.
4. Write a python program to implement the EM algorithm for Bayesian networks in
Python
5. Write a python program using the K2 algorithm for learning the structure of a
Bayesian network
6. Develop a code to implement the Bayesian Networks for performing the Iteration
process and Analyze the random networks.
7. Write a EM code for understand the heart diseases and implement using the Bayesian
Networks.
8. Develop a code by implementing the probability relationship check between
two dataset using Bayesian Networks
35
Dhirajlal Gandhi College of Technology Department of Information
Technology
36
Dhirajlal Gandhi College of Technology Department of Information
Technology
37
Dhirajlal Gandhi College of Technology Department of Information
Technology
38
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Aim: Credits
5. BUILD REGRESSION MODELS
To build regression models such as locally weighted linear regression
and plot the necessary graphs.
Algorithm:
1. Read the Given data Sample to X and the curve (linear or non-linear) to Y
2. Set the value for Smoothening parameter or Free parameter say τ
3. Set the bias /Point of interest set x0 which is a subset of X
4. Determine the weight matrix using :
6. Prediction = x0*β.
Program:
from
math
import
ceil
import
numpy
as np
from
scipy
import
linalg
def lowess(x, y, f, iterations):
n = len(x)
r = int(ceil(f * n))
h = [np.sort(np.abs(x - x[i]))[r] for i in range(n)]
w = np.clip(np.abs((x[:, None] - x[None, :]) / h), 0.0, 1.0)
w=
(1 -
w **
3)
** 3
yest
=
np.z
eros
(n)
delt
a=
np.o
nes(
n)
for iteration in range(iterations):
for i in range(n):
weights = delta * w[:, i]
b = np.array([np.sum(weights * y), np.sum(weights * y * x)])
A = np.array([[np.sum(weights), np.sum(weights * x)],
[np.sum(weights * x), np.sum(weights * x * x)]])
beta = linalg.solve(A, b)
yest[i] = beta[0] + beta[1] * x[i]
residuals = y - yest
s = np.median(np.abs(residuals))
delta = np.clip(residuals / (6.0 * s), -1, 1)
delta = (1 -
delta ** 2)
** 2 return
yest
39
Dhirajlal Gandhi College of Technology Department of Information
Technology
import math
n = 100
x = np.linspace(0, 2 * math.pi, n)
y = np.sin(x) + 0.3 * np.random.randn(n)
f =0.25
iterations=3
yest = lowess(x, y, f, iterations)
import matplotlib.pyplot as plt
plt.plot(x,y,"r.")
plt.plot(x,yest,"b-")
Output:
Result:
Thus the program to implement non-parametric Locally Weighted Regression algorithm
in order to fit data points with a graph visualization have been executed successfully.
Viva Questions:
1. In which category does linear regression belong to?
a) Neither supervised nor unsupervised
learning b) Both supervised and unsupervised
learning
c) Unsupervised learning d) Supervised learning
2. The learner is trying to predict housing prices based on the size of each house. What
type of regression is this?
40
Dhirajlal Gandhi College of Technology Department of Information
Technology
3. The learner is trying to predict housing prices based on the size of each house.
The variable “size” is
a) dependent variable b) label set
variable c) independent variable d) target
variable
5. The learner is trying to predict the cost of papaya based on its size. The variable “cost”
is a) Independent variable b) target Variable c) ranked variable d) categorical variable
Practice Exercise:
1. Develop a code to understand and predict an outcome variable based on the
input
Regression models.
2. Predict an outcome of the number of customer increased by analyzing through
the
Regression models.
3. Implementation of Logistic Regression of iris using sklearn
4. Write a python program to implement Simple Linear Regression and plot the graph.
5. Write a python program to build linear regression using scikit-learn library
6. Write a python program to implement linear regression.
7. Write a python program to implement multiple regressions.
8. Write a program to plot linear regression for Weather Conditions using the
dataset Weather Conditions in World War Two plotting the maximum and
minimum temperature.
41
Dhirajlal Gandhi College of Technology Department of Information
Technology
42
Dhirajlal Gandhi College of Technology Department of Information
Technology
43
Dhirajlal Gandhi College of Technology Department of Information
Technology
44
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Aim: Credits
6. BUILD DECISION TREES AND
RANDOM FORESTS.
To implement the concept of decision trees with suitable dataset
from real world problems using CART algorithm.
Algorithm:
Steps in CART algorithm:
1. Numerical computing libraries
2. Visalization libraries
3. Exploratory data analysis
4. Split the data set into training data and test data
5. Train the decision tree model
6. Measure the performance of the decision tree model
7. Train the random forests model
8. Measure the performance of the random forest model
Program:
#Numerical computing libraries
impo
rt
pand
as as
pd
impo
rt
num
py as
np
#Visalization libraries
import
matplotlib.pypl
ot as plt import
seaborn as sns
%matplotlib inline
raw_data = pd.read_csv('kyphosis-data.csv')
raw_data.columns
#Explorato
ry data
analysis
raw_data.in
fo()
sns.pairplot(raw_data, hue = 'Kyphosis')
45
Dhirajlal Gandhi College of Technology Department of Information
Technology
model = DecisionTreeClassifier()
model.fit(x_training_data, y_training_data)
predictions = model.predict(x_test_data)
Output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 81 entries, 0 to 80
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Kyphosis 81 non-null object
1 Age 81 non-null int64
2 Number 81 non-null int64
3 Start 81 non-null int64
dtypes: int64(3), object(1)
memory usage: 2.7+ KB
precision recall f1-score support
accuracy 0.80 25
macro avg 0.75 0.81 0.76 25
weighted avg 0.85 0.80 0.81 25
[[15 4]
[ 1 5]]
precision recall f1-score support
46
Dhirajlal Gandhi College of Technology Department of Information
Technology
accuracy 0.88 25
macro avg 0.93 0.75 0.80 25
weighted avg 0.90 0.88 0.86 25
[[19 0]
[ 3 3]]
Viva Question:
1. Which of the following statements is true about the Random Forest algorithm?
a) It is an unsupervised learning
algorithm. b) It is primarily used for
regression tasks.
c) It combines multiple decision trees to make predictions.
d) It is a type of clustering algorithm.
47
Dhirajlal Gandhi College of Technology Department of Information
Technology
9. What is the term used for the value that a decision tree predicts for an instance?
a) Split b) Node c) Leaf d) Branch
Practice Exercise:
1. Develop a code to Build a decision trees to predict the expected output from the
desired input.
2. Develop a code to build random forests for the dataset by understand the difference
between Random and Decision Tree.
3. Develop a code to understand the risk to prevent the heart attack using the Decision
Trees.
48
Dhirajlal Gandhi College of Technology Department of Information
Technology
4. Develop a code to build the comparison code to predict the Majority winner after the
election using the random forest Classifier.
5. Write a program to demonstrate the working of the decision tree based ID3 algorithm.
Use an appropriate data set for building the decision tree and apply this knowledge to
classify a new sample
6. Write a python program to build decision tree regression using scikit-learn library
7. Write a python program to build decision trees and random forests
8. Write a program to implementations of Random Forest (an ensemble of decision trees
using Bagging) and Gradient Boosting in Python using scikit-learn library
9. Write a program to implement the random forest classifier for the following dataset
https://www.kaggle.com/datasets/elikplim/car-evaluation-data-set
49
Dhirajlal Gandhi College of Technology Department of Information
Technology
50
Dhirajlal Gandhi College of Technology Department of Information
Technology
51
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Aim: Credits
7. BUILD
SVM
MODELS.
To create a machine learning model which classifies the cancer cells from a
given Breast
Cancer dataset using Support Vector Machine algorithm.
Algorithm:
1. Import all the necessary libraries.
2. Read the given csv file.
3. Gather all the words given in that dataset and identify the stop
words with a mean distribution.
4. Create an ML model using the Support Vector Classifier after splitting the
dataset into training and test set.
5. Display the accuracy.
Program:
# !pip install -r requirements.txt
#Import scikit-learn
dataset library from
sklearn import
datasets
#Load dataset
cancer = datasets.load_breast_cancer()
# print the names of the
13 features
print("Features: ",
cancer.feature_names)
52
Dhirajlal Gandhi College of Technology Department of Information
Technology
Output:
Features: ['mean radius' 'mean texture' 'mean perimeter' 'mean area'
'mean smoothness' 'mean compactness' 'mean concavity'
'mean concave points' 'mean symmetry' 'mean fractal dimension'
'radius error' 'texture error' 'perimeter error' 'area error'
'smoothness error' 'compactness error' 'concavity error'
'concave points error' 'symmetry error' 'fractal dimension error'
'worst radius' 'worst texture' 'worst perimeter' 'worst area'
'worst smoothness' 'worst compactness' 'worst concavity'
'worst concave points' 'worst symmetry' 'worst fractal dimension']
Labels: ['malignant' 'benign']
[[1.799e+01 1.038e+01 1.228e+02 1.001e+03 1.184e-01 2.776e-01 3.001e-01
1.471e-01 2.419e-01 7.871e-02 1.095e+00 9.053e-01 8.589e+00 1.534e+02
6.399e-03 4.904e-02 5.373e-02 1.587e-02 3.003e-02 6.193e-03 2.538e+01
1.733e+01 1.846e+02 2.019e+03 1.622e-01 6.656e-01 7.119e-01 2.654e-01
4.601e-01 1.189e-01]
[2.057e+01 1.777e+01 1.329e+02 1.326e+03 8.474e-02 7.864e-02 8.690e-02
7.017e-02 1.812e-01 5.667e-02 5.435e-01 7.339e-01 3.398e+00 7.408e+01
5.225e-03 1.308e-02 1.860e-02 1.340e-02 1.389e-02 3.532e-03 2.499e+01
2.341e+01 1.588e+02 1.956e+03 1.238e-01 1.866e-01 2.416e-01 1.860e-01
2.750e-01 8.902e-02]
[1.969e+01 2.125e+01 1.300e+02 1.203e+03 1.096e-01 1.599e-01 1.974e-01
1.279e-01 2.069e-01 5.999e-02 7.456e-01 7.869e-01 4.585e+00 9.403e+01
6.150e-03 4.006e-02 3.832e-02 2.058e-02 2.250e-02 4.571e-03 2.357e+01
2.553e+01 1.525e+02 1.709e+03 1.444e-01 4.245e-01 4.504e-01 2.430e-01
3.613e-01 8.758e-02]
[1.142e+01 2.038e+01 7.758e+01 3.861e+02 1.425e-01 2.839e-01 2.414e-01
1.052e-01 2.597e-01 9.744e-02 4.956e-01 1.156e+00 3.445e+00 2.723e+01
9.110e-03 7.458e-02 5.661e-02 1.867e-02 5.963e-02 9.208e-03 1.491e+01
2.650e+01 9.887e+01 5.677e+02 2.098e-01 8.663e-01 6.869e-01 2.575e-01
6.638e-01 1.730e-01]
[2.029e+01 1.434e+01 1.351e+02 1.297e+03 1.003e-01 1.328e-01 1.980e-01
53
Dhirajlal Gandhi College of Technology Department of Information
Technology
Result:
Thus the program to create a machine learning model which classifies the cancer cells
from a given Breast Cancer dataset using Support Vector Machine algorithm have
been successfully executed.
Viva Questions:
1. Which of the following is a characteristic of Support Vector Machines (SVM)
algorithm?
a) It is primarily used for regression tasks. b) It is a supervised learning
algorithm.
c) It is a clustering algorithm. d) It is an unsupervised learning
algorithm.
2. Which of the following is the main objective of Support Vector Machines (SVM)
algorithm?
a) Minimize the misclassification rate b) Maximize the margin between
classes c) Reduce the dimensionality of the input features d) Optimize the bias-variance
tradeoff
54
Dhirajlal Gandhi College of Technology Department of Information
Technology
9. How does the complexity of training an SVM model scale with the number of training
examples?
a) Quadratically b) Linearly c) Exponentially d) Logarithmically
10. Which of the following algorithms can be seen as a variant of Support Vector Machines?
a) Decision Trees b) Naive Bayes c) Logistic Regression d) Perceptron
Practice Questions:
1. Develop a code to Build support vector machine models for classification Task
2. Implement the code to access about the recent purchase made by the customer by
using the Support Vector Machine models for classification Task.
3. Implementation of SVM classification.
4. Write a python program to build SVM (Support Vector Machine) models using scikit-
learn
5. Write a program to building SVM models for both classification and regression using
Python and scikit-learn library.
6. Write a program to implement the SVM using the following
dataset https://www.kaggle.com/mltuts/social-network-ads
55
Dhirajlal Gandhi College of Technology Department of Information
Technology
56
Dhirajlal Gandhi College of Technology Department of Information
Technology
57
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Aim: Credits
8. IMPLEMENT ENSEMBLING TECHNIQUES
To implement the ensembling technique of Blending with the
given Alcohol QCM Dataset.
Algorithm:
1. Split the training dataset into train, test and validation dataset.
2. Fit all the base models using train dataset.
3. Make predictions on validation and test dataset.
4. These predictions are used as features to build a second level model
5. This model is used to make predictions on test and meta-features.
Program:
import matplotlib.pyplot as plt
from sklearn.datasets
import load_diabetes from
sklearn.ensemble import (
GradientBoo
stingRegress
or,
RandomFore
stRegressor,
VotingRegressor,
)
from sklearn.linear_model import
LinearRegression
X, y =
load_diabetes(return_
X_y=True)
#
Tr
ai
n
cla
ssi
fie
rs
reg1 =
GradientBoostingRegressor(random_
state=1)
reg2 =
RandomForestRegressor(random_
state=1)
reg3 =
LinearRegr
ession()
r
e
g
1
.
f
i
t
(
X
,
y
)
r
e
g
2
.
f
i
t
(
X
,
y
)
r
e
g
3
.
f
i
t
(
X
,
y
)
y
)
x
t
X
[
:
2
0
]
pred1 =
reg1.pre
dict(xt)
pred2 =
reg2.pre
dict(xt)
pred3 =
reg3.pre
dict(xt)
pred4 =
ereg.pre
dict(xt)
plt.figure
()
58
Dhirajlal Gandhi College of Technology Department of Information
Technology
plt.show()
Output:
Viva Questions:
1. Which of the following algorithm is not an example of an ensemble
method? A. Extra Tree Regressor B. Random Forest
C. Gradient Boosting D. Decision Tree
59
Dhirajlal Gandhi College of Technology Department of Information
Technology
4) Which of the following can be true for selecting base learners for an ensemble?
1. Different learners can come from same algorithm with different hyper parameters
2. Different learners can come from different algorithms
3. Different learners can come from different training spaces
A. 1 B. 2 C. 1 and 3 D. 1, 2 and 3
Q5. True or False: Ensemble learning can only be applied to supervised learning
methods. A. True B. False
6. True or False: Ensembles will yield bad results when there is significant diversity
among the models.
Note: All individual models have meaningful and good
predictions. A. True B. False
7. Which of the following is / are true about weak learners used in ensemble model?
1. They have low variance and they don’t usually overfit
2. They have high bias, so they can not solve hard learning problems
3. They have high variance and they don’t usually overfit
A. 1 and 2 B. 1 and 3 C. 2 and 3 D. None of these
8.True or False: Ensemble of classifiers may or may not be more accurate than any of
its individual model.
A. True B. False
10. Generally, an ensemble method works better, if the individual base models have
?
Note: Suppose each individual base models have accuracy greater than
50%. A. Less correlation among predictions
B. High correlation among predictions
C. Correlation does not have any impact on ensemble output
D. None of the above
60
Dhirajlal Gandhi College of Technology Department of Information
Technology
Practice Exercise:
1. Implement an application that imposes the ensembling techniques
2. Implement an application like Open Social Paid Network that imposes
the
Ensembling techniques.
3. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data
set for clustering using k-Means algorithm. Compare the results of these two
algorithms and comment on the quality of clustering. You can add Java/Python ML
library classes/API in the program.
4. Write a python program to implement ensemble techniques, such as voting
and
bagging, using scikit-learn
5. Write a program to implementations of Random Forest (an ensemble of decision
trees using Bagging) and Gradient Boosting in Python using scikit-learn library
6. Write a program to implement Averaging method under ensembling techniques.
61
Dhirajlal Gandhi College of Technology Department of Information
Technology
62
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Credits
9. IMPLEMENT CLUSTERING ALGORITHMS
Aim:
To implment k-Nearest Neighbour algorithm to classify the Iris Dataset.
Algorithm:
1. Select the number K of the neighbors
2. Calculate the Euclidean distance of K number of neighbors
3. Take the K nearest neighbors as per the calculated Euclidean distance.
4. Among these k neighbors, count the number of the data points in each
category.
5. Assign the new data points to that category for which the number of
the neighbor is maximum.
6. Print the Accuracy and Averages.
Program:
from sklearn.model_selection import
train_test_split from
sklearn.neighbors import
KNeighborsClassifier
from sklearn.metrics import classification_report
from sklearn.metrics import
confusion_matrix import
pandas as pd
import numpy as np
from sklearn
import
datasets
iris=datasets.l
oad_iris()
iris_data=iris.data
iris_labels=iris.target
x_train, x_test, y_train, y_test=(train_test_split(iris_data, iris_labels,
test_size=0.20))
classifier=KNeighborsClassifier(
n_neighbors=6)
classifier.fit(x_train, y_train)
y_pred=classifier.predict(x_test)
print("accuracy is")
print(classification_report(y_test, y_pred))
Output:
accuracy
is precision recall f1-score support
0 1.00 1.00 1.00
13
1 0.83 1.00 0.91
5
2 1.00 0.92 0.96
12
accuracy
0.97 30 macro avg 0.94
0.97 0.96 30 weighted avg
0.97 0.97 0.97 30
63
Dhirajlal Gandhi College of Technology Department of Information
Technology
Result:
Thus the program to implement k-Nearest Neighbour Algorithm for clustering Iris
dataset have been executed successfully and output got verified.
Viva Questions:
2. Clustering is a-
a. Supervised learning b. Unsupervised learning
c. Reinforcement learning d. None
3. Which of the following clustering algorithms suffers from the problem of convergence
at local optima?
a. K- Means clustering b. Hierarchical clustering
c. Diverse clustering d. All of the above
Practice Exercise:
1. Implement an application that clustering algorithms
2. Implement an application that predict the segmentation and classify the customer
requirement using the clustering algorithms
3. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data
set for clustering using k-Means algorithm. Compare the results of these two
algorithms and comment on the quality of clustering. You can add Java/Python ML
library classes/API in the program.
4. Write a python program to implement clustering algorithms, specifically K-means
and DBSCAN, using scikit-learn
5. Write a python program to implement Agglomerative Hierarchical Clustering, using
Python and scikit-learn library
6. Write a program to implement K-means clustering algorithm
7. Write a program to implement density-based spatial clustering.
65
Dhirajlal Gandhi College of Technology Department of Information
Technology
66
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Credits
10. IMPLEMENT EM FOR BAYESIAN NETWORKS.
Aim:
To implement the EM algorithm for clustering networks using the given
dataset.
Algorithm:
1. Initialize θ randomly Repeat until convergence:
2. E-step:
a. Compute q(h) = P(H = h | E = e; θ) for each h (probabilistic
inference)
b. Create fully-observed weighted examples: (h, e) with weight
q(h)
3. M-step:
a. Maximum likelihood (count and normalize) on weighted
examples to get θ
Program:
from sklearn.cluster
import KMeans from
sklearn import
preprocessing
from sklearn.mixture import GaussianMixture
from sklearn.datasets
import load_iris
import sklearn.metrics
as sm
impo
rt
pand
as as
pd
impo
rt
num
py as
np
import
matplotlib.pypl
ot as plt
dataset=load_ir
is()
#
print(dataset)
X=pd.DataFram
e(dataset.data)
X.columns=['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y=pd.DataFrame(dataset.target)
y.columns=['Targets']
# print(X)
plt.figure(figsize=(14,7))
colormap=np.array(['red','lime','black'])
# REAL PLOT plt.subplot(1,3,1)
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y
.Targets],s=40) plt.title('Real')
# K-PLOT
plt.subplot(1,3
,2)
model=KMean
s(n_clusters=3
) model.fit(X)
predY=np.choose(model.labels_,
[0,1,2]).astype(np.int64)
plt.scatter(X.Petal_Length,X.Petal_Width,c=colorma
p[predY],s=40) plt.title('KMeans')
# GMM PLOT
67
Dhirajlal Gandhi College of Technology Department of Information
Technology
scaler=preprocessing.StandardScaler()
scaler.fit(X)
xsa=scaler.transform(X)
xs=pd.DataFrame(xsa,columns=X.columns)
gmm=GaussianMixture(n_components=3)
gmm.fit(xs)
y_cluster_gmm=gmm.predict(xs)
plt.subplot(1,3,3)
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y_cluster_gmm],s=40)
plt.title('GMM Classification')
Output:
Viva Question
1. What is needed to make probabilistic systems feasible in the world?
a. Reliability b. Crucial robustness c. Feasibility d. None of the above
68
Dhirajlal Gandhi College of Technology Department of Information
Technology
7. What is the consequence between a node and its predecessors while creating
bayesian network?
a. Functionally dependent b. Dependant
c. Conditionally independent d. Both Conditionally dependant & Dependant
10. When we query a node in a Bayesian network, the result is often referred to as
the marginal.
a. TRUE b. FALSE c. Can be true or false d. Can not say
Practice Exercise:
69
Dhirajlal Gandhi College of Technology Department of Information
Technology
70
Dhirajlal Gandhi College of Technology Department of Information
Technology
71
Dhirajlal Gandhi College of Technology Department of Information
Technology
72
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Credits
11. BUILD SIMPLE NN MODELS.
Aim:
To implement the neural network model for the given dataset.
Algorithm:
1. Image Acquisition: The first step is to acquire images of paper
documents with the help of optical scanners. This way, an original
image can be captured and stored.
2. Pre-processing: The noise level on an image should be optimized and
areas outside the
text removed. Pre-processing is especially vital for recognizing
handwritten documents that are more sensitive to noise.
3. Segmentation: The process of segmentation is aimed at grouping
characters into meaningful chunks. There can be predefined classes for
characters. So, images can be
scanned for patterns that match the classes.
4. Feature Extraction: This step means splitting the input data into a set of
features, that is, to find essential characteristics that make one or
another pattern recognizable.
5. Training an MLP neural network using the following steps:
a. Starting with the input layer, propagate data forward to the output
layer.
This step is the forward propagation.
b. Based on the output, calculate the error (the
difference between the predicted and known outcome).
The error needs to be minimized.
c. Backpropagate the error. Find its derivative with respect to
each weight in the network, and update the model.
d. Post processing: This stage is the process of refinement as an
OCR model can require some corrections. However, it
isn’t possible to achieve 100%
recognition accuracy. The identification of characters heavily
depends on the context
Program:
#download winequality-red.csv from kaggle
!pip install tensorflow --upgrade
!conda install -c conda-forge tensorflow
%tensorfl
ow_versi
on 2.x
import
numpy as
np
import pandas as pd
73
Dhirajlal Gandhi College of Technology Department of Information
Technology
import tensorflow as tf
# 75% of the data is selected
train_df = df.sample(frac=0.75, random_state=4)
input_shape
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=1,input_shape=input_shape)])
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=64, activation='relu',
input_shape=input_shape),
tf.keras.layers.Dense(units=64, activation='relu'),
tf.keras.layers.Dense(units=1)
])
74
Dhirajlal Gandhi College of Technology Department of Information
Technology
model.summary()
validation_data=(X_val, y_val),
y_val.iloc[0:3]
loss_df = pd.DataFrame(losses.history)
Output:
Epoch 1/15
5/5 [==============================] - 1s 48ms/step - loss: 0.6090 - val_loss:
0.5105
Epoch 2/15
5/5 [==============================] - 0s 11ms/step - loss: 0.4491 - val_loss:
0.3487
Epoch 3/15
5/5 [==============================] - 0s 9ms/step - loss: 0.2871 - val_loss:
0.1808
75
Dhirajlal Gandhi College of Technology Department of Information
Technology
Epoch 4/15
5/5 [==============================] - 0s 9ms/step - loss: 0.1460 - val_loss:
0.1220
Epoch 5/15
5/5 [==============================] - 0s 9ms/step - loss: 0.1404 - val_loss:
0.1537
Epoch 6/15
5/5 [==============================] - 0s 13ms/step - loss: 0.1447 - val_loss:
0.1256
Epoch 7/15
5/5 [==============================] - 0s 13ms/step - loss: 0.1209 - val_loss:
0.1123
Epoch 8/15
5/5 [==============================] - 0s 10ms/step - loss: 0.1175 - val_loss:
0.1161
Epoch 9/15
5/5 [==============================] - 0s 14ms/step - loss: 0.1189 - val_loss:
0.1113
Epoch 10/15
5/5 [==============================] - 0s 14ms/step - loss: 0.1139 - val_loss:
0.1081
Epoch 11/15
5/5 [==============================] - 0s 14ms/step - loss: 0.1126 - val_loss:
0.1080
Epoch 12/15
5/5 [==============================] - 0s 14ms/step - loss: 0.1121 - val_loss:
0.1057
Epoch 13/15
5/5 [==============================] - 0s 10ms/step - loss: 0.1100 - val_loss:
0.1045
Epoch 14/15
5/5 [==============================] - 0s 15ms/step - loss: 0.1095 - val_loss:
0.1041
Epoch 15/15
5/5 [==============================] - 0s 13ms/step - loss: 0.1088 - val_loss:
0.1036
1/1 [==============================] - 0s 65ms/step
<Axes: >
76
Dhirajlal Gandhi College of Technology Department of Information
Technology
Result:
Thus the program to implement the neural network model for the given dataset.
Viva Questions:
1. What is adaline in neural networks?
a. Adaptive line element b. Adaptive linear
element c. Automatic linear element d. None of the
mentioned
Practice Questions:
1. Develop a neural network Model to optimize the pattern.
2. Develop a neural network Model to optimize the pattern for the animation.
3. Write a python program to build a simple neural network model using the Keras library
4. Write a python program to building a simple neural network model using Python
and the Keras library
5. Write a program to build simple Neural Network models using Pytorch
78
Dhirajlal Gandhi College of Technology Department of Information
Technology
79
Dhirajlal Gandhi College of Technology Department of Information
Technology
6.
80
Dhirajlal Gandhi College of Technology Department of Information
Technology
Experiment Score
Date
of /10
Completion Additional
Aim: Credits
12. BUILD DEEP LEARNING NN MODELS.
To implement and build a Deep neural network model which predicts the
age and gender of a person using the given pre-trained models.
Algorithm:
1. Choose the Dataset.
2. Prepare the Dataset for training.
3. Create training Data.
4. Shuffle the Dataset.
5. Assigning Labels and Features.
6. Normalising X and converting labels to categorical data.
7. Split X and Y for use in CNN.
8. Define, compile and train the CNN Model.
9. Accuracy and Score of the model.
Program:
# first neural network
with keras tutorial from
numpy import loadtxt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# define
the keras
model
model =
Sequenti
al()
model.add(Dense(12, input_shape=(8,), activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
81
Dhirajlal Gandhi College of Technology Department of Information
Technology
82
Dhirajlal Gandhi College of Technology Department of Information
Technology
83
Dhirajlal Gandhi College of Technology Department of Information
Technology
Epoch 39/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6067 - accuracy:
0.6940
Epoch 40/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6138 - accuracy:
0.6888
Epoch 41/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6121 - accuracy:
0.6927
Epoch 42/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6147 - accuracy:
0.6940
Epoch 43/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6280 - accuracy:
0.6849
Epoch 44/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6046 - accuracy:
0.6992
Epoch 45/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6073 - accuracy:
0.6979
Epoch 46/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6114 - accuracy:
0.6888
Epoch 47/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6094 - accuracy:
0.6901
Epoch 48/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6020 - accuracy:
0.7109
Epoch 49/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6041 - accuracy:
0.6953
Epoch 50/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5968 - accuracy:
0.7070
Epoch 51/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6096 - accuracy:
0.6901
Epoch 52/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5909 - accuracy:
0.7018
Epoch 53/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5887 - accuracy:
0.7018
Epoch 54/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5906 - accuracy:
0.6966
Epoch 55/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5838 - accuracy:
0.7096
Epoch 56/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5867 - accuracy:
0.7148
Epoch 57/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5853 - accuracy:
0.7188
Epoch 58/150
84
Dhirajlal Gandhi College of Technology Department of Information
Technology
85
Dhirajlal Gandhi College of Technology Department of Information
Technology
Epoch 78/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5527 - accuracy:
0.7357
Epoch 79/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5490 - accuracy:
0.7344
Epoch 80/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5519 - accuracy:
0.7435
Epoch 81/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5478 - accuracy:
0.7383
Epoch 82/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5498 - accuracy:
0.7279
Epoch 83/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5433 - accuracy:
0.7344
Epoch 84/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5443 - accuracy:
0.7357
Epoch 85/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5427 - accuracy:
0.7513
Epoch 86/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5469 - accuracy:
0.7357
Epoch 87/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5411 - accuracy:
0.7422
Epoch 88/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5384 - accuracy:
0.7461
Epoch 89/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5520 - accuracy:
0.7331
Epoch 90/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5396 - accuracy:
0.7253
Epoch 91/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5445 - accuracy:
0.7357
Epoch 92/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5368 - accuracy:
0.7344
Epoch 93/150
77/77 [==============================] - 0s 3ms/step - loss: 0.5402 - accuracy:
0.7344
Epoch 94/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5334 - accuracy:
0.7396
Epoch 95/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5345 - accuracy:
0.7448
Epoch 96/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5429 - accuracy:
0.7435
Epoch 97/150
86
Dhirajlal Gandhi College of Technology Department of Information
Technology
87
Dhirajlal Gandhi College of Technology Department of Information
Technology
Epoch 117/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5205 - accuracy:
0.7604
Epoch 118/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5230 - accuracy:
0.7448
Epoch 119/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5199 - accuracy:
0.7500
Epoch 120/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5192 - accuracy:
0.7513
Epoch 121/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5258 - accuracy:
0.7552
Epoch 122/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5228 - accuracy:
0.7461
Epoch 123/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5276 - accuracy:
0.7552
Epoch 124/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5260 - accuracy:
0.7643
Epoch 125/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5255 - accuracy:
0.7487
Epoch 126/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5168 - accuracy:
0.7409
Epoch 127/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5227 - accuracy:
0.7617
Epoch 128/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5180 - accuracy:
0.7643
Epoch 129/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5194 - accuracy:
0.7500
Epoch 130/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5168 - accuracy:
0.7591
Epoch 131/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5245 - accuracy:
0.7474
Epoch 132/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5165 - accuracy:
0.7539
Epoch 133/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5201 - accuracy:
0.7591
Epoch 134/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5218 - accuracy:
0.7513
Epoch 135/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5166 - accuracy:
0.7500
Epoch 136/150
88
Dhirajlal Gandhi College of Technology Department of Information
Technology
Result:
Thus the program to implement and build a Deep neural network model which predicts
the age and gender of a person using the given pre-trained models has been
executes and successfully verified.
Viva Questions:
1. What is the primary goal of deep
learning?
89
Dhirajlal Gandhi College of Technology Department of Information
Technology
2. What kind of neural network is most frequently applied to the image classification?
a. RNN (Recurrent Neural Network) b. CNN (Convolutional Neural Network)
c. FNN (Feedforward Neural Network) d. LSTM (Long Short-Term Memory)
3. What is the main benefit of deep neural networks over shallow neural networks?
a. Faster training b. Simplicity of architecture
c. Ability to learn complex features d. Lower memory usage
4. What is the primary advantage of using mini-batch gradient descent over batch gradient
descent?
a. Faster convergence b. Lower memory usage
c. Guaranteed global minimum d. Simplicity of implementation
5. What is the main function of pooling layer in a convolutional neural network (CNN)?
a. To add non-linearity b. To increase model complexity
c. To reduce the spatial dimensions of feature maps d. To add noise to the input
data
9. Which deep learning system is known for its dynamic computation graph and
was created by Facebook AI Research?
a. TensorFlow b. PyTorch c. Keras d. Theano
10. What is the main benefit of batch gradient descent in deep learning over
stochastic gradient descent?
a. It converges faster to the optimal
solution. b. It reduces memory usage during
training.
c. It provides a more accurate estimate of the gradient.
d. It requires less computation.
Practice Exercise:
90
Dhirajlal Gandhi College of Technology Department of Information
Technology
91
Dhirajlal Gandhi College of Technology Department of Information
Technology
92
Dhirajlal Gandhi College of Technology Department of Information
Technology
93
Dhirajlal Gandhi College of Technology Department of Information
Technology
94
Dhirajlal Gandhi College of Technology Department of Information
Technology
95
Dhirajlal Gandhi College of Technology Department of Information
Technology
96