0% found this document useful (0 votes)
93 views

AI-ML LAB Manual Format

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

AI-ML LAB Manual Format

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

LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 1 of 34

P.S.R.R COLLEGE OF ENGINEERING

LABORATORY ACTIVITY MANUAL

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


CS3491/ ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING LABORATORY
YEAR :II SEMESTER: IV

ABSTRACT: Artificial intelligence (AI) is the theory and development of computer systems
capable of performing tasks that historically required human intelligence, such as recognizing speech,
making decisions, and identifying patterns. AI is an umbrella term that encompasses a wide variety of
technologies, including machine learning, deep learning, and natural language processing
(NLP). The term is commonly used to describe a range of different technologies in use today, many
disagree on whether these actually constitute artificial intelligence. Instead, some argue that much of
the technology used in the real world today actually constitutes highly advanced machine learning
that is simply a first step towards true artificial intelligence, or “general artificial intelligence” (GAI).

LIST OF EXPERIMENTS

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 2 of 34

Ex.
No. Programs
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.a IMPLEMENTATION OF UNINFORMED SEARCH ALGORITHMS – BREADTH


FIRST SEARCH

Aim:

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 3 of 34

To implement Breadth first search using Python Programming.

Algorithm

Step 1: Start
Step 2: Initialize Graph
Step 3: Create list for visited node
Step 4: Create list to initialize queue
Step 5: Perform Breadth First Search
Step 6: print the order of node visited
Step 7: Stop

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

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 4 of 34

Output

Following is the Breadth-First Search


537248

Result

Thus the Breadth First Program is implemented and executed successfully using
Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 5 of 34

1.b IMPLEMENTATION OF UNINFORMED SEARCH ALGORITHMS –


DEPTH FIRST SEARCH

Aim:

To implement Depth first search using Python Programming

Algorithm

Step 1: Start
Step 2: Initialize Graph
Step 3: Create list for visited node
Step 4: Create list to initialize queue
Step 5: Perform Depth First Search
Step 6: print the order of node visited
Step 7: Stop

Source Code

graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
visited = set() # Set to keep track of visited nodes of graph.
def dfs(visited, graph, node): #function for dfs
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')

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 6 of 34

Output

Following is the Depth-First Search


5
3
2
4
8
7

Result

Thus the Depth First Program is implemented and executed successfully using
Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 7 of 34

2.a IMPLEMENTATION OF INFORMED SEARCH


ALGORITHM - A* SEARCH
Aim:

To implement A* search using Python Programming.

Algorithm

Step 1: Start
Step 2: Initialize heap
Step 3: Initialize heuristic value for all node
Step 4: Perform A* Search
Step 5: print the destination node heuristic value
Step 6: Stop

Source Code
import heapq
def astar(graph, heuristic, start, goal):
visited = set()
heap = [(0, start)]
while heap:
(cost, node) = heapq.heappop(heap)
if node in visited:
continue
visited.add(node)
if node == goal:
return cost
for neighbor, distance in graph[node].items():
if neighbor not in visited:
heapq.heappush(heap, (cost + distance + heuristic[neighbor], neighbor))
return -1
graph = {
'A': {'B': 2, 'C': 3},
'B': {'D': 4},
'C': {'D': 2},
'D': {'E': 3},
'E': {}
}

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 8 of 34

heuristic = {
'A': 7,
'B': 6,
'C': 4,
'D': 2,
'E': 0
}
print("A*:")
print(astar(graph, heuristic, 'A', 'E'))

Output

A*:
14

Result

Thus the A* search is implemented and executed successfully using Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 9 of 34

2.b IMPLEMENTATION OF INFORMED SEARCH ALGORITHM –


MEMORY BOUNDED A* SEARCH

Aim:

To implement Memory Bounded A* search using Python Programming.

Algorithm

Step 1: Start
Step 2: Import priority queue
Step 3: Initialize graph and heuristic value for all node
Step 4: Initialize weights for all paths
Step 5: Perform Memory Bounded heuristic search
Step 6: print the result
Step 7: Stop
Source Code
from queue import PriorityQueue
import sys
graph = {
'A': {'B': 2, 'C': 3},
'B': {'D': 4},
'C': {'D': 2},
'D': {'E': 3},
'E': {}
}
heuristic = {
'A': 7,
'B': 6,
'C': 4,
'D': 2,
'E': 0
}

class Node:
def init (self, state, parent=None, action=None, path_cost=0, heuristic_cost=0):

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 10 of 34

self.state = state
self.parent = parent
self.action = action
self.path_cost = path_cost
self.heuristic_cost = heuristic_cost
self.f_cost = path_cost + heuristic_cost
def lt (self, other):
return self.f_cost < other.f_cost
def memory_bounded_a_star_search(problem, memory_limit=sys.maxsize):
start_node = Node(problem.initial_state)
if problem.goal_test(start_node.state):
return start_node.state
frontier = PriorityQueue()
frontier.put(start_node)
explored = {}
while frontier:
node = frontier.get()
state = node.state
if problem.goal_test(state):
return state
if state in explored and explored[state].f_cost <= node.f_cost:
continue
explored[state] = node
if node.f_cost > memory_limit:
continue
for action in problem.actions(state):
child_state = problem.result(state, action)
child_path_cost = node.path_cost + problem.step_cost(state, action, child_state)
child_heuristic_cost = problem.heuristic(child_state)
child_node = Node(child_state, node, action, child_path_cost, child_heuristic_cost)
if child_state not in explored or child_node.f_cost < explored[child_state].f_cost:
frontier.put(child_node)
return None

Output

Memory Bounded A*:


34

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 11 of 34

Result

Thus the Memory Bounded A* search is implemented and executed successfully


using Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 12 of 34

3 IMPLEMENT NAÏVE BAYES MODELS

IMPLEMENT NAÏVE BAYES MODELS TO CLASSIFY THE LABELS IN


IRIS DATASET
Aim:
To implement Naive Bayes to accurately classify the label based on given features
using Python Programming.

Algorithm

Step 1: Start
Step 2: Import sklearn
Step 3: Import iris dataset from sklearn datasets
Step 4: Import all required packages to perform naive bias classification in machine learning
Step 5: Display the result
Step 6: Stop

Source Code
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
# Load the iris dataset
iris = load_iris()
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2,
random_state=0)
# Train a Gaussian Naive Bayes classifier
classifier = GaussianNB()
classifier.fit(X_train, y_train)
# Predict the test set labels
y_pred = classifier.predict(X_test)
# Calculate the accuracy score
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

Output

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 13 of 34

Result

Thus the Naive Bias model for iris dataset is implemented and executed successfully
using Python Program

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 14 of 34

4 IMPLEMENT BAYESIAN NETWORK

IMPLEMENT BAYESIAN NETWORKS TO PREDICT PATIENT PROBABILITY RATE


TO HAVING HEART DISEASE

Aim:

To implement Bayesian Network to find the probability rate of patient having heart
disease using Python Programming.

Algorithm

Step 1: Start
Step 2: Import sklearn
Step 3: Import heardisease dataset as csv
Step 4: Import all required packages to perform Bayesian Network in machine learning
Step 5: Display the result
Step 6: Stop

Source Code – Import packages


import numpy as
np import pandas
as pd import csv
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.models import BayesianModel
from pgmpy.inference import VariableElimination
Source Code – Loading dataset
from google.colab import drive
drive.mount('/content/drive')
heartDisease = pd.read_csv("/content/drive/My Drive/Bayesiannetworkdataset.csv")
heartDisease.head()

Source Code – Filling sparse values


heartDisease = heartDisease.replace('?',np.nan)
print('\n Attributes and datatypes')

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 15 of 34

print(heartDisease.dtypes)
Source Code – Fitting model in Bayesian Network
model= BayesianModel([('age','heartdisease'),('gender','heartdisease'),('exang','heartdisease'),
('cp','hear tdisease'),('heartdisease','restecg'),('heartdisease','chol')])
print('\nLearning CPD using Maximum likelihood estimators')
model.fit(heartDisease,estimator=MaximumLikelihoodEstimator)
Source Code – Reduce redundant variables print('\
n Inferencing with Bayesian Network:')
HeartDiseasetest_infer = VariableElimination(model)
Source Code – Predicting Probability value of heartdisease for evidence restecg
print('\n 1. Probability of HeartDisease given evidence= restecg')
q1=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'restecg':1})
print(q1)
Output

Source Code – Predicting Probability value of heartdisease for evidence


cp print('\n 2. Probability of HeartDisease given evidence= cp ')
q2=HeartDiseasetest_infer.query(variables=['heartdisease'],evidence={'cp':2})
print(q2)

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 16 of 34

Result

Thus the Bayesian model for heart disease dataset is implemented and executed
successfully using Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 17 of 34

5 BUILD REGRESSION MODELS

5. a TO IMPLEMENT SINGLE LINEAR REGRESSION

Aim:

To implement Single Linear Regression using Python Programming

Algorithm

Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform Linear regression in machine learning
Step 5: Display the result
Step 6: Stop

Source Code – Import packages

import numpy as np
from sklearn.linear_model import LinearRegression
Source code - Data and Reshape of x and display of y value
x = np.array([2, 15, 25, 35, 45, 55]).reshape((-1, 1))
y = np.array([5, 20, 14, 32, 22, 38])
x
y

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 18 of 34

Source code - Create Model

model = LinearRegression()
model.fit(x, y)

Source code - Get results


r_sq = model.score(x, y)
print(f"coefficient of determination: {r_sq}")
print(f"intercept: {model.intercept_}")

print(f"slope: {model.coef_}")

Source code - Predict Response


y_pred = model.predict(x)
print(f"predicted response:\n{y_pred}")

y_pred = model.intercept_ + model.coef_ * x


print(f"predicted response:\n{y_pred}")

x_new = np.arange(5).reshape((-1, 1))


x_new

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 19 of 34

y_new = model.predict(x_new)
y_new

Result
Thus the Single Linear Regression for is implemented and executed successfully
using Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 20 of 34

5 .b TO IMPLEMENT MULTIPLE LINEAR REGRESSIONS TO PREDICT


UNEMPLOYMENT RATE
Aim:

To implement Multiple Linear Regression to predict Unemployment Rate based on


given features using Python Programming.

Algorithm

Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform Multiple Linear regression in
machine
learning
Step 5: Display the result
Step 6: Stop
Source Code - import
pandas as pd data = {'year':
[2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2016,2016,2016,2016,20
16,2016,2016,2016,2016,2016,2016,2016],
'month': [12,11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1],
'interest_rate':
[2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,
1.75,1.75],
'unemployment_rate':
[5.3,5.3,5.3,5.3,5.4,5.6,5.5,5.5,5.5,5.6,5.7,5.9,6,5.9,5.8,6.1,6.2,6.1,6.1,6.1,5.9,6.2,6.2,6.1],
'index_price':
[1464,1394,1357,1293,1256,1254,1234,1195,1159,1167,1130,1075,1047,965,943,958,971,94
9,884,866,876,822,704,719]
}
df = pd.DataFrame(data)
print(df)

Output:

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 21 of 34

Source Code – Index price vs Unemployment rate

