Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
29 views127 pages

Dhi Ai Lab

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

Dhi Ai Lab

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

lOMoARcPSD|18803293

lOMoARcPSD|18803293

Program:
# Breadth-First Search (BFS) algorithm
graph = { '5' : ['3','7'], '3' : ['2', '4'], '7' : ['8'], '2' : [], '4' : ['8'], '8' : []
}

visited = [] # List for visited


nodes. queue = [] #Initialize a
queue

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


BFS
visited.append(node)
queue.append(node)
while queue: # Creating loop to visit each
node m = queue.pop(0)
print (m, end = " ")
for neighbour in graph[m]:
if neighbour not in visited:
visited.append(neighbour)
queue.append(neighbour)

# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, '5')
lOMoARcPSD|18803293

Mahendra College of Engineering Department of Electronics and


Communication Engineering

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:

Breadth-First Search (BFS)


algorithm:
1. Create an empty queue and enqueue
the starting node
2. Mark the starting
node as visited
3. While the queue is not empty, dequeue a node from the
queue and visit it
4. Enqueue all of its neighbors that have not been visited yet, and
mark them as visited
5. Repeat steps 3-4 until the
queue is empty

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

3. The Breadth First Search traversal of a graph will result into?


a) Linked List b) Tree c) Graph with back edges d) Arrays

4. Which of the following is not an application of Breadth First Search?


a) Finding shortest path between two nodes b) Finding bipartiteness of a
graph c) GPS navigation system d) Path Finding

5. When the Breadth First Search of a graph is unique?


a) When the graph is a Binary Tree b) When the graph is a Linked List
c) When the graph is a n-ary Tree d) When the graph is a Ternary 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

7. In BFS, how many times a node is visited?


a) Once b) Twice c) Equivalent to number of indegree of the node d) Thrice

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

a)MNOPQR b)NQMPOR c)QMNPRO d)QMNPOR


2
Dhirajlal Gandhi College of Technology Department of Information
Technology

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.

Algorithm: Depth-First Search


(DFS) algorithm:
1. Mark the starting node as
visited and print it
2. For each adjacent node of the current node that has not been
visited, repeat step 1
3. If all adjacent nodes have been visited, backtrack to the previous node
and repeat step 2
4. Repeat steps 2-3 until all nodes
have been visited

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' : [] }

visited = set() # List


for visited nodes.
queue = []
#Initialize a queue

def dfs(visited, graph, node):


#function for DFS
if
node
not
in
visit
ed:
print
(nod
e)
visit
ed.a
dd(n
ode)
for neighbour
in
graph[node]:
dfs(visited,
graph,
neighbour)

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

2. Time Complexity of DFS is? (V – number of vertices, E – number of


edges)
a) O(V + E) b) O(V) c) O(E) d) O(V*E)

3. The Data structure used in standard implementation of Breadth First Search


is?
a) Stack b) Queue c) Linked List d)
Tree

4. The Depth First Search traversal of a graph will result


into?
a) Linked List b) Tree c) Graph with back edges d)
Array

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

6. Which of the following is not an application of Depth First


Search?
a) For generating topological sort of a
graph
b) For generating Strongly Connected Components of a directed
graph
c) Detecting cycles in the graph d) Peer to Peer
Networks

7. When the Depth First Search of a graph is


unique?
a) When the graph is a Binary Tree b) When the graph is a Linked
List
c) When the graph is a n-ary Tree d) When the graph is a ternary
Tree

8. Regarding implementation of Depth First Search using stacks, what is the


maximum distance between two nodes present in the stack? (Considering each edge
length 1)
a) Can be anything b) 0 c) At most 1 d) Insufficient
Information

9. In Depth First Search, how many times a node is


visited?
a) Once b) Twice c) Equivalent to number of indegree of the node d)
Thrice

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.

Algorithm: Algorithm for A*


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

Program:
from queue
import
PriorityQueue v
=14
graph =[[] for i in range(v)]

# Function For Implementing Best First Search


# Gives output path having lowest cost

def best_first_search(actual_Src, target, n):


visited =[False] *n
pq
=Priority
Queue()
pq.put((0,
actual_Src
))
visited[ac
tual_Src]
=True
print("A*
:")
while pq.empty() ==False:
u =pq.get()[1]
# Displaying the path
having lowest cost
print(u, end=" ")
if u ==target:
break
for v, c in graph[u]:
if
visite
d[v]
==Fal
se:
v
i
s
i
t
e
d
[
v
]
=
T
r
u
e
p
q
.
p
u
t
(
(
c
,
v
)
)

9
Dhirajlal Gandhi College of Technology Department of Information
Technology

print()

# Function for adding edges to


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

# The nodes shown in above example(by alphabets) are


# implemented using integers addedge(x,y,cost);
addedge(0, 1, 3)
addedge(0, 2, 6)
addedge(0, 3, 5)
addedge(1, 4, 9)
addedge(1, 5, 8)
addedge(2, 6, 12)
addedge(2, 7, 14)
addedge(3, 8, 7)
addedge(8, 9, 5)
addedge(8, 10, 6)
addedge(9, 11, 1)
addedge(9, 12, 10)
addedge(9, 13, 2)

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

OUTPUT:
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

D. A function that checks if the goal node has been reached.

3. What is the purpose of the g(n) function in the A* search


algorithm? A. To calculate the exact cost from the start node to the
current node. B. To estimate the cost from the current node to the
goal node.
C. To track the path from the start node to the current
node. D. To store the visited nodes during the search
process.

4. Which of the following best describes the admissibility property in relation to


the heuristic function in A* search algorithm?
A. The heuristic function never overestimates the actual cost to reach the goal node.
B. The heuristic function always underestimates the actual cost to reach the goal node.
C. The heuristic function provides an accurate estimate of the actual cost to reach the
goal node.
D. The heuristic function does not affect the search process in A* algorithm.

5. In A* search algorithm, the f(n) function is calculated


as: A. f(n) = g(n) + h(n)
B. f(n) = g(n) * h(n)
C. f(n) = g(n) – h(n)
D. f(n) = g(n) / h(n)

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.

7. What is the purpose of the closed list in the A* search


algorithm? A. To store the path from the start node to the current
node.
B. To keep track of the nodes that have been visited and expanded.
C. To store the nodes that have been expanded and their associated
costs. D. To select the next node to expand based on the lowest path
cost.

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

C. Randomizing the order of expanding


nodes. D. Ignoring the closed list.

10. Which of the following search algorithms is a generalization of the A* search


algorithm and guarantees finding an optimal path even with inconsistent heuristic
functions?
A. Depth-First Search (DFS)
B. Breadth-First Search (BFS)
C. Iterative-Deepening A* (IDA*)
D. Uniform Cost Search (UCS)

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

2. B) IMPLEMENTING INFORMED SEARCH ALGORITHMS - MEMORY-


BOUNDED A*

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={}

def applyAOStar(self): # starts a


recursive AO* algorithm
self.aoStar(self.start, False)

def getNeighbors(self, v): # gets the Neighbors


of a given node return self.graph.get(v,'')

def getStatus(self,v): # return the status of a given node

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 getHeuristicNodeValue(self, n):


return self.H.get(n,0) # always return the heuristic value of a given node

def setHeuristicNodeValue(self, n, value):


self.H[n]=value # set the revised heuristic value of a given node

def printSolution(self):
print("FOR GRAPH SOLUTION, TRAVERSE THE GRAPH FROM THE
START NODE:",self.start)
print("------------------------------------------------------------")
print(self.solutionGraph)
print("------------------------------------------------------------")

def computeMinimumCostChildNodes(self, v): # Computes the Minimum Cost of


child nodes of a given node v
minimumCost=0
costToChildNodeListDict={}
costToChildNodeListDict[minimumCost]=[]
flag=True
for nodeInfoTupleList in self.getNeighbors(v): # iterate over all the set of child
node/s cost=0
nodeList=[]
for c, weight in nodeInfoTupleList:
cost=cost+self.getHeuristicNodeValue(c)+weight
nodeList.append(c)
if flag==True: # initialize Minimum Cost with the cost of first set of child
node/s minimumCost=cost
costToChildNodeListDict[minimumCost]=nodeList # set the Minimum Cost child
node
/s flag=False
else: # checking the Minimum Cost nodes with the
current Minimum Cost if minimumCost>cost:
minimumCost=cost
costToChildNodeListDict[minimumCost]=nodeList # set the
Minimum Cost child
node/s
return minimumCost, costToChildNodeListDict[minimumCost] #
return Minimum Cost and Minimum Cost child node/s

def aoStar(self, v, backTracking): # AO* algorithm for a start node


and backTracking status flag
print("HEURISTIC VALUES :", self.H)

15
Dhirajlal Gandhi College of Technology Department of Information
Technology

print("SOLUTION GRAPH :", self.solutionGraph)


print("PROCESSING NODE :", v)
print("-----------------------------------------------------------------------------------------")
if self.getStatus(v) >= 0: # if status node v >= 0, compute Minimum Cost nodes of
v minimumCost, childNodeList = self.computeMinimumCostChildNodes(v)
print(minimumCost, childNodeList)
self.setHeuristicNodeValue(v, minimumCost)
self.setStatus(v,len(childNodeList))
solved=True # check the Minimum Cost nodes of v are
solved for childNode in childNodeList:
self.parent[childNode]=v
if self.getStatus(childNode)!=-1:
solved=solved & False
if solved==True: # if the Minimum Cost nodes of v are solved, set the current
node status as solved(-1)
self.setStatus(v,-1)
self.solutionGraph[v]=childNodeList # update the solution graph with the
solved nodes which may be a part of solution
if v!=self.start: # check the current node is the start node for backtracking the
current node value
self.aoStar(self.parent[v], True) # backtracking the current node value
with backtracking status set to true
if backTracking==False: # check the current call is not for
backtracking for childNode in childNodeList: # for each Minimum
Cost child node
self.setStatus(childNode,0) # set the status of child node to 0(needs exploration)
self.aoStar(childNode, False) # Minimum Cost child node is further explored
with backtracking status as false

#for simplicity we ll consider heuristic distances


given print ("Graph - 1")
h1 = {'A': 1, 'B': 6, 'C': 2, 'D': 12, 'E': 2, 'F': 1, 'G': 5, 'H': 7, 'I': 7, 'J': 1}
graph1 = { 'A': [[('B', 1), ('C', 1)], [('D', 1)]], 'B': [[('G', 1)], [('H', 1)]], 'C': [[('J', 1)]],
'D': [[('E', 1), ('F', 1)]], 'G': [[('I', 1)]]
}

G1= Graph(graph1, h1, 'A')


G1.applyAOStar()
G1.printSolution()

print ("Graph - 2")


h2 = {'A': 1, 'B': 6, 'C': 12, 'D': 10, 'E': 4, 'F': 4, 'G': 5, 'H': 7} # Heuristic values of
Nodes graph2 = { # Graph of Nodes and Edges
'A': [[('B', 1), ('C', 1)], [('D', 1)]], # Neighbors of Node 'A', B, C & D with repective weights
'B': [[('G', 1)], [('H', 1)]], # Neighbors are included in a list of lists
'D': [[('E', 1), ('F', 1)]] # Each sublist indicate a "OR" node or "AND" nodes

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

2. How many types of informed search method are in artificial intelligence?


a) 1 b) 2 c) 3 d) 4

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

5. What is the heuristic function of greedy best-first search?


a) f(n) != h(n) b) f(n) < h(n) c) f(n) = h(n) d) f(n) > h(n)

6. Which search uses only the linear space for searching?


a) Best-first search b) Recursive best-first
search c) Depth-first search d) None of the
mentioned

7. Which method is used to search better by learning?


a) Best-first search b) Depth-first search
c) Metalevel state space d) None of the mentioned

8. Which search is complete and optimal when h(n) is consistent?


a) Best-first search b) Depth-first
search c) Both Best-first & Depth-first search d) A* search

9. Which is used to improve the performance of heuristic search?


a) Quality of nodes b) Quality of heuristic
function c) Simple form of nodes 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

1. Develop a code by implementing the Informed search algorithm- Memory Bounded A*


2. Implement the code for accessing the Basketball Logo through Informed
search algorithm- Memory Bounded A*
3. Write a Program to Implement N-Queens Problem using Python
4. Consider the following grid map, where each cell is either passable (0) or blocked (1):
000100000
000100000
000100000
000100000
000000000
000000000
Write the program to find the shortest path from the start to the goal using A*
algorithm.
Note: Moves can in any of the four cardinal directions (up, down, left, right) but
not diagonally.
The start position is (0, 0) and the goal position is (5, 8).

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

# store the feature matrix (X) and response vector


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

# splitting X and y into training and testing sets


from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)

# training the model on training set


from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb.fit(X_train, y_train)

# making predictions on the testing


set y_pred = gnb.predict(X_test)

# 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

4. In Naive Bayes Algorithm, how is the class of a new instance


determined?
a) By calculating the conditional probability of each
class b) By applying the K-nearest neighbors algorithm
c) By minimizing the squared error between the predicted and actual
values d) By using gradient descent to optimize the classification boundary

5. Which assumption is violated by Naive Bayes Algorithm if there is a high degree


of interdependence among the features?
a) Linearity assumption b) Normality assumption
c) Independence assumption d) Homoscedasticity
assumption

6. Which variant of Naive Bayes Algorithm is suitable for handling continuous-


valued features?
a) Gaussian Naive Bayes b) Multinomial Naive
Bayes c) Complement Naive Bayes d) Bernoulli Naive
Bayes

7. Which step is involved in the training phase of Naive Bayes


Algorithm?
a) Calculating the prior probabilities of each
class
b) Estimating the conditional probabilities of each feature given the
class c) Combining the prior and conditional probabilities
d) All of the above

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

10. Which of the following is a limitation of Naive Bayes


Algorithm?
a) It cannot handle missing values in the
dataset.
b) It is computationally expensive for large datasets.
c) It requires a large amount of labeled training
data. d) It is sensitive to irrelevant features in the
dataset.

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.

2. Determine the relationships between variables: The next step is to


determine the probabilistic relationships between the variables. This
can be done by identifying the causal relationships between the
variables or by using data to estimate the conditional probabilities of
each variable given its parents.

3. Construct the Bayesian Network: The Bayesian Network can be


constructed by representing the variables as nodes in a directed
acyclic graph (DAG). The edges between the nodes represent the
conditional dependencies between the variables.

4. Assign probabilities to the variables: Once the structure of the Bayesian


Network has been defined, the probabilities of each variable must be
assigned. This can be done by using expert knowledge, data, or a
combination of both.

5. Inference: Inference refers to the process of using the Bayesian


Network to make predictions or draw conclusions. This can be
done by using various inference algorithms, such as variable
elimination or belief propagation.

6. Learning: Learning refers to the process of updating the probabilities in


the Bayesian Network based on new data. This can be done using
various learning algorithms, such as maximum likelihood or Bayesian
learning.
7. Evaluation: The final step in implementing a Bayesian Network is
to evaluate its performance. This can be done by comparing the
predictions of the model to actual data and computing various
performance metrics, such as accuracy or precision.

Database: 0 1 2 3 4 Total

Cleveland: 164 55 36 35 13 303

30
Dhirajlal Gandhi College of Technology Department of Information
Technology

Attribute Information:

age: age in years


sex: sex (1 = male; 0 = female)
cp: chest pain type Value
1: typical angina Value 2:
atypical angina Value 3:
non-anginal pain Value 4:
asymptomatic
trestbps: resting blood pressure (in mm Hg on admission to the hospital)
chol: serum cholestoral in mg/dl
fbs: (fasting blood sugar > 120 mg/dl) (1 = true; 0 = false)
restecg: resting electrocardiographic results
Value 0: normal
Value 1: having ST-T wave abnormality (T wave inversions and/or ST elevation or
depression of > 0.05 mV)
Value 2: showing probable or definite left ventricular hypertrophy by Estes’
criteria thalach: maximum heart rate achieved
exang: exercise induced angina (1 = yes; 0 = no)
oldpeak = ST depression induced by exercise relative to
rest slope: the slope of the peak exercise ST segment
Value 1: upsloping
Value 2: flat
Value 3: downsloping
thal: 3 = normal; 6 = fixed defect; 7 = reversable defect
Heartdisease: It is integer valued from 0 (no presence) to 4.

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)

print('Sample instances from the dataset are given below')


print(heartDisease.head())

print('\n Attributes and datatypes')


print(heartDisease.dtypes)

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)

print('\n Inferencing with Bayesian Network:')


HeartDiseasetest_infer = VariableElimination(model)

print('\n 1. Probability of HeartDisease given evidence= restecg')


q1=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'restecg':1})
print(q1)

print('\n 2. Probability of HeartDisease given evidence= cp ')


q2=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'cp':2})

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

Attributes and datatypes


age int64
sex int64
exang int64
cp int64
trestbps int64
chol int64
fbs int64
restecg int64
thalach int64
Unnamed: 9 float64
oldpeak float64
slope int64
ca object thal
object heartdisease
int64 dtype: object

Learning CPD using Maximum likelihood estimators

Inferencing with Bayesian Network:

1. Probability of HeartDisease given evidence= restecg


+-----------------+---------------------+
| heartdisease | phi(heartdisease) |
+=================+=====================+
| heartdisease(0) | 0.2000 |
+-----------------+---------------------+
| heartdisease(1) | 0.2000 |
+-----------------+---------------------+
| heartdisease(2) | 0.2000 |
+-----------------+---------------------+
| heartdisease(3) | 0.2000 |
+-----------------+---------------------+
| heartdisease(4) | 0.2000 |
+-----------------+---------------------+

33
Dhirajlal Gandhi College of Technology Department of Information
Technology

2. Probability of HeartDisease given evidence= cp


+-----------------+---------------------+
| heartdisease | phi(heartdisease) |
+=================+=====================+
| heartdisease(0) | 0.2000 |
+-----------------+---------------------+
| heartdisease(1) | 0.2000 |
+-----------------+---------------------+
| heartdisease(2) | 0.2000 |
+-----------------+---------------------+
| heartdisease(3) | 0.2000 |
+-----------------+---------------------+
| heartdisease(4) | 0.2000 |
+-----------------+---------------------+

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:

1. How many terms are required for building a bayes model?


a) 1 b) 2 c) 3 d) 4

2. What is needed to make probabilistic systems feasible in the world?


a) Reliability b) Crucial robustness c) Feasibility d) None of the mentioned

3. Where does the bayes rule can be used?


a) Solving queries b) Increasing complexity
c) Decreasing complexity d) Answering probabilistic query

4. What does the bayesian network provides?


a) Complete description of the domain b) Partial description of the
domain c) Complete description of the problem d) None of the mentioned

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

6. How the bayesian network can be used to answer any query?


a) Full distribution b) Joint distribution
c) Partial distribution d) All of the mentioned

34
Dhirajlal Gandhi College of Technology Department of Information
Technology

7. How the compactness of the bayesian network can be described?


a) Locally structured b) Fully structured c) Partial structure d) All of the mentioned

8. To which does the local structure is associated?


a) Hybrid b) Dependant c) Linear d) None of the mentioned

9. Which condition is used to influence a variable directly by all the others?


a) Partially connected b) Fully connected
c) Local connected d) None of the mentioned

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 :

5. Determine the value of model term parameter β 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

a) Multivariate Logistic Regression b) Logistic Regression


c) Linear Regression d) Multivariate Linear Regression
View Answer

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

4. The target variable is represented along


a) Y axis b) X axis
c) Either Y-axis or X-axis, it doesn’t matter d) Depends on the dataset

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

6. The independent variable is represented along


a) Either X-axis or Y-axis, it doesn’t matter b) Y axis c) X axis d) Depends on the
dataset

7. How many variables are required to represent a linear regression model?


a) 3 b) 2 c) 1 d) 4

8. What does (x(5), y(5)) represent or imply?


a) There are 5 training examples b) The values of x and y are
5 c) The fourth training example d) The fifth training
example

9. Hypothesis h maps from x (independent variable) to y (dependent


variable). a) True b) False

10. Learning algorithm outputs the


hypothesis. a) False b) True

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

#Split the data set into training data


and test data from
sklearn.model_selection import
train_test_split x =
raw_data.drop('Kyphosis', axis = 1)
y = raw_data['Kyphosis']
x_training_data, x_test_data, y_training_data, y_test_data = train_test_split(x,
y, test_size =
0.3)

#Train the decision tree model


from sklearn.tree import DecisionTreeClassifier

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)

#Measure the performance of the decision tree


model from sklearn.metrics import
classification_report
from sklearn.metrics import confusion_matrix
print(classification_report(y_test_data, predictions))
print(confusion_matrix(y_test_data, predictions))

#Train the random forests model


from sklearn.ensemble import RandomForestClassifier
random_forest_model = RandomForestClassifier()
random_forest_model.fit(x_training_data, y_training_data)
random_forest_predictions = random_forest_model.predict(x_test_data)

#Measure the performance of the random forest model


print(classification_report(y_test_data, random_forest_predictions))
print(confusion_matrix(y_test_data, random_forest_predictions))

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

absent 0.94 0.79 0.86 19


present 0.56 0.83 0.67 6

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

absent 0.86 1.00 0.93 19


present 1.00 0.50 0.67 6

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

2. Random Forest algorithm is an example of which type of learning?


a) Supervised learning b) Unsupervised learning
c) Reinforcement learning d) Semi-supervised learning
3. In Random Forest, each decision tree is built using:
a) The entire dataset b) A random subset of
features c) A random subset of instances d) A fixed number of
iterations

4. What is bagging in the context of Random Forest algorithm?


a) A technique to handle missing values in the
dataset b) A method for feature scaling
c) The process of generating multiple bootstrap samples
d) The procedure for selecting optimal
hyperparameters

5. Random Forest algorithm is susceptible to overfitting when:


a) The number of decision trees is too small
b) The dataset has a large number of
features
c) The dataset is perfectly balanced d) The decision trees are shallow

6. What is the purpose of pruning in decision trees?


a) To reduce overfitting by removing unnecessary
branches b) To increase the depth of the tree for better
accuracy
c) To randomly split the data for better generalization
d) To remove outliers from the dataset

7. Which algorithm is commonly used for constructing decision trees?


a) ID3 b) K-means c) Support Vector Machines (SVM) d) Naive Bayes

8. How is impurity measured in decision tree algorithms?


a) Gini Index b) Mean Squared Error (MSE)
c) Pearson Correlation Coefficient d) Entropy

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

10. What is the main advantage of decision trees?


a) Easy to interpret and visualize b) Handles non-linear relationships well
c) Requires minimal training data d) Provides high precision without overfitting

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)

# print the label type of cancer('malignant' 'benign')


print("Labels: ", cancer.target_names)
# print
data(featur
e)shape
cancer.data
.shape
# print the cancer data features (top 5 records)
print(cancer.data[0:5])
# print the cancer labels (0:malignant, 1:benign)
print(cancer.target)
# Import train_test_split function
from sklearn.model_selection import train_test_split
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(cancer.data,
cancer.target, test_size=0.3,random_state=109) # 70% training and 30%
test
#Import svm model
from sklearn import svm

#Create a svm Classifier

52
Dhirajlal Gandhi College of Technology Department of Information
Technology

clf = svm.SVC(kernel='linear') # Linear Kernel

#Train the model using the training


sets clf.fit(X_train, y_train)

#Predict the response for test dataset


y_pred = clf.predict(X_test)
#Import scikit-learn metrics module for accuracy
calculation from sklearn import metrics

# Model Accuracy: how often is the classifier correct?


print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
# Model Precision: what percentage of positive tuples are labeled as such?
print("Precision:",metrics.precision_score(y_test, y_pred))

# Model Recall: what percentage of positive tuples are labelled as such?


print("Recall:",metrics.recall_score(y_test, y_pred))

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

1.043e-01 1.809e-01 5.883e-02 7.572e-01 7.813e-01 5.438e+00 9.444e+01


1.149e-02 2.461e-02 5.688e-02 1.885e-02 1.756e-02 5.115e-03 2.254e+01
1.667e+01 1.522e+02 1.575e+03 1.374e-01 2.050e-01 4.000e-01 1.625e-01
2.364e-01 7.678e-02]]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 0 0 1 0 0 1 1 1 1 0 1 0 0 1 1 1 1 0 1 0 0
1 0 1 0 0 1 1 1 0 0 1 0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 1 0 1 1
1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 1 1 0 1 1 0 1 1 1 1 0 1
1 1 1 1 1 1 1 1 0 1 1 1 1 0 0 1 0 1 1 0 0 1 1 0 0 1 1 1 1 0 1 1 0 0 0 1 0
1 0 1 1 1 0 1 1 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 1 1 0 1 0 0 0 0 1 1 0 0 1 1
1 0 1 1 1 1 1 0 0 1 1 0 1 1 0 0 1 0 1 1 1 1 0 1 1 1 1 1 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1
1 1 0 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0
0 1 0 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1
1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 1 1 1 1 1 0 1 1
0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1
1 1 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 0 0 1 0 1 0 1 1 1 1 1 0 1 1 0 1 0 1 0 0
1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 0 0 0 0 0 0 1]
Accuracy: 0.9649122807017544
Precision: 0.9811320754716981
Recall: 0.9629629629629629

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

3. In SVM, the hyperplane that maximizes the margin is selected based


on:
a) The nearest support vectors b) The class labels of the training
samples c) The Euclidean distance between data points d) The average value of the
input features

4. Which kernel function is commonly used in SVM to handle non-linearly separable


data?
a) Linear kernel b) Polynomial
kernel c) Radial basis function (RBF) kernel d) Sigmoid
kernel

5. In SVM, what is the role of the regularization parameter


C?

54
Dhirajlal Gandhi College of Technology Department of Information
Technology

a) It controls the width of the margin b) It determines the number of support


vectors c) It balances the tradeoff between training error and model complexity
d) It specifies the kernel function to be used

6. What is the purpose of the kernel trick in SVM?


a) It transforms the input features into a higher-dimensional
space b) It improves the convergence speed of the algorithm
c) It regularizes the model to prevent overfitting
d) It optimizes the weights of the support vectors

7. Which of the following statements about SVM is true?


a) SVM is not affected by outliers in the training data
b) SVM can handle multi-class classification tasks directly
c) SVM is generally not suitable for high-dimensional
data
d) SVM is computationally less expensive than other classification algorithms

8. Which of the following is a potential drawback of using SVM?


a) Difficulty in interpreting the model’s decision-making
process b) High sensitivity to feature scaling
c) Limited effectiveness on small-sized datasets
d) Inability to handle imbalanced class distributions

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
)

ereg = VotingRegressor([("gb", reg1), ("rf",


reg2), ("lr", reg3)])
e
r
e
g
.
f
i
t
(
X
,

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.plot(pred1, "gd", label="GradientBoostingRegressor")


plt.plot(pred2, "b^", label="RandomForestRegressor")
plt.plot(pred3, "ys", label="LinearRegression")
plt.plot(pred4, "r*", ms=10, label="VotingRegressor")

plt.tick_params(axis="x", which="both", bottom=False, top=False, labelbottom=False)


plt.ylabel("predicted")
plt.xlabel("training samples")
plt.legend(loc="best")
plt.title("Regressor predictions and their average")

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

2. What is true about an ensembled classifier?


1. Classifiers that are more “sure” can vote with more conviction
2. Classifiers can be more “sure” about a particular part of the space

59
Dhirajlal Gandhi College of Technology Department of Information
Technology

3. Most of the times, it performs better than a single classifier


A. 1 and 2 B. 1 and 3 C. 2 and 3 D. All of the above
3. Which of the following option is / are correct regarding benefits of ensemble model?
1. Better performance
2. Generalized models
3. Better interpretability
A. 1 and 3 B. 2 and 3 C. 1 and 2 D. 1, 2 and 3

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

9. If you use an ensemble of different base models, is it necessary to tune the


hyper parameters of all base models to improve the ensemble performance?
A. Yes B. No C. can’t say

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:

1. The goal of clustering is to-


a. Divide the data points into groups b. Classify the data point into different
classes c. Predict the output values of input data points d. All of the above

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

4. Which version of the clustering algorithm is most sensitive to outliers?


a. K-means clustering algorithm b. K-modes clustering
algorithm c. K-medians clustering algorithm d. None

5. Which of the following is a bad characteristic of a dataset for clustering


analysis- a. Data points with outliers b. Data points with different
densities
c. Data points with non-convex shapes d. All of the above

6. For clustering, we do not require-


a. Labeled data b. Unlabeled data c. Numerical data d. Categorical data

7. Which of the following is an application of clustering?


a. Biological network analysis b. Market trend prediction
c. Topic modeling d. All of the above

8. On which data type, we cannot perform cluster analysis?


a. Time series data b. Text data c. Multimedia data d. None

9. Netflix’s movie recommendation system uses-


a. Supervised learning b. Unsupervised learning
c. Reinforcement learning d. All of the above

10. The final output of Hierarchical clustering


is- a. The number of cluster centroids
b. The tree representing how close the data points are to each
other c. A map defining the similar data points into individual
groups
64
Dhirajlal Gandhi College of Technology Department of Information
Technology

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

2. Where does the bayes rule can be used?


a. Solving queries b. Increasing complexity
c. Decreasing complexity d. Answering probabilistic query

3. What does the bayesian network provides?


a. Complete description of the domain b. Partial description of the
domain c. Complete description of the problem d. None of the above

68
Dhirajlal Gandhi College of Technology Department of Information
Technology

4. is the process of calculating a probability distribution of interest e.g. P(A


| B=True), or P(A,B|C, D=True).
a. Diagnostics b. Supervised anomaly detection c. Inference d. Prediction
5. The Distributive law simply means that if we want to marginalize out the variable A
we can perform the calculations on the subset of distributions that contain a.

a. TRUE b. FALSE c. Can be true or false d. Can not say

6. Bayesian networks are a factorized representation of the full joint.


a. TRUE b. FALSE c. Can be true or false d. Can not say

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

8. Which condition is used to influence a variable directly by all the others?


a. Partially connected b. Fully connected
c. Local connected d. None of the above

9. To which does the local structure is associated?


a. Hybrid b. Dependant c. Linear d. None of the above

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:

1. Develop a code by implementing the probability relationship check between


two dataset using Bayesian Networks
2. Write a EM code for implementing the Bayesian Networks.
3. Write a EM code for understand the heart diseases and implement using the
Bayesian Networks.
4. Develop a code to implement the Bayesian Networks for performing the
Iteration process and Analyze the random networks.
5. 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 Java/Python ML library classes/API.
6. Write a python program to EM algorithm to learn parameters for a
Bayesian network using the pgmpy library

69
Dhirajlal Gandhi College of Technology Department of Information
Technology

7. Write a program to implement Bayesian Network that will model the


performance of a student on an exam.

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

# be sure to change the file path


# if you have the dataset in another
# directly than the working folder
df = pd.read_csv('winequality-red.csv')
df.head()

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)

# it drops the training data


# from the original dataframe
val_df = df.drop(train_df.index)

# calling to (0,1) range


max_val = train_df.max(axis= 0)
min_val = train_df.min(axis= 0)

range = max_val - min_val


train_df = (train_df - min_val)/(range)

val_df = (val_df- min_val)/range

# now let's separate the targets and


labels X_train =
train_df.drop('quality',axis=1) X_val =
val_df.drop('quality',axis=1)
y_train = train_df['quality']
y_val = val_df['quality']

# We'll need to pass the shape


# of features/inputs as an argument
# in our model, so let's define a variable
# to save it.
input_shape = [X_train.shape[1]]

input_shape

model = tf.keras.Sequential([
tf.keras.layers.Dense(units=1,input_shape=input_shape)])

# after you create your model it's


# always a good habit to print out it's
summary model.summary()

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

# adam optimizer works pretty well for


# all kinds of problems and is a good starting
point model.compile(optimizer='adam',

# MAE error is good for


# numerical predictions
loss='mae')

losses = model.fit(X_train, y_train,

validation_data=(X_val, y_val),

# it will use 'batch_size' number


# of examples per example
batch_size=256,
epochs=15, # total epoch

# this will pass the first 3 rows of features


# of our data as input to make
predictions model.predict(X_val.iloc[0:3,
:])

y_val.iloc[0:3]

loss_df = pd.DataFrame(losses.history)

# history stores the loss/val


# loss in each epoch

# loss_df is a dataframe which


# contains the losses so we can
# plot it to visualize our model
training loss_df.loc[:,
['loss','val_loss']].plot()

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

2. Which is true for neural networks?


a. It has set of nodes and connections
b. Each node computes it’s weighted input
c. Node could be in excited state or non-excited
state d. All of the above

3. What are models in neural networks?


a. Representation of biological neural networks
b. Mathematical representation of our understanding
c. Both first & second d. None of the above

4. How many types of Artificial Neural Networks?


a. 3 b. 2 c. 4 d. 5

5. What does RNN Stands for?


a. Recurrent Neural Network b. Recurring Neural
Network c. Removable Neural Network d. None of the
above
77
Dhirajlal Gandhi College of Technology Department of Information
Technology

6. What is auto-association task in neural networks?


a. Predicting the future inputs b. Related to storage & recall
task c. Find relation between 2 consecutive inputs d. All of the above

7. What is plasticity in neural networks?


a. Input pattern has become static b. Input pattern keeps on
changing c. Output pattern keeps on changing d. None of the above

8. Signal transmission at synapse is a?


a. Chemical process b. Physical process
c. Both chemical & physical process d. None of the above

9. Operations in the neural networks can perform what kind of operations?


a. Parallel b. Serial c. Both parallel & serial d. None of the above

10. A neural network is a network or circuit of


neurons. a. True b. False

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

# load the dataset


dataset = loadtxt('pima-indians-diabetes.csv', delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,0:8]
y = dataset[:,8]

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

# compile the keras model


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

# fit the keras model on


the dataset model.fit(X, y,
epochs=150,
batch_size=10)

# evaluate the keras model


_, accuracy = model.evaluate(X, y)

81
Dhirajlal Gandhi College of Technology Department of Information
Technology

print('Accuracy: %.2f' % (accuracy*100))


Output:
Epoch 1/150
77/77 [==============================] - 1s 3ms/step - loss: 2.8286 - accuracy:
0.3815
Epoch 2/150
77/77 [==============================] - 0s 3ms/step - loss: 1.0170 - accuracy:
0.4688
Epoch 3/150
77/77 [==============================] - 0s 3ms/step - loss: 0.8012 - accuracy:
0.5456
Epoch 4/150
77/77 [==============================] - 0s 3ms/step - loss: 0.7406 - accuracy:
0.6172
Epoch 5/150
77/77 [==============================] - 0s 3ms/step - loss: 0.7065 - accuracy:
0.6549
Epoch 6/150
77/77 [==============================] - 0s 3ms/step - loss: 0.7018 - accuracy:
0.6497
Epoch 7/150
77/77 [==============================] - 0s 3ms/step - loss: 0.6937 - accuracy:
0.6536
Epoch 8/150
77/77 [==============================] - 0s 3ms/step - loss: 0.6737 - accuracy:
0.6471
Epoch 9/150
77/77 [==============================] - 0s 3ms/step - loss: 0.6628 - accuracy:
0.6536
Epoch 10/150
77/77 [==============================] - 0s 3ms/step - loss: 0.6555 - accuracy:
0.6615
Epoch 11/150
77/77 [==============================] - 0s 3ms/step - loss: 0.6494 - accuracy:
0.6562
Epoch 12/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6463 - accuracy:
0.6706
Epoch 13/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6456 - accuracy:
0.6654
Epoch 14/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6476 - accuracy:
0.6706
Epoch 15/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6430 - accuracy:
0.6693
Epoch 16/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6314 - accuracy:
0.6810
Epoch 17/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6316 - accuracy:
0.6732
Epoch 18/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6257 - accuracy:
0.6862
Epoch 19/150

82
Dhirajlal Gandhi College of Technology Department of Information
Technology

77/77 [==============================] - 0s 2ms/step - loss: 0.6278 - accuracy:


0.6758
Epoch 20/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6360 - accuracy:
0.6706
Epoch 21/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6319 - accuracy:
0.6745
Epoch 22/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6231 - accuracy:
0.6810
Epoch 23/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6316 - accuracy:
0.6836
Epoch 24/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6261 - accuracy:
0.6797
Epoch 25/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6184 - accuracy:
0.6849
Epoch 26/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6247 - accuracy:
0.6797
Epoch 27/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6209 - accuracy:
0.6849
Epoch 28/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6302 - accuracy:
0.6771
Epoch 29/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6160 - accuracy:
0.6875
Epoch 30/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6378 - accuracy:
0.6706
Epoch 31/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6200 - accuracy:
0.6810
Epoch 32/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6208 - accuracy:
0.6862
Epoch 33/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6161 - accuracy:
0.6875
Epoch 34/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6181 - accuracy:
0.6862
Epoch 35/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6161 - accuracy:
0.6836
Epoch 36/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6193 - accuracy:
0.6784
Epoch 37/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6129 - accuracy:
0.6940
Epoch 38/150
77/77 [==============================] - 0s 2ms/step - loss: 0.6112 - accuracy:
0.6875

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

77/77 [==============================] - 0s 2ms/step - loss: 0.5795 - accuracy:


0.7214
Epoch 59/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5772 - accuracy:
0.7109
Epoch 60/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5766 - accuracy:
0.7161
Epoch 61/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5758 - accuracy:
0.7174
Epoch 62/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5766 - accuracy:
0.7135
Epoch 63/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5670 - accuracy:
0.7266
Epoch 64/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5674 - accuracy:
0.7292
Epoch 65/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5660 - accuracy:
0.7279
Epoch 66/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5701 - accuracy:
0.7240
Epoch 67/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5566 - accuracy:
0.7396
Epoch 68/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5718 - accuracy:
0.7148
Epoch 69/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5703 - accuracy:
0.7253
Epoch 70/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5617 - accuracy:
0.7344
Epoch 71/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5586 - accuracy:
0.7331
Epoch 72/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5605 - accuracy:
0.7253
Epoch 73/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5492 - accuracy:
0.7383
Epoch 74/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5999 - accuracy:
0.7266
Epoch 75/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5550 - accuracy:
0.7318
Epoch 76/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5611 - accuracy:
0.7279
Epoch 77/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5530 - accuracy:
0.7396

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

77/77 [==============================] - 0s 2ms/step - loss: 0.5321 - accuracy:


0.7448
Epoch 98/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5371 - accuracy:
0.7396
Epoch 99/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5359 - accuracy:
0.7370
Epoch 100/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5380 - accuracy:
0.7266
Epoch 101/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5298 - accuracy:
0.7422
Epoch 102/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5315 - accuracy:
0.7422
Epoch 103/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5327 - accuracy:
0.7383
Epoch 104/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5470 - accuracy:
0.7266
Epoch 105/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5286 - accuracy:
0.7474
Epoch 106/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5370 - accuracy:
0.7370
Epoch 107/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5330 - accuracy:
0.7500
Epoch 108/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5329 - accuracy:
0.7435
Epoch 109/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5405 - accuracy:
0.7318
Epoch 110/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5290 - accuracy:
0.7461
Epoch 111/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5258 - accuracy:
0.7435
Epoch 112/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5259 - accuracy:
0.7435
Epoch 113/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5321 - accuracy:
0.7526
Epoch 114/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5292 - accuracy:
0.7448
Epoch 115/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5254 - accuracy:
0.7500
Epoch 116/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5280 - accuracy:
0.7539

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

77/77 [==============================] - 0s 2ms/step - loss: 0.5131 - accuracy:


0.7552
Epoch 137/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5140 - accuracy:
0.7552
Epoch 138/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5130 - accuracy:
0.7552
Epoch 139/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5112 - accuracy:
0.7474
Epoch 140/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5240 - accuracy:
0.7526
Epoch 141/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5111 - accuracy:
0.7643
Epoch 142/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5129 - accuracy:
0.7500
Epoch 143/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5077 - accuracy:
0.7539
Epoch 144/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5085 - accuracy:
0.7617
Epoch 145/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5148 - accuracy:
0.7565
Epoch 146/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5155 - accuracy:
0.7526
Epoch 147/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5310 - accuracy:
0.7435
Epoch 148/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5162 - accuracy:
0.7448
Epoch 149/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5115 - accuracy:
0.7552
Epoch 150/150
77/77 [==============================] - 0s 2ms/step - loss: 0.5104 - accuracy:
0.7630
24/24 [==============================] - 0s 2ms/step - loss: 0.5008 - accuracy:
0.7617
Accuracy: 76.17

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

a. Data compression b. Feature


extraction c. Learning patterns from data d. Linear
regression

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

6. What kind of layer is commonly used in neural networks to add non-linearity?


a. Fully Connected Layer b. Pooling Layer
c. Convolutional Layer d. Activation Layer

7. What does “epoch” mean in deep


learning? a. The number of layers in a neural
network b. The learning rate of the
optimizer
c. One complete pass through the entire training
dataset d. The type of activation function used

8. In deep learning, what is the role of a loss function?


a. To measure the model’s prediction accuracy b. To initialize model
parameters c. To calculate the gradients for optimization d. To normalize
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

1. Develop a deep-learning Neural Network for experiencing different architectural model.


2. Develop a deep-learning Neural Network using the pima- indians diabetes dataset.
3. Write a python program to build a deep learning neural network model using the
Keras library
4. Write a python program to building a deep neural network model using Python and the
Keras library(multi-layer perceptron (MLP) model for multi-class classification)

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

You might also like