0% found this document useful (0 votes)
51 views59 pages

Cs3491-Aiml Lab Manual

Uploaded by

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

Cs3491-Aiml Lab Manual

Uploaded by

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

Om sakthi

Adhiparasakthi Engineering College, Melmaruvathur

Department of Information Technology

CS3491

ARTIFICIAL INTELLIGENCE & MACHINE LEARNING LABORATORY

LAB MANUAL

II YEAR IT
R-2021
Syllabus

PRACTICAL EXERCISES: 30 PERIODS

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


2. Implementation of Informed search algorithms (A*, memory-bounded A*)
3. Implement naïve Bayes models
4. Implement Bayesian Networks
5. Build Regression models
6. Build decision trees and random forests
7. Build SVM models
8. Implement ensembling techniques
9. Implement clustering algorithms
10. Implement EM for Bayesian networks
11. Build simple NN models
12. Build deep learning NN models

1
COURSE OBJECTIVES:

1. Study about uninformed and Heuristic search techniques.

2. Learn techniques for reasoning under uncertainty

3. Introduce Machine Learning and supervised learning algorithms


4. Study about ensemble and unsupervised learning algorithms
5. Learn the basics of deep learning using neural networks

COURSE OUTCOMES:

On completion of the course, students will be able to:


CO1: Use appropriate search algorithms for problem solving

CO2: Apply reasoning under uncertainty

CO3: Build supervised learning models

CO4: Build ensembling and unsupervised models

CO5: Build deep learning neural network models

TEXT BOOKS:
1. Stuart Russell and Peter Norvig, “Artificial Intelligence – A Modern Approach”,
Fourth Edition, Pearson Education, 2021.
2. Ethem Alpaydin, “Introduction to Machine Learning”, MIT Press, Fourth Edition,
2020.
REFERENCES:

1. Dan W. Patterson, “Introduction to Artificial Intelligence and Expert Systems”, Pearson


Education,2007

2. Kevin Night, Elaine Rich, and Nair B., “Artificial Intelligence”, McGraw Hill, 2008

3. Patrick H. Winston, "Artificial Intelligence", Third Edition, Pearson Education, 2006

2
4. Deepak Khemani, “Artificial Intelligence”, Tata McGraw Hill Education,
2013 (http://nptel.ac.in/)
5. Christopher M. Bishop, “Pattern Recognition and Machine Learning”, Springer, 2006.
6. Tom Mitchell, “Machine Learning”, McGraw Hill, 3rd Edition,1997.
7. Charu C. Aggarwal, “Data Classification Algorithms and Applications”, CRC Press, 2014
8. Mehryar Mohri, Afshin Rostamizadeh, Ameet Talwalkar, “Foundations of Machine
Learning”, MIT Press, 2012.
9. Ian Goodfellow, Yoshua Bengio, Aaron Courville, “Deep Learning”, MIT Press, 2016

3
CO-PO - Mapping - Laboratory Experiments

Exercises CO PO

1 CO1 PO1,PO2,PO3,PO4,PO9,PO1 0,PO11,PO12

2 CO1 PO1,PO2,PO3,PO4,PO9,PO1 0,PO11,PO12

3 CO2 PO1,PO2,PO3,PO4,PO5,PO9, PO10,PO11,PO12

4 CO2 PO1,PO2,PO3,PO4,PO5,PO9, PO10,PO11,PO12

5 CO3 PO1,PO2,PO3,PO4,PO5,PO9, PO10,PO11,PO12

6 CO3 PO1,PO2,PO3,PO4,PO5,PO9, PO10,PO11,PO12

7 CO3 PO1,PO2,PO3,PO4,PO5,PO9, PO10,PO11,PO12

8 CO3 PO1,PO2,PO3,PO4,PO5,PO9, PO10,PO11,PO12

9 CO4 PO1,PO2,PO3,PO4,PO9,PO1 0,PO11,PO12

10 CO4 PO1,PO2,PO3,PO4,PO9,PO1 0,PO11,PO12

11 CO5 PO1,PO2,PO3,PO4,PO5,PO9, PO10,PO11,PO12

PO1,PO2,PO3,PO4,PO5,PO9, PO10,PO11,PO12
12 CO5

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

Ex.1.1 Date:

Aim:

To implement uninformed search algorithms such as Breadth First and Depth First Search using
python.

1.1 Breadth First Search

Algorithm:

Program:

graph = {

'5' : ['3','7'],

'3' : ['2', '4'],

'7' : ['8'],

'2' : ['1','6'],

'4' : ['8'],

'8' : [],

'1':[],

'6':[]

5
}

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

OutPut:

following is the Breadth-First Search


53724816

6
Ex.1.2 Date:

Algorithm:

1.2 Depth First Search

Program:

# Using a Python dictionary to act as an adjacency list

graph = {

'5' : ['3','7'],

'3' : ['2', '4'],

'7' : ['8'],

'2' : ['1','6'],

'4' : ['8'],

'8' : [],

'1': [],

'6': []

visited = set() # Set to keep track of visited nodes of graph.

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

7
if node not in visited:

print (node)

visited.add(node)

for neighbour in graph[node]:

dfs(visited, graph, neighbour)

# Driver Code

print("Following is the Depth-First Search")

dfs(visited, graph, '5')

OutPut:

Following is the Depth-First Search


5
3
2
1
6
4
8
7

Result:
Thus the Uninformed searching algorithms such as BFS and DFS were implemented and
executed successfully.

8
2. Implementation of Informed search algorithms (A*, memory-bounded A*)

Ex.2 Date:

Aim:

To implement informed search algorithms such as A* and memory-bounded A* Search using


python.

2.1 A* Search algorithm

Algorithm:

1. Start
2. Begin with the start node in the open list and an empty closed list.
3. While there are nodes in the open list, continue iterating.
4. Choose the node with the lowest combined cost and heuristic value (f(n) = g(n) + h(n)).
5. If the current node is the goal, reconstruct and return the path.
6. Remove the current node from the open list, add it to the closed list, and expand its
neighbors.
7. Update the cost (g(n)), heuristic estimate (h(n)), and parent of each neighbor if necessary.
8. Add neighbors to the open list if they are not already present.
9. After finding the goal, reconstruct the path from the start node to the goal node.
10. Stop

9
Program:

import heapq

from collections import deque

def a_star(grid, start, goal):

def heuristic(a, b):

return abs(a[0] - b[0]) + abs(a[1] - b[1])

def reconstruct_path(came_from, current):

path = deque()

while current in came_from:

path.appendleft(current)

current = came_from[current]

return path

open_set = []

heapq.heappush(open_set, (0, start))

came_from = {}

g_score = {start: 0}

f_score = {start: heuristic(start, goal)}

while open_set:

current = heapq.heappop(open_set)[1]

if current == goal:

return reconstruct_path(came_from, current)

for neighbor in [(current[0]+1, current[1]), (current[0]-1, current[1]), (current[0],


current[1]+1), (current[0], current[1]-1)]:

10
if 0 <= neighbor[0] < len(grid) and 0 <= neighbor[1] < len(grid[0]) and not
grid[neighbor[0]][neighbor[1]]:

tentative_g_score = g_score[current] + 1

if tentative_g_score < g_score.get(neighbor, float("inf")):

came_from[neighbor] = current

g_score[neighbor] = tentative_g_score

f_score[neighbor] = tentative_g_score + heuristic(neighbor, goal)

heapq.heappush(open_set, (f_score[neighbor], neighbor))

return None

grid = [

[0, 0, 0, 0, 0],

[0, 1, 0, 1, 0],

[0, 1, 0, 1, 0],

[0, 1, 0, 1, 0],

[0, 0, 0, 0, 0]

start = (0, 0)

goal = (4, 4)

path = a_star(grid, start, goal)

print(list(path))

OutPut:

[(0, 1), (0, 2), (0, 3), (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)]

11
2.2 Memory bounded A* Search algorithm

Algorithm:

1. Start
2. Create an open list (priority queue) to store nodes to be explored.
3. Create a closed set to store explored nodes.
4. Push the start node onto the open list with a cost estimate (f_cost) calculated as the sum
of its g_cost (cost from start to current node) and h_cost (heuristic estimate from current
node to goal).
5. While the open list is not empty:
a. Pop the node with the lowest f_cost from the open list.
b. This node becomes the current node.
6. If the current node is the goal node,
a. Reconstruct the path by following the parent pointers from the goal node back to
the start node.
b. Return the path and the total cost.
7. else,
a. add the current node to the closed set.
8. Generate successor nodes by applying valid actions (neighbors) to the current node.
9. For each successor node:
a. Calculate its g_cost (cost from start to successor node) by adding the cost of
reaching the successor node from the current node.
b. Calculate its h_cost (heuristic estimate from successor node to goal).
c. Calculate its f_cost as the sum of g_cost and h_cost.
d. If the f_cost of the successor node is within the memory limit:
i. If the successor node has not been explored (not in the closed set), add it
to the open list.
e. Return:
10. If the goal is not found within the memory limit or there is no valid path,
a. return None.
11. Stop.

12
Program:
import heapq
class Node:
def __init__(self, state, g_cost, h_cost, parent=None):
self.state = state
self.g_cost = g_cost
self.h_cost = h_cost
self.f_cost = g_cost + h_cost
self.parent = parent

def __lt__(self, other):


return self.f_cost < other.f_cost

def memory_bounded_astar(start, goal, memory_limit, heuristic, neighbors_fn, cost_fn):


open_list = []
closed_set = set()
heapq.heappush(open_list, Node(start, 0, heuristic(start, goal)))

while open_list:
current_node = heapq.heappop(open_list)
closed_set.add(current_node.state)

if current_node.state == goal:
# Reconstruct path
path = []
cost = current_node.g_cost
while current_node:
path.append(current_node.state)
current_node = current_node.parent
return path[::-1], cost

for neighbor in neighbors_fn(current_node.state):


if neighbor not in closed_set:
g_cost = current_node.g_cost + cost_fn(current_node.state, neighbor)
h_cost = heuristic(neighbor, goal)
new_node = Node(neighbor, g_cost, h_cost, current_node)
if new_node.f_cost <= memory_limit:
heapq.heappush(open_list, new_node)

return None, None

13
# Example usage:
def heuristic(state, goal):
return abs(state[0] - goal[0]) + abs(state[1] - goal[1])

def neighbors(state):
x, y = state
return [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]

def cost(state, next_state):


return 1 # Uniform cost for grid movement

start = (0, 0)
goal = (4, 4)
memory_limit = 10 # Example memory limit

path, cost = memory_bounded_astar(start, goal, memory_limit, heuristic, neighbors, cost)


if path is not None:
print("Optimal path found with cost:", cost)
print("Path:", path)
else:
print("Path not found within memory limit.")

Output:

Optimal path found with cost: 8


Path: [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (2, 3), (3, 3), (4, 3), (4, 4)]

Result:

Thus the implementation of informed search algorithms such as A* and memory-bounded A*


were successfully executed using python.

14
3. Implementation of Naïve Bayes models

Ex.3 Date:

Aim:

To implement a Naïve Bayes classifier model in Python using scikit-learn

Algorithm:

Step 1: Start the program

Step 2:Import Necessary Libraries such as sci-kit learn..etc

Step 3: Define the Naive Bayes Classifier Class

Step 4: Initialize and Define Toy Example Data

Step 5: Train and Predict given data using Naive Bayes Classifier

Step 6: Calculate the accuracy of the prediction.

Step 7:Stop the program

Program:

from sklearn.model_selection import train_test_split

from sklearn.naive_bayes import GaussianNB

from sklearn.metrics import accuracy_score

# Sample data

X = [[1, 'S'], [1, 'M'], [1, 'M'], [1, 'S'], [1, 'S'],

[2, 'S'], [2, 'M'], [2, 'M'], [2, 'L'], [2, 'L'],

[3, 'L'], [3, 'M'], [3, 'M'], [3, 'L'], [3, 'L']]

15
y = ['N', 'N', 'Y', 'Y', 'N', 'N', 'N', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'N']

# Encoding categorical features

from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()

X_encoded = [[le.fit_transform([x[0]])[0], le.fit_transform([x[1]])[0]] for x in X]

# Splitting the dataset

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

# Initializing and training the Naïve Bayes classifier

model = GaussianNB()

model.fit(X_train, y_train)

# Predicting

y_pred = model.predict(X_test)

# Accuracy

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

OutPut:

Accuracy:0.3333333

Result:

Thus the implementation of a Naïve Bayes classifier model executed in Python using scikit-
learn

16
4. Implementation of Bayesian network model

Ex.3 Date:

Aim:

The aim of implementing Bayesian Networks is to model the probabilistic relationships between
a set of variables

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.

17
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.

Program:
import numpy as np
import csv

import pandas as pd

from pgmpy.models import BayesianModel

from pgmpy.estimators import MaximumLikelihoodEstimator from pgmpy.inference import


VariableElimination

#read Cleveland Heart Disease data heartDisease = pd.read_csv('heart.csv') heartDisease =


heartDisease.replace('?',np.nan)

#display the data

print('Few examples from the dataset are given below') print(heartDisease.head())

#Model Bayesian Network Model=BayesianModel([('age','trestbps'),('age','fbs'),


('sex','trestbps'),('exang','trestbps'),('trestbps','heartdise ase'),('fbs','heartdisease'),('heartdisease','restecg'),

('heartdisease','thalach'),('heartdisease','chol')])

#Learning CPDs using Maximum Likelihood Estimators


print('\n Learning CPD using Maximum likelihood
estimators')

model.fit(heartDisease,estimator=MaximumLikelihoodEstimator)

# Inferencing with Bayesian Network

18
print('\n Inferencing with Bayesian Network:')
HeartDisease_infer =
VariableElimination(model)

#computing the Probability of HeartDisease given


Age print('\n 1. Probability of HeartDisease given
Age=30')

q=HeartDisease_infer.query(variables=['heartdisease'],evidence ={'age':28})

print(q['heartdisease'])

#computing the Probability of HeartDisease given cholesterol print('\n 2. Probability of HeartDisease


given cholesterol=100')
q=HeartDisease_infer.query(variables=['heartdisease'],evidence={'chol':100})

print(q['heartdisease'])

Output:
age sex cptrestbps ...slope
cathalheartdisease 0 63 1 1 145 ...
3060

1 67 1 4 160 ... 2 3 3 2

2 67 1 4 120 ... 2 2 7 1

3 37 1 3 130 ... 3 0 3 0

4 41 0 2 130 ... 1 0 3 0

[5 rows x 14 columns]

Learning CPD using Maximum likelihood


estimators Inferencing with Bayesian
Network:

1. Probability of HeartDisease given Age=28

19
╒════════════════╤═════════════════════╕

│ heartdisease │ phi(heartdisease) │

╞════════════════╪═════════════════════╡

│ heartdisease_0 │ 0.6791 │

├────────────────┼─────────────────────┤

│ heartdisease_1 │ 0.1212 │

├────────────────┼─────────────────────┤

│ heartdisease_2 │ 0.0810 │

├────────────────┼─────────────────────┤

│ heartdisease_3 │ 0.0939 │

├────────────────┼─────────────────────┤

│ heartdisease_4 │ 0.0247 │

╘════════════════╧═════════════════════╛

2. Probability of HeartDisease given cholesterol=100

╒════════════════╤═════════════════════╕

│ heartdisease │ phi(heartdisease) │

╞════════════════╪═════════════════════╡

│ heartdisease_0 │ 0.5400 │

├────────────────┼─────────────────────┤

│ heartdisease_1 │ 0.1533 │

├────────────────┼─────────────────────┤

│ heartdisease_2 │ 0.1303 │

├────────────────┼─────────────────────┤

20
│ heartdisease_3 │ 0.1259 │

├────────────────┼─────────────────────┤

│ heartdisease_4 │ 0.0506 │

╘════════════════╧═════════════════════╛

Result:

Thus the program is executed successfully and output is verified.

21
5. Build Regression models

Aim:

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.zeros(n) delta = np.ones(n)

for iteration in range(iterations):

for i in range(n):

22
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

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

23
Output:

Result:

Thus the program to implement non-parametric Locally Weighted Regression algorithm in order
to fit data points with graph visualization have been executed successfully.

24
6. Build decision trees and random forests.

Aim:

To implement the concept of decision trees with suitable dataset from real world
problems using the CART algorithm.

Algorithm:

Steps in CART algorithm:

1. It begins with the original set S as the root node.

2. On each iteration of the algorithm, it iterates through the very unused attribute of the set S
and calculates Gini index of this attribute.

3. Gini Index works with the categorical target variable “Success” or “Failure”. It
performs only Binary splits.

4. The set S is then split by the selected attribute to produce a subset of the data.

5. The algorithm continues to recur on each subset, considering only attributes never selected
before.

Program:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

data = pd.read_csv('/Users/ganesh/PycharmProjects/DecisionTree/Social_Network_Ads.csv')
data.head()

25
feature_cols = ['Age', 'EstimatedSalary']
x = data.iloc[:, [2, 3]].values

y = data.iloc[:, 4].values

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=0)

from sklearn.preprocessing import StandardScaler


sc_x = StandardScaler()
x_train = sc_x.fit_transform(x_train)
x_test = sc_x.transform(x_test)

from sklearn.tree import DecisionTreeClassifier


classifier = DecisionTreeClassifier()
classifier = classifier.fit(x_train,

y_train) y_pred =

classifier.predict(x_test)

from sklearn import metrics

print('Accuracy Score:', metrics.accuracy_score(y_test, y_pred))

from sklearn.metrics import confusion_matrix cm = confusion_matrix(y_test, y_pred) print(cm)

from matplotlib.colors import ListedColormap x_set, y_set = x_test, y_test

x1, x2 = np.meshgrid(np.arange(start=x_set[:, 0].min()-1, stop=x_set[:, 0].max()+1,


step=0.01), np.arange(start=x_set[:, 1].min()-1, stop=x_set[:, 1].max()+1, step=0.01))
plt.contourf(x1,x2, classifier.predict(np.array([x1.ravel(), x2.ravel()]).T).reshape(x1.shape),
alpha=0.75, cmap=ListedColormap(("red", "green")))

26
plt.xlim(x1.min(), x1.max())

plt.ylim(x2.min(), x2.max())

for i, j in enumerate(np.unique(y_set)):

plt.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1], c=ListedColormap(("red", "green"))(i), label=j)

plt.title("Decision Tree(Test set)")


plt.xlabel("Age")
plt.ylabel("Estimated Salary")
plt.legend()

plt.show()

from sklearn.tree import export_graphviz


from six import StringIO
from IPython.display import Image
import pydotplus

dot_data = StringIO()

export_graphviz(classifier, out_file=dot_data, filled=True, rounded=True, special_characters=True,


feature_names=feature_cols, class_names=['0', '1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
Image(graph.write_png('decisiontree.png'))

classifier = DecisionTreeClassifier(criterion="gini", max_depth=3) classifier = classifier.fit(x_train,


y_train)

y_pred = classifier.predict(x_test)

print("Accuracy:", metrics.accuracy_score(y_test, y_pred))

dot_data = StringIO()

27
export_graphviz(classifier, out_file=dot_data, filled=True, rounded=True, special_characters=True,
feature_names=feature_cols, class_names=['0', '1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
Image(graph.write_png('opt_decisiontree_gini.png'))

Output of decision tree without pruning:

28
Optimized output of decision tree using Gini Index (CART):

Result:

Thus the program to implement the concept of decision trees with suitable dataset from real
world problems using CART algorithm have been executed successfully.

29
7. Build SVM models.

Aim:

To create a machine learning model which classifies the Spam and Ham E-Mails from
a given dataset using the Support Vector Machine algorithm.

Algorithm:

1. Import all the necessary libraries.

2. Read the given csv file which contains the emails which are both spam and
ham.

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 and f1 score and print the confusion matrix for the
classification of spam and ham.

Program:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

import string

from nltk.corpus import stopwords


import os
from wordcloud import WordCloud, STOPWORDS,
ImageColorGenerator from PIL import Image
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split

30
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import roc_curve, auc
from sklearn import metrics
from sklearn import model_selection
from sklearn import svm

from nltk import word_tokenize

from sklearn.metrics import roc_auc_score


from matplotlib import pyplot

from sklearn.metrics import plot_confusion_matrix

class
data_read_write(object):
def init (self):

pass

def init (self, file_link):


self.data_frame =
pd.read_csv(file_link)

def read_csv_file(self,
file_link): return
self.data_frame

def write_to_csvfile(self, file_link):

self.data_frame.to_csv(file_link, encoding='utf-8', index=False, header=True)


return

class generate_word_cloud(data_read_write): def init (self):

pass

def variance_column(self, data):


return np.variance(data)
def word_cloud(self, data_frame_column, output_image_file):

31
text = " ".join(review for review in data_frame_column) stopwords = set(STOPWORDS)
stopwords.update(["subject"])
wordcloud = WordCloud(width = 1200, height = 800, stopwords=stopwords, max_font_size = 50,
margin=0,background_color = "white").generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.savefig("Distribution.png")
plt.show() wordcloud.to_file(output_image_file)
return

class data_cleaning(data_read_write): def init (self):

pass

def message_cleaning(self, message):

Test_punc_removed = [char for char in message if char not in string.punctuation]


Test_punc_removed_join = ''.join(Test_punc_removed)
Test_punc_removed_join_clean = [word for word in Test_punc_removed_join.split()
if word.lower() not in stopwords.words('english')]
final_join = ' '.join(Test_punc_removed_join_clean)

return final_join

def apply_to_column(self, data_column_text):

data_processed = data_column_text.apply(self.message_cleaning)
return data_processed

class apply_embeddding_and_model(data_read_write):

def init (self):

pass

def apply_count_vector(self, v_data_column):

32
vectorizer = CountVectorizer(min_df=2, analyzer="word", tokenizer=None,
preprocessor=None, stop_words=None)

return vectorizer.fit_transform(v_data_column)

def apply_svm(self, X, y):

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)


params = {'kernel': 'linear', 'C': 2, 'gamma': 1}
svm_cv = svm.SVC(C=params['C'], kernel=params['kernel'],
gamma=params['gamma'], probability=True)
svm_cv.fit(X_train, y_train)
y_predict_test = svm_cv.predict(X_test)
cm = confusion_matrix(y_test, y_predict_test)
sns.heatmap(cm, annot=True)
print(classification_report(y_test, y_predict_test))
print("test set")

print("\nAccuracy Score: " + str(metrics.accuracy_score(y_test, y_predict_test)))


print("F1 Score: " + str(metrics.f1_score(y_test, y_predict_test)))
print("Recall: " + str(metrics.recall_score(y_test, y_predict_test)))
print("Precision: " + str(metrics.precision_score(y_test, y_predict_test)))

class_names = ['ham', 'spam']


titles_options = [("Confusion matrix, without normalization", None), ("Normalized
confusion matrix", 'true')]

for title, normalize in titles_options:

disp = plot_confusion_matrix(svm_cv, X_test, y_test,

display_labels=class_names,
cmap=plt.cm.Blues,
normalize=normalize)
disp.ax_.set_title(title)
print(title)
print(disp.confusion_matrix)
plt.savefig("SVM.png)
plt.show()

33
ns_probs = [0 for _ in range(len(y_test))]
lr_probs = svm_cv.predict_proba(X_test)
lr_probs = lr_probs[:, 1]
ns_auc = roc_auc_score(y_test, ns_probs)
lr_auc = roc_auc_score(y_test, lr_probs)
print('No Skill: ROC AUC=%.3f' %
(ns_auc)) print('SVM: ROC AUC=%.3f' %
(lr_auc))
ns_fpr, ns_tpr, _ = roc_curve(y_test, ns_probs)
lr_fpr, lr_tpr, _ = roc_curve(y_test, lr_probs)

pyplot.plot(ns_fpr, ns_tpr, linestyle='--', label='No Skill')

pyplot.plot(lr_fpr, lr_tpr, marker='.', label='SVM')


pyplot.xlabel('False Positive Rate')
pyplot.ylabel('True Positive Rate') pyplot.legend()
pyplot.savefig("SVMMat.png")
pyplot.show()

return

data_obj = data_read_write("emails.csv")

data_frame = data_obj.read_csv_file("processed.csv") data_frame.head()

data_frame.tail()
data_frame.describe()
data_frame.info()

data_frame.head()

data_frame.groupby('spam').describe()

data_frame['length'] = data_frame['text'].apply(len)
data_frame['length'].max()

sns.set(rc={'figure.figsize':(11.7,8.27)})

34
ham_messages_length = data_frame[data_frame['spam']==0]
spam_messages_length = data_frame[data_frame['spam']==1]

ham_messages_length['length'].plot(bins=100, kind='hist',label = 'Ham')


spam_messages_length['length'].plot(bins=100, kind='hist',label = 'Spam')
plt.title('Distribution of Length of Email Text')
plt.xlabel('Length of Email Text')
plt.legend()

data_frame[data_frame['spam']==0].text.values

ham_words_length = [len(word_tokenize(title))
for title in data_frame[data_frame['spam']==0].text.values]
spam_words_length = [len(word_tokenize(title))
for title in data_frame[data_frame['spam']==1].text.values]
print(max(ham_words_length))
print(max(spam_words_length))

sns.set(rc={'figure.figsize':(11.7,8.27)})

ax = sns.distplot(ham_words_length, norm_hist = True, bins = 30, label = 'Ham')


ax = sns.distplot(spam_words_length, norm_hist = True, bins = 30, label = 'Spam')
plt.title('Distribution of Number of Words')

plt.xlabel('Number of Words')

plt.legend()
plt.savefig("SVMGraph.png"
) plt.show()

def mean_word_length(x):
word_lengths = np.array([])
for word in word_tokenize(x):
word_lengths = np.append(word_lengths, len(word)) return
word_lengths.mean()

35
ham_meanword_length = data_frame[data_frame['spam']==0].text.apply(mean_word_length)
spam_meanword_length = data_frame[data_frame['spam']==1].text.apply(mean_word_length)

sns.distplot(ham_meanword_length, norm_hist = True, bins = 30, label = 'Ham')


sns.distplot(spam_meanword_length , norm_hist = True, bins = 30, label = 'Spam')
plt.title('Distribution of Mean Word Length')
plt.xlabel('Mean Word Length')
plt.legend()
plt.savefig("Graph.png")
plt.show()

from nltk.corpus import stopwords stop_words


= set(stopwords.words('english'))

def stop_words_ratio(x): num_total_words =0

num_stop_words = 0

for word in
word_tokenize(x):
if word in stop_words:

num_stop_words += 1

num_total_words += 1

return num_stop_words / num_total_words

ham_stopwords = data_frame[data_frame['spam'] == 0].text.apply(stop_words_ratio)


spam_stopwords = data_frame[data_frame['spam'] == 1].text.apply(stop_words_ratio)

sns.distplot(ham_stopwords, norm_hist=True, label='Ham')


sns.distplot(spam_stopwords, label='Spam')

36
print('Ham Mean: {:.3f}'.format(ham_stopwords.values.mean()))
print('Spam Mean: {:.3f}'.format(spam_stopwords.values.mean()))
plt.title('Distribution of Stop-word Ratio')

plt.xlabel('Stop Word Ratio')


plt.legend()

ham = data_frame[data_frame['spam']==0]
spam = data_frame[data_frame['spam']==1]
spam['length'].plot(bins=60, kind='hist')
ham['length'].plot(bins=60, kind='hist')

data_frame['Ham(0) and Spam(1)'] = data_frame['spam']

print( 'Spam percentage =', (len(spam) / len(data_frame) )*100,"%")


print( 'Ham percentage =', (len(ham) / len(data_frame) )*100,"%")
sns.countplot(data_frame['Ham(0) and Spam(1)'], label = "Count")

data_clean_obj = data_cleaning()

data_frame['clean_text'] = data_clean_obj.apply_to_column(data_frame['text'])

data_frame.head()

data_obj.data_frame.head()

data_obj.write_to_csvfile("processed_file.csv")

cv_object = apply_embeddding_and_model()

spamham_countvectorizer = cv_object.apply_count_vector(data_frame['clean_text'])

X = spamham_countvectorizer
label = data_frame['spam'].values
y = label
cv_object.apply_svm(X,y)

37
Output:

precision recall f1-score support

0 0.99 0.99 0.99 877

1 0.98 0.97 0.98 269

accuracy 0.99 1146

macro avg 0.99 0.98 0.99 1146

weighted avg 0.99 0.99 0.99 1146

test set:

Accuracy Score: 0.9895287958115183

F1 Score: 0.9776119402985075

Recall: 0.9739776951672863

Precision: 0.9812734082397003

Normalized confusion matrix:

[[0.99429875 0.00570125]

[0.0260223 0.9739777 ]]

38
39
Result:
Thus the program to create a machine learning model which classifies the Spam and Ham E-
Mails from a given dataset using Support Vector Machine algorithm have been successfully
executed.

40
8. Implement ensembling techniques.

Aim:

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 tests and meta-features.

Program:

import pandas as pd

from sklearn.metrics import mean_squared_error

from sklearn.ensemble import RandomForestRegressor


import xgboost as xgb
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
df = pd.read_csv("train_data.csv")
target = df["target"] train
= df.drop("target")
X_train, X_test, y_train, y_test = train_test_split(train, target, test_size=0.20)
train_ratio = 0.70

validation_ratio = 0.20

41
test_ratio = 0.10

x_train, x_test, y_train, y_test = train_test_split( train, target, test_size=1 -


train_ratio)

x_val, x_test, y_val, y_test = train_test_split(

x_test, y_test, test_size=test_ratio/(test_ratio + validation_ratio))


model_1 = LinearRegression()
model_2 = xgb.XGBRegressor()
model_3 = RandomForestRegressor()
model_1.fit(x_train, y_train)
val_pred_1 = model_1.predict(x_val)
test_pred_1 = model_1.predict(x_test)
val_pred_1 = pd.DataFrame(val_pred_1)
test_pred_1 = pd.DataFrame(test_pred_1)
model_2.fit(x_train, y_train)
val_pred_2 = model_2.predict(x_val)
test_pred_2 = model_2.predict(x_test)
val_pred_2 = pd.DataFrame(val_pred_2)
test_pred_2 = pd.DataFrame(test_pred_2)

model_3.fit(x_train, y_train)
val_pred_3 = model_1.predict(x_val)
test_pred_3 = model_1.predict(x_test)
val_pred_3 = pd.DataFrame(val_pred_3)
test_pred_3 = pd.DataFrame(test_pred_3)
df_val = pd.concat([x_val, val_pred_1, val_pred_2, val_pred_3], axis=1)
df_test = pd.concat([x_test, test_pred_1, test_pred_2, test_pred_3], axis=1)
final_model = LinearRegression()

final_model.fit(df_val, y_val)

final_pred = final_model.predict(df_test)
print(mean_squared_error(y_test, pred_final))

Output:

4790

42
Result:

Thus the program to implement ensembling technique of Blending with the given Alcohol QCM
Dataset has been executed successfully and the output got verified.

43
9. Implement clustering algorithms

Aim:

To implement k-Nearest Neighbour algorithm to classify the Iris Dataset.

Algorithm:

Step-1: Select the number K of the neighbors

Step-2: Calculate the Euclidean distance of K number of neighbors

Step-3: Take the K nearest neighbors as per the calculated Euclidean distance. Step-4: Among
these k neighbors, count the number of the data points in each category.

Step-5: Assign the new data points to that category for which the number of the neighbor is
maximum.

Step-6: Our model is ready.

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.load_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)

44
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 9

1 1.00 0.93 0.96 14

2 0.88 1.00 0.93 7

accuracy 0.97 30

macro avg 0.96 0.98 0.97 30

weighted avg 0.97 0.97 0.97 30

Result:

Thus the program to implement k-Nearest Neighbor Algorithm for clustering Iris dataset have
been executed successfully and output got verified.

45
10. Implement EM for Bayesian networks.

Aim:

To implement the EM algorithm for clustering networks using the given dataset.

Algorithm:

● Initialize θ randomly Repeat until convergence:


● E-step:
● Compute q(h) = P(H = h | E = e; θ) for each h (probabilistic
inference) Create fully-observed weighted examples: (h, e) with
weight q(h)
● M-step:
● 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
import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

dataset=load_iris()
# print(dataset)

46
X=pd.DataFrame(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=KMeans(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=colormap[predY],s=40
) plt.title('KMeans')

# GMM PLOT

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

47
Output:

Result:

Thus the program to implement EM Algorithm for clustering networks using the

given dataset has been executed successfully and the output got verified.

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

● Starting with the input layer, propagate data forward to the output
layer. This step is the forward propagation.
● Based on the output, calculate the error (the difference between the
predicted and known outcome). The error needs to be minimized.
● Back propagate the error. Find its derivative with respect to each
weight in the network, and update the model.

6. 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.

49
Program:

from future import print_function import numpy as np

import tensorflow as tf

from keras.models import Sequential

from keras.layers.core import Dense, Dropout, Activation


from keras.layers import Conv2D, MaxPooling2D, Flatten
from keras.optimizers import RMSprop, SGD
from keras.optimizers import Adam
from keras.utils import np_utils
from emnist import list_datasets
from emnist import extract_training_samples
from emnist import extract_test_samples
import matplotlib

matplotlib.use('TkAgg')

import matplotlib.pyplot as plt


np.random.seed(1671) # for reproducibility
# network and training

NB_EPOCH = 30

BATCH_SIZE = 256

VERBOSE = 2

NB_CLASSES = 256 # number of outputs = number of


classes OPTIMIZER = Adam()

N_HIDDEN = 512

VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for


VALIDATION DROPOUT = 0.20

print(list_datasets())

X_train, y_train = extract_training_samples('byclass')


print("train shape: ", X_train.shape)

print("train labels: ",y_train.shape)

50
X_test, y_test = extract_test_samples('byclass')
print("test shape: ",X_test.shape)
print("test labels: ",y_test.shape)
#for indexing from 0
y_train = y_train-1
y_test = y_test-1
RESHAPED = len(X_train[0])*len(X_train[1])
X_train = X_train.reshape(len(X_train),
RESHAPED) X_test = X_test.reshape(len(X_test),
RESHAPED) X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# normalize

X_train /= 255

X_test /= 255

print(X_train.shape[0], 'train samples')


print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices

Y_train = np_utils.to_categorical(y_train,
NB_CLASSES) Y_test = np_utils.to_categorical(y_test,
NB_CLASSES) # M_HIDDEN hidden layers

# 35 outputs

# final stage is softmax


model = Sequential()
model.add(Dense(N_HIDDEN,
input_shape=(RESHAPED,))) model.add(Activation('relu'))
model.add(Dropout(DROPOUT))
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(DROPOUT)
) model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(DROPOUT)
)

51
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(DROPOUT))
model.add(Dense(NB_CLASSES
))
model.add(Activation('softmax'))
model.summary()

model.compile(loss='categorical_crossentropy',
optimizer=OPTIMIZER,

metrics=['accuracy'])

history = model.fit(X_train, Y_train,


batch_size=BATCH_SIZE, epochs=NB_EPOCH,
verbose=VERBOSE,
validation_split=VALIDATION_SPLIT) score =
model.evaluate(X_test, Y_test, verbose=VERBOSE)
print("\nTest score:", score[0])
print('Test accuracy:', score[1])

# list all data in history


print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy') plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss') plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

52
Output:
['balanced', 'byclass', 'bymerge', 'digits', 'letters', 'mnist']
train shape: (697932, 28, 28)

train labels: (697932,)

test shape: (116323, 28, 28)

test labels: (116323,)

697932 train samples


116323 test samples
Model: "sequential"

Layer (type) Output Shape Param #

=================================================================

dense (Dense) (None, 512) 401920

activation (Activation) (None, 512) 0

dropout (Dropout) (None, 512) 0

dense_1 (Dense) (None, 256) 131328

activation_1 (Activation) (None, 256) 0

dropout_1 (Dropout) (None, 256) 0

dense_2 (Dense) (None, 256) 65792

activation_2 (Activation) (None, 256) 0

dropout_2 (Dropout) (None, 256) 0

dense_3 (Dense) (None, 256) 65792

activation_3 (Activation) (None, 256) 0

dropout_3 (Dropout) (None, 256) 0

dense_4 (Dense) (None, 256) 65792

53
activation_4 (Activation) (None, 256) 0

=================================================================

Total params: 730,624

Trainable params: 730,624

Non-trainable params: 0

54
Result:

Thus the program to implement the neural network model for the given dataset.

55
12. Build deep learning NN models.

Aim:

To implement and build a Convolutional neural network model which predicts the
age and gender of a person using the given pre-trained models.

Algorithm:
Steps in CNN Algorithm:

Step-1: Choose the Dataset.

Step-2: Prepare the Dataset for training.


Step-3: Create training Data.

Step-4: Shuffle the Dataset.

Step-5: Assigning Labels and Features.

Step-6: Normalising X and converting labels to categorical data.


Step-7: Split X and Y for use in CNN.
Step-8: Define, compile and train the CNN Model.
Step-9: Accuracy and Score of the model.
Program:

import cv2 as cv import math import time

from google.colab.patches import cv2_imshow

def getFaceBox(net, frame, conf_threshold=0.7): frameOpencvDnn = rame.copy()


frameHeight = frameOpencvDnn.shape[0]
frameWidth = frameOpencvDnn.shape[1]
blob = cv.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True,
False)
net.setInput(blob)
detections = net.forward()
bboxes = []

for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2] if confidence > conf_threshold:

56
x1 = int(detections[0, 0, i, 3] * frameWidth)
y1 = int(detections[0, 0, i, 4] * frameHeight)
x2 = int(detections[0, 0, i, 5] * frameWidth)
y2 = int(detections[0, 0, i, 6] * frameHeight)
bboxes.append([x1, y1, x2, y2])
cv.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (0, 255, 0),int(round(frameHeight/150)), 8)
return frameOpencvDnn, bboxes

faceProto = "/content/opencv_face_detector.pbtxt"
faceModel = "/content/opencv_face_detector_uint8.pb"
ageProto = "/content/age_deploy.prototxt"
ageModel = "/content/age_net.caffemodel" genderProto
= "/content/gender_deploy.prototxt" genderModel =
"/content/gender_net.caffemodel"

MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)

ageList = ['(0-2)', '(4-6)', '(8-12)', '(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)']

genderList = ['Male', 'Female']

ageNet = cv.dnn.readNet(ageModel, ageProto)


genderNet = cv.dnn.readNet(genderModel, genderProto)
faceNet = cv.dnn.readNet(faceModel, faceProto)

def age_gender_detector(frame):

# Read frame t = time.time()


frameFace, bboxes = getFaceBox(faceNet, frame)
for bbox in bboxes:

# print(bbox)

face = frame[max(0,bbox[1]-padding):min(bbox[3]+padding,frame.shape[0]- 1),max(0,bbox[0]-


padding):
min(bbox[2]+padding, frame.shape[1]-1)]
blob = cv.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES,
swapRB=False)
genderNet.setInput(blob)

genderPreds = genderNet.forward()

57
gender = genderList[genderPreds[0].argmax()]

# print("Gender Output : {}".format(genderPreds))


print("Gender : {}, conf = {:.3f}".format(gender, genderPreds[0].max()))
ageNet.setInput(blob)
agePreds = ageNet.forward()
age = ageList[agePreds[0].argmax()]
print("Age Output : {}".format(agePreds))
print("Age : {}, conf = {:.3f}".format(age, agePreds[0].max()))
label = "{},{}".format(gender, age)
cv.putText(frameFace, label, (bbox[0], bbox[1]-10), cv.FONT_HERSHEY_SIMPLEX,
0.8, (0, 255, 255), 2, cv.LINE_AA)

return frameFace

from google.colab import files uploaded = files.upload() input = cv.imread("2.jpg")

output = age_gender_detector(input)
cv2_imshow(output)
Output:
gender : Male, conf = 1.000

Age Output : [[2.8247703e-05 8.9249297e-05 3.0017464e-04 8.8183772e-03 9.3055397e-01


5.1735926e-02 7.6946630e-03 7.7927281e-04]]

Age: (25-32), conf = 0.873.

Result:

Thus the program to implement and build a Convolutional neural network model which
predicts the age and gender of a person using the given pre-trained models has been
executed successfully and the output got verified.

58

You might also like