import pandas as pd
import matplotlib.pyplot as plt
data = {'year':
[2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2016,2016,2016,2016,20
16,2016,2016,2016,2016,2016,2016,2016],
'month': [12,11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1],
'interest_rate':
[2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,
1.75,1.75],
'unemployment_rate':
[5.3,5.3,5.3,5.3,5.4,5.6,5.5,5.5,5.5,5.6,5.7,5.9,6,5.9,5.8,6.1,6.2,6.1,6.1,6.1,5.9,6.2,6.2,6.1],
'index_price':
[1464,1394,1357,1293,1256,1254,1234,1195,1159,1167,1130,1075,1047,965,943,958,971,94
9,884,866,876,822,704,719]
}
df = pd.DataFrame(data)
plt.scatter(df['interest_rate'], df['index_price'], color='red')
plt.title('Index Price Vs Interest Rate', fontsize=14)
plt.xlabel('Interest Rate', fontsize=14)
plt.ylabel('Index Price', fontsize=14)
plt.grid(True)
plt.show()
Output

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 22 of 34

Source Code – Index price vs Unemployment rate


import pandas as pd
import matplotlib.pyplot as plt
data = {'year':
[2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2016,2016,2016,2016,20
16,2016,2016,2016,2016,2016,2016,2016],
'month': [12,11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1],
'interest_rate':
[2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,
1.75,1.75],
'unemployment_rate':
[5.3,5.3,5.3,5.3,5.4,5.6,5.5,5.5,5.5,5.6,5.7,5.9,6,5.9,5.8,6.1,6.2,6.1,6.1,6.1,5.9,6.2,6.2,6.1],
'index_price':
[1464,1394,1357,1293,1256,1254,1234,1195,1159,1167,1130,1075,1047,965,943,958,971,94
9,884,866,876,822,704,719]
}

df = pd.DataFrame(data)
plt.scatter(df['unemployment_rate'], df['index_price'],
color='green') plt.title('Index Price Vs Unemployment Rate',
fontsize=14) plt.xlabel('Unemployment Rate', fontsize=14)
plt.ylabel('Index Price', fontsize=14)
plt.grid(True)
plt.show()

Output

Source Code – Display Statistical Measure

import statsmodels.api as sm

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 23 of 34

data = {'year':
[2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2016,2016,2016,2016,20
16,2016,2016,2016,2016,2016,2016,2016],
'month': [12,11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1],
'interest_rate':
[2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,
1.75,1.75],
'unemployment_rate':
[2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,
1.75,1.75],
'unemployment_rate':
[5.3,5.3,5.3,5.3,5.4,5.6,5.5,5.5,5.5,5.6,5.7,5.9,6,5.9,5.8,6.1,6.2,6.1,6.1,6.1,5.9,6.2,6.2,6.1],
'index_price':
[1464,1394,1357,1293,1256,1254,1234,1195,1159,1167,1130,1075,1047,965,943,958,971,94
9,884,866,876,822,704,719]
}

df = pd.DataFrame(data)

x = df[['interest_rate','unemployment_rate']]
y = df['index_price']

# with sklearn
regr = linear_model.LinearRegression() regr.fit(x, y)

print('Intercept: \n', regr.intercept_)


print('Coefficients: \n', regr.coef_)
# with statsmodels
x = sm.add_constant(x) # adding a constant
model = sm.OLS(y, x).fit()
predictions = model.predict(x)
print_model = model.summary()
print(print_model)
Output

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 24 of 34

Result

Thus the Multiple Linear Regression to predict unemployment rate is implemented


and executed successfully using Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 25 of 34

5 .c TO IMPLEMENT LOGISTIC REGRESSIONS TO PREDICT PREDICT DIGIT


VALUES FROM

Aim:
To implement Logistic Regression library to predict digit values from images using
Python Programming

Algorithm

Step 1: Start
Step 2: Import sklearn
Step 3: Import image dataset
Step 4: Import all required packages to perform Logistic regression in machine learning
Step 5: Display the result
Step 6: Stop

Source Code – Import packages


import re

Importing libraries
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as ply
import seaborn as sns
from sklearn import metrics
%matplotlib inline
digits = load_digits()

Determining the total number of images and labels


print("Image Data Shape", digits.data.shape)
print("Label Data Shape", digits.target.shape)
Displaying some of the images and their labels
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(20, 4))
for index, (image, label) in enumerate(zip(digits.data[0:5], digits.target[0:5])):
plt.subplot(1, 5, index+1)

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 26 of 34

plt.imshow(np.reshape(image, (8, 8)), cmap=plt.cm.gray)


plt.title("Training : %i\n" %label, fontsize=20)

Dividing dataset into “training” and “test” set


from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.23,
random_state=2)

print(x_train.shape)

print(y_train.shape)

print(x_test.shape)

print(y_test.shape)
(414)

Importing the logistic regression model


from sklearn.linear_model import LogisticRegression
Making an instance of the model and training
logisticRegr = LogisticRegression()
logisticRegr.fit(x_train, y_train)

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 27 of 34

Predicting the output of the first element of the test set


print(logisticRegr.predict(x_test[0].reshape(1, -1)))

Predicting the output of the first 10 elements of the test set


logisticRegr.predict(x_test[0:10])

Prediction for the entire dataset


predictions = logisticRegr.predict(x_test)

Determining the accuracy of the model


score=logisticRegr.score(x_test, y_test)
print(score)

Presenting predictions and actual output


index=0
misclassifiedIndex=[]
for predict, actual in zip(predictions, y_test):
if predict==actual:
misclassifiedIndex.append(index)
index+=1
plt.figure(figsize=(20,3))
for plotIndex, wrong in enumerate(misclassifiedIndex[0:4]):
plt.subplot(1, 4, plotIndex +1)
plt.imshow(np.reshape(x_test[wrong], (8,8)), cmap=plt.cm.gray)

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 28 of 34

plt.title("predicted: {}, Actual: {}" .format(predictions[wrong], y_test[wrong]), fontsize=20)

Result

Thus the Logistic Regression to predict digit values from images is implemented
and executed successfully using Python Program

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 29 of 34

6. TO IMPLEMENT DECISION TREE AND RANDOM FOREST


a) TO IMPLEMENT DECISION TREE TO PREDICT DIABETES
Aim:

To implement Decision Tree to predict diabetes based on given features using


Python Programming

Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform Decision tree in machine learning
Step 5: Display the result
Step 6: Stop

Source Code - Importing Required Libraries


Let's first load the required libraries.
import pandas as pd
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.model_selection import train_test_split # Import train_test_split function
from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation

Loading Data
Pima Indian Diabetes dataset
col_names = ['pregnant', 'glucose', 'bp', 'skin', 'insulin', 'bmi', 'pedigree', 'age', 'label']
# load dataset
pima = pd.read_csv("diabetes.csv", header=None, names=col_names)

pima.head()
Feature Selection
#split dataset in features and target variable

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 30 of 34

feature_cols = ['pregnant', 'insulin', 'bmi', 'age','glucose','bp','pedigree']


X = pima[feature_cols] # Features
y = pima.label # Target variable

Splitting Data
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) # 70%
training and 30% test
Building Decision Tree Model
Let's create a decision tree model using Scikit-learn.
# Create Decision Tree classifer object
clf = DecisionTreeClassifier()
# Train Decision Tree Classifer
clf = clf.fit(X_train,y_train)
#Predict the response for test dataset
y_pred = clf.predict(X_test)

Evaluating the Model


# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

Visualizing Decision Trees


pip install graphviz
pip install pydotplus
The export_graphviz function converts the decision tree classifier into a dot file, and
pydotplus converts this dot file to png or displayable
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
from IPython.display import Image
import pydotplus
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,
filled=True, rounded=True,

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 31 of 34

special_characters=True,feature_names = feature_cols,class_names=['0','1']) graph =


pydotplus.graph_from_dot_data(dot_data.getvalue()) graph.write_png('diabetes.png')
Image(graph.create_png())

Example

Decision Tree

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 32 of 34

# Create Decision Tree classifer object


clf = DecisionTreeClassifier(criterion="entropy", max_depth=3)
# Train Decision Tree Classifer
clf = clf.fit(X_train,y_train)
#Predict the response for test dataset
y_pred = clf.predict(X_test)
# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
Accuracy: 0.7705627705627706
Visualizing Decision Trees
from six import StringIO from IPython.display import Image
from sklearn.tree import export_graphviz
import pydotplus
dot_data = StringIO()
export_graphviz(clf, 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()) graph.write_png('diabetes.png')
Image(graph.create_png())

Result

Thus the Decision Tree to predict Diabetes from the given feature is implemented
and executed successfully using Python Program

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 33 of 34

6.b TO IMPLEMENT RANDOM FOREST TO PREDICT HEART DISEASE

Aim:

To implement Random Forest to predict heart disease using Random Forest

Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform Random Forest in machine learning
Step 5: Display the result
Step 6: Stop

Source Code – Import Libraries


import pandas as pd, numpy as np
import matplotlib.pyplot as plt, seaborn as sns
%matplotlib inline
Source Code – Import Dataset
df=pd.read_csv("https://raw.githubusercontent.com/soumya-mishra/Heart-
Disease_DT/main/heart_v2.csv")
df.head()
df.to_csv("heart_v2.csv")

Source Code – Putting Feature Variable to X and Target variable to y


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

Source Code – Train-Test-Split


X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=42)
X_train.shape, X_test.shape

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 34 of 34

pip install -U scikit-learn

Source Code – Import RandomForestClassifier and fit the data

from sklearn.ensemble import RandomForestClassifier


classifier_rf = RandomForestClassifier(random_state=42, n_jobs=-1, max_depth=5,
n_estimators=100, oob_score=True)
classifier_rf.fit(X_train, y_train)
classifier_rf.oob_score_

Source Code – Hyperparameter tuning for Random Forest using GridSearchCV and fit
the data
rf = RandomForestClassifier(random_state=42, n_jobs=-1)
params = {
'max_depth': [2,3,5,10,20],
'min_samples_leaf': [5,10,20,50,100,200],
'n_estimators': [10,25,30,50,100,200]
}
from sklearn.model_selection import GridSearchCV
# Instantiate the grid search model
grid_search = GridSearchCV(estimator=rf,
param_grid=params,
cv = 4,
n_jobs=-1, verbose=1, scoring="accuracy")
grid_search.fit(X_train, y_train)

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 35 of 34

grid_search.best_score_

rf_best = grid_search.best_estimator_
rf_best

from sklearn.tree import plot_tree


plt.figure(figsize=(80,40))
plot_tree(rf_best.estimators_[5], feature_names = X.columns,class_names=['Disease',
"No Disease"],filled=True);

from sklearn.tree import plot_tree


plt.figure(figsize=(80,40))
plot_tree(rf_best.estimators_[5], feature_names = X.columns,class_names=['Disease', "No
Disease"],filled=True);

Source Code – Feature importance

rf_best.feature_importances_
imp_df = pd.DataFrame({
"Varname": X_train.columns,
"Imp":
rf_best.feature_importances_
})

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 36 of 34

imp_df.sort_values(by="Imp", ascending=False)

Result

Thus the Random Forest to predict heart disease from the given feature in dataset is
implemented and executed successfully using Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 37 of 34

7. TO IMPLEMENT SVM MODEL


TO PREDICT PRODUCT IS PURCHASED BY CUSTOMER OR
NOT BASED ON GIVEN FEATURES
Aim:

To implement SVM model to predict customer is purchase the product or not based on
given features using python programming.

Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform Support Vector Machine in machine learning
Step 5: Import Grid Search to find optimal Hyper parameters for SVM
Step 6: Display the result
Step 7: Stop

Source code - Install Packages


% pip install sklearn
% pip install pandas
% pip install seaborn
% pip install matplotlib
% pip install numpy

Source Code – Import Libraries


# importing the libraries
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

Source Code – Import the data set and divide it into input and output variables
from google.colab import drive
drive.mount('/content/drive')
df = pd.read_csv("/content/drive/My Drive/customer_purchases.csv")
df.head()

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 38 of 34

Source Code

# split the data into inputs and outputs


X = dataset.iloc[:, [0,1]].values
y = dataset.iloc[:, 2].values

print out the target/output class to verify that our data is a binary set
# printing the target values

print(dataset.Purchased)
pip install chart_studio

Source Code - importing the required modules for data visualization


import matplotlib.pyplot as plt
import chart_studio.plotly as py
import plotly.graph_objects as go
import plotly.offline as pyoff
import pandas as pd
# importing the dats set
df = pd.read_csv("/content/drive/My Drive/customer_purchases.csv")
# counting the total output data from purchased column
target_balance = df['Purchased'].value_counts().reset_index()
# dividing the output classes into two sections
target_class = go.Bar(
name = 'Target Balance',
x = ['Not-Purchased', 'Purchased'],
y = target_balance['Purchased']
)
# ploting the output classes
fig = go.Figure(target_class)

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 39 of 34

pyoff.iplot(fig)

Source Code – Training And Testing Linear SVM Model


# training and testing data
from sklearn.model_selection import train_test_split
# assign test data size 25%
X_train, X_test, y_train, y_test =train_test_split(X, y, test_size=0.25, random_state=0)
Source Code - importing StandardScaler
from sklearn.preprocessing import StandardScaler
# scalling the input data
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.fit_transform(X_test)
Source Code - importing SVM module
from sklearn.svm import SVC
# kernel to be set linear as it is binary class
classifier = SVC(kernel='linear')
# traininf the model
classifier.fit(X_train, y_train)
Testing the model
# testing the model
y_pred = classifier.predict(X_test)
# importing accuracy score
from sklearn.metrics import accuracy_score
# printing the accuracy of the model
print(accuracy_score(y_test, y_pred))

Source Code –Visualize Training Data

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 40 of 34

import matplotlib.pyplot as plt


from matplotlib.colors import ListedColormap
# plotting the fgiure
plt.figure(figsize = (7,7))
# assigning the input values
X_set, y_set = X_train, y_train
# ploting the linear graph
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(('black', 'white')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
# ploting scattered graph for the values
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', 'blue'))(i),
label = j)
# labeling the graph
plt.title('Purchased Vs Non-Purchased')
plt.xlabel('Salay')
plt.ylabel('Age')
plt.legend()
plt.show()

Source Code - ploting graph of size 7,7


plt.figure(figsize = (7,7))
# assigning the testing dataset X_set, y_set = X_test, y_test # ploting the predicted
graphX1, 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))

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 41 of 34

plt.contour f(X1,X2,classifier.predict(np.array([X1.ravel(),
X2.ravel()]).T).reshape(X1.shape),alpha = 0.75, cmap = ListedColormap(('black', 'white')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
# plorting scattred graph for the testing values
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', 'blue'))(i),
label = j)
# labelling the graph
plt.title('Purchased vs Not-purchased Predictions')
plt.xlabel('Salary')
plt.ylabel('Age')
plt.legend()
plt.show()

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 42 of 34

Evaluation of SVM Algorithm Performance For Binary Classification


Source Code - importing the required modules
import seaborn as sns
from sklearn.metrics import confusion_matrix
# passing actual and predicted values
cm = confusion_matrix(y_test, y_pred, labels=classifier.classes_)
# true Write data values in each cell of the matrix
sns.heatmap(cm, annot=True)
plt.savefig('confusion.png')

Source Code - importing classification report


from sklearn.metrics import classification_report
# printing the report
print(classification_report(y_test, y_pred))

Result

Thus the Support Vector Machine to predict customer purchase the product or not from the
given feature in dataset is implemented and executed successfully using Python
Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 43 of 34

8 IMPLEMENT ENSEMBLING TECHNIQUES - ADABOOSTING

IMPLEMENT ENSEMBLING TECHNIQUES - ADABOOSTING TO CLASSIFY


THE FLOWER SPECIES
Aim:

To implement Ada Boost Ensembling model to predict classify flower species based on
given dataset using Python Programming

Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform AdaBoost Ensembling model in machine
learning
Step 5: Display the result
Step 6: Stop

Source code - Install Packages


import pandas as pd
import numpy as np
from sklearn import datasets
# loading the dataset
iris = datasets.load_iris()
Source code - converting the dataset into pandas dataframe
data = pd.DataFrame(iris.data, columns=iris.feature_names)
# head
data.head()
iris.target
Source code - creating variables
specie1=0
specie2 = 0
specie3 = 0
labels=["specie 1", 'specie 2', 'specie3']

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 44 of 34

# for loop to count the outputs


for i in iris.target:
if i ==0:
specie1+=1
elif i ==1:
specie2+=1
else:
specie3+=1
Source code - importing required module
import matplotlib.pyplot as plt
fig = plt.figure()
# Creating plot
fig = plt.figure(figsize =(10, 7))
plt.pie([specie1, specie2, specie3], labels = labels)
# show plot
plt.show()

# input and output


Input, output = datasets.load_iris(return_X_y=True)
Source code - importing the module
from sklearn.model_selection import train_test_split
# splitting the dataset
X_train, X_test, y_train, y_test = train_test_split(Input, output, test_size=0.25)
Source code - Import the AdaBoost classifier
from sklearn.ensemble import AdaBoostClassifier
# Create adaboost classifer with 1 stump trees
Ada_classifier = AdaBoostClassifier(n_estimators=1)
# Train Adaboost Classifer
AdaBoost = Ada_classifier.fit(X_train, y_train)

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 45 of 34

#Predict the response for test dataset


AdaBoost_pred = AdaBoost.predict(X_test)
Source code - importing the module
from sklearn.metrics import
accuracy_score # printing
print("The accuracy of the model is: ", accuracy_score(y_test, AdaBoost_pred))

Source code - Create adaboost classifer with 20 stump trees


Ada_classifier = AdaBoostClassifier(n_estimators=20)
# Train Adaboost Classifer
AdaBoost = Ada_classifier.fit(X_train, y_train)
#Predict the response for test dataset
AdaBoost_pred = AdaBoost.predict(X_test)
# printing
print("The accuracy of the model is: ", accuracy_score(y_test, AdaBoost_pred))

Result

Thus the AdaBoost to predict the class of the flower from the given feature in dataset
is implemented and executed successfully using Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 46 of 34

9 IMPLEMENT CLUSTERING ALGORITHMS


IMPLEMENT K-MEANS CLUSTERING TO CLUSTER SIMILAR FLOWER
SPECIES
Aim:

To implement K-means Clustering to predict species of the flowers based on given


features using Python Programming

Algorithm
Step 1: Start
Step 2: Import sklearn
Step 3: Initialize features and labels
Step 4: Import all required packages to perform K-means clustering
Step 5: Display the result
Step 6: Stop

Source code - Install Packages


K-Means Clustering
Implement K-Means Clustering using Scikit-Learn for IRIS dataset, which contains
information about different species of flowers.
Source Code
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
data = {
'x': [25, 34, 22, 27, 33, 33, 31, 22, 35, 34, 67, 54, 57, 43, 50, 57, 59, 52, 65, 47, 49, 48, 35,
33, 44, 45, 38,
43, 51, 46],
'y': [79, 51, 53, 78, 59, 74, 73, 57, 69, 75, 51, 32, 40, 47, 53, 36, 35, 58, 59, 50, 25, 20, 14,
12, 20, 5, 29, 27,8, 7]
}
df = pd.DataFrame(data)
kmeans = KMeans(n_clusters=3).fit(df)
centroids = kmeans.cluster_centers_
print(centroids)
plt.scatter(df['x'], df['y'], c=kmeans.labels_.astype(float), s=50, alpha=0.5)
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
plt.show()

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 47 of 34

Source Code - To find optimal value of K using Elbow Method

from sklearn.datasets import load_iris


import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
# Load IRIS Dataset
iris = load_iris()
X = iris.data
# Create the WCSS Plot against no. of clusters
wcss = []
for i in range(1, 11):
kmeans = KMeans(n_clusters=i, init='k-means++', max_iter=300, n_init=10,
random_state=0)
kmeans.fit(X)
wcss.append(kmeans.inertia_)lt.plot(range(1, 11), wcss)
plt.title('Elbow Method')
plt.xlabel('Number of Clusters')
plt.ylabel('WCSS')
plt.show()

Source Code - Train K-Means Clusters

kmeans = KMeans(n_clusters=3, init='k-means++', max_iter=300, n_init=10,


random_state=0)

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 48 of 34

y_kmeans = kmeans.fit_predict(X)
# Create the visualization plot of the clusters
plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 100, c = 'red', label = 'Cluster 1')
plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], s = 100, c = 'blue', label = 'Cluster 2')
plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], s = 100, c = 'green', label = 'Cluster 3')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s = 100, c = 'black',
label = 'Centroids')
plt.title('IRIS Clusters')
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.legend()
plt.show()

Result

Thus the K-means Cluster to predict the species of the flower from the given feature in
dataset is implemented and executed successfully using Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 49 of 34

10 IMPLEMENT EM FOR BAYESIAN NETWORKS


IMPLEMENT EM FOR BAYESIAN NETWORKS
Aim:

To implement Expectation Maximization for Bayesian Network using Python Programming

Algorithm
Step 1: Start
Step 2: Initialize features and labels
Step 3: Assign weight for features
Step 4: Import all required packages to perform Neural Computation
Step 5: Display the result
Step 6: Stop

Source code - Install Packages


Expectation Maximization
from .em import *
import
numpy as np

from pomegranate import *


import itertools
from .mb import *
def em_bayesnet(model, data, ind_h, max_iter = 50, criteria = 0.005):
"""Returns the data array with the hidden node filled in.
(model is not modified.)

model : a Bayesian Network object


an already baked Bayesian Network object with initialized parameters
data : an ndarray
each column is the data for the node in the same order as the nodes in the model

the hidden node should be a column of NaNs


ind_h : int
index of the hidden node
max_iter : int
Returns

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 50 of 34

data : an ndarray
the same data arary with the hidden node column filled in
"""
hidden_params = np.array(mb.prob_table[mb.hidden].values())

change = abs(hidden_params - previous_params)

convergence = max(change) < criteria

previous_params = np.array(mb.prob_table[mb.hidden].values())
i += 1
if i == max_iter:
print 'Maximum iterations reached.'
#-----fill in the hidden node data by sampling the distribution
labels = {}
for key, prob in expected_counts.counts.items():
try:
labels[key[1:]].append((key[0], prob))
except:
labels[key[1:]] = [(key[0], prob)]
for key, counts in ct.table.items():

Markov Blanket Model

def
search_hidden(
data):

Parameters

data : An ndarray (n_sample,


n_nodes)

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 51 of 34

Returns

ind_h : the index of the hidden node column

"""

is_col_nan = np.all(np.isnan(data), axis=0)

ind = np.where(is_col_nan)

if np.size(ind)==1:

ind_h = ind[0][0]

else:

raise ValueError('Data contains more than one hidden nodes or no


hidden node')

return ind_h

class MarkovBlanket():

"""

An object for storing info on nodes within the markov blanket of the
hidden node
Parameters

ind_h : int
index of the hidden node within the model
Attributes

hidden : int
index of the hidden node
parents : list of int
a list of indices of the parent nodes
children : list of int

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 52 of 34

a list of indices of the children nodes


coparents : list of int
a list of indices of the coparent nodes

prob_table : dict
a dict of probabilities table of nodes within the Markov blanket
"""
def init (self, ind_h):
self.hidden = ind_h
self.parents = []
self.children = []
self.coparents = []
self.prob_table = {}
def populate(self, model):
"""populate the parents, children, and coparents nodes
"""
state_indices = {state.name : i for i, state in enumerate(model.states)}
edges_list = [(parent.name, child.name) for parent, child in
model.edges]

edges_list = [(state_indices[parent],state_indices[child])
for parent, child in edges_list]

self.children = list(set([child for parent, child in


edges_list if parent==self.hidden]))

self.parents = list(set([parent for parent, child in edges_list if


child==self.hidden]))
self.coparents = list(set([parent for parent, child in edges_list if
child in self.children]))
try:
self.coparents.remove(self.hidden)
except ValueError:
pass
def calculate_prob(self, model):
"""Create the probability table from nodes
"""

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 53 of 34

for ind_state in [self.hidden]+self.children:


distribution = model.states[ind_state].distribution
if is instance(distribution, ConditionalProbabilityTable):
table = distribution.parameters[0]
self.prob_table[ind_state] = {
tuple(row[:-1]) : row[-1] for row in table}
else:
self.prob_table[ind_state] = distribution.parameters[0]
def update_prob(self, model, expected_counts, ct):
"""Update the probability table using expected counts
"""
ind = {x : i for i, x in enumerate([self.hidden] + self.parents
+ self.children + self.coparents)}
mb_keys = expected_counts.counts.keys()
for ind_state in [self.hidden] + self.children:
distribution = model.states[ind_state].distribution
if isinstance(distribution, ConditionalProbabilityTable):

idxs = distribution.column_idxs
table = self.prob_table[ind_state] # dict

# calculate the new parameter for this key


num = 0
denom = 0

# marginal counts
for mb_key in mb_keys:
# marginal counts of node + parents
num +=
ct.table[mb_key[1:]]*expected_counts.counts[mb_key]

# marginal counts of parents


if tuple([mb_key[ind[x]] for x in idxs[:-1]]) == key[:-1]:
denom +=
PREPARED BY ct.table[mb_key[1:]]*expected_counts.counts[mb_key]
REVIEWED BY APPROVED BY
try:
AP/CSE HOD/CSE PRINCIPAL
prob = num/denom
except ZeroDivisionError:
prob = 0
LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 54 of 34

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


else: # DiscreteProb
table = self.prob_table[ind_state] # dict
# calculate the new parameter for this key
for key in table.keys():
LABORATORY
prob = 0 ACTIVITY MANUAL
for mb_key in mb_keys:
P.s.r.r college of engineering
if mb_key[ind[ind_state]] == key:
"""
PREAMBLE
prob+=ct.table[mb_key[1:]]*expected_counts.counts[mb_k
Doc No Issue No ey] Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01# update20-03-2024
the parameter
Parameters Page: 55 of 34
table[key] = prob
class ExpectedCounts():
"""Calculate the expected counts using the model parameters
Parameters
model : BayesianNetwork object
model : a BayesianNetwork object

mb : a MarkovBlanket
mb : object
MarkovBlanket object

counts : dict items : ndarray


a dict of expected countscolumns
for nodes
areindata
the Markov blanket
for parents,
""" children, coparents
def init (self, model, mb):
self.counts[key] = 0
class """= {}
self.counts
CountTable():
"""Counting the={}
self.populate(model,
self.table data"""
mb)
def init (self,
def populate(self, model,
model, mb):mb, items):
self.ind = {} of keys
#create combinations
for ind in mb.parents + mb.children + mb.coparents:
keys_list.append(model.states[ind].distribution.keys())
self.populate(model, mb, items)
self.counts = {p:0 for p in itertools.product(*keys_list)}
def update(self, model, mb):
def populate(self, model, mb, items):
ind = {x keys_list
: i for i, x=in[]enumerate([mb.hidden] + mb.parents +
mb.children+ mb.coparents)}
marginal_prob = {}
for ind in mb.parents + mb.children +
# calculate joint probability and marginal probability
mb.coparents:
for i, key in enumerate(self.counts.keys()):
keys_list.append(model.states[ind].distribution.k
prob = 1
eys())
for j, ind_state in enumerate([mb.hidden] + mb.children):
distribution
# init = model.states[ind_state].distribution
if isinstance(distribution, ConditionalProbabilityTable):
self.table
idxs = {p:0 for p in
= distribution.column_idxs
itertools.product(*keys_list)}
state_key = tuple([key[ind[x]] for x in idxs])
else:self.ind = {p:[] for p in
itertools.product(*keys_list)}
PREPARED BY state_keyREVIEWED
= key[ind[ind_state]]
BY APPROVED BY
prob = prob*mb.prob_table[ind_state][state_key]
AP/CSE # countHOD/CSE PRINCIPAL
self.counts[key] = prob
try: for i, row in enumerate(items):
marginal_prob[key[1:]] += prob
try:
except KeyError:
LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 56 of 34

self.table[tuple(row)] += 1
self.ind[tuple(row)].append(i)

except KeyError:

print 'Items in row', i, 'does not match the set of


keys.'
raise KeyError

Bayesian Network

import numpy as np
from pomegranate import *
data = np.array([[np.nan, 'yellow', 'sweet', 'long'],
[np.nan, 'green', 'sour', 'round'],
[np.nan, 'green', 'sour', 'round'],
[np.nan, 'yellow', 'sweet', 'long'],
[np.nan, 'yellow', 'sweet', 'long'],
[np.nan, 'green', 'sour', 'round'],
[np.nan, 'green', 'sweet', 'long'],
[np.nan, 'green', 'sweet', 'round']])

Fruit = DiscreteDistribution({'banana':0.4, 'apple':0.6})


Color = ConditionalProbabilityTable([['banana', 'yellow', 0.6],

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 57 of 34

['banana', 'green', 0.4],


['apple', 'yellow', 0.6],
['apple', 'green', 0.4]], [Fruit] )
Taste = ConditionalProbabilityTable([['banana', 'sweet', 0.6],
['banana', 'sour', 0.4],
['apple', 'sweet', 0.4],
['apple', 'sour', 0.6]], [Fruit])
Shape = ConditionalProbabilityTable([['banana', 'long', 0.6],
['banana', 'round', 0.4],
['apple', 'long', 0.4],
['apple', 'round', 0.6]], [Fruit])

Create the state (node) objects and BayesianNetwork object.

s_fruit = State(Fruit, 'fruit')


s_color = State(Color, 'color')
s_taste = State(Taste, 'taste')
s_shape = State(Shape, 'shape')
model = BayesianNetwork('fruit')

Add states and edges to the network

model.add_states(s_fruit, s_color, s_taste, s_shape)


model.add_transition(s_fruit, s_color)
model.add_transition(s_fruit, s_taste)
model.add_transition(s_fruit, s_shape)
model.bake()

from bayesnet_em import *


hidden_node_index = 0
new_data = em_bayesnet(model, data, hidden_node_index)
new_data

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 58 of 34

new_model = model.fit(new_data) new_model.predict_proba({'fruit':'banana'})


[1].parameters

new_model.probability(['apple', 'green', 'sweet', 'round'])

Result
Thus the EM for Bayesian Network is implemented and executed successfully using Python
Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 59 of 34

11 BUILD SIMPLE NN MODELS

Aim:

To build simple Neuron Network using Python Programming

Algorithm
Step 1: Start
Step 2: Initialize features and labels
Step 3: Assign weight for features
Step 4: Import all required packages to perform Neural Computation
Step 5: Display the result
Step 6: Stop

Source code - Install Packages


# importing dependancies
import numpy as np
# The activation function
def activation(x):
return 1 / (1 + np.exp(-x))
# A 2 x 1 matrix of randomly generated weights in the range -1 to 1
weights = np.random.uniform(-1,1,size = (2, 1))
# The training set divided into input and output. Notice that
# we are trying to train our neural network to predict the output
# of the logical OR.
training_inputs = np.array([[0, 0, 1, 1, 0, 1]]).reshape(3, 2)
training_outputs = np.array([[0, 1, 1]]).reshape(3,1)
for i in range(15000):
# forward pass
dot_product = np.dot(training_inputs, weights)
output = activation(dot_product)
# backward pass.
temp2 = -(training_outputs - output) * output * (1 - output)
adj = np.dot(training_inputs.transpose(), temp2)
# 0.5 is the learning rate.
weights = weights - 0.5 * adj

# The testing set

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 60 of 34

test_input = np.array([1, 0])


test_output = activation(np.dot(test_input, weights))
# OR of 1, 0 is 1
print(test_output)

Output

Result

Thus the simple Neural Network is implemented and executed successfully using
Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 61 of 34

12 BUILD DEEP LEARNING NN MODELS

BUILD DEEP LEARNING NN MODELS TO PREDICT DIABETES


Aim:

To build deep Neuron Network to predict diabetes using Python Programming

Algorithm
Step 1: Start
Step 2: Import keras
Step 3: Initialize features and labels
Step 4: Initialize weights, bias values, optimizers, and hidden layers
Step 4: Import all required packages to perform Neural computation
Step 5: Display the result
Step 6: Stop

Source code - Import Packages and load dataset


import pandas as pd
from google.colab import drive
drive.mount('/content/drive')
data = pd.read_csv("/content/drive/My Drive/diabetes.csv")
data.head()
x = data.drop("Outcome", axis=1)
y = data["Outcome"]

Define Keras Model


from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(12, input_dim=8, activation="relu"))
model.add(Dense(12, activation="relu"))
model.add(Dense(1, activation="sigmoid"))

Compile The Keras Model


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

Start Training (Fit the Model)


model.fit(x,y, epochs=150, batch_size=10)

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL


LABORATORY ACTIVITY MANUAL

P.s.r.r college of engineering


PREAMBLE
Doc No Issue No Issue Date Rev No Rev Date Section No:1.0
PSRR/CSPY/01 01 20-03-2024 Page: 62 of 34

Evaluate the Model


_, accuracy = model.evaluate(x, y)
print("Model accuracy: %.2f"% (accuracy*100))

Making Predictions
predictions = model.predict(x)
print([round(x[0]) for x in predictions])

Result

Thus the deep Neural Network to predict diabetes based on given features is
implemented and executed successfully using Python Programming.

PREPARED BY REVIEWED BY APPROVED BY

AP/CSE HOD/CSE PRINCIPAL

You might also like