0% found this document useful (0 votes)
3 views51 pages

Machine Learning With Python Lab 14102024

Uploaded by

amitamishra.cse
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)
3 views51 pages

Machine Learning With Python Lab 14102024

Uploaded by

amitamishra.cse
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

MACHINE LEARNING WITH PYTHON LAB (LC-CSE-412G)

LABORATORY MANUAL
[Link]. Semester- VIII
MACHINE LEARNING WITH PYTHON LAB
Subject code: LC-CSE-412G

Prepared by: Checked by: Approved by:


Prof. Sukrati Chaturvedi Dr. Ashima Mehta Name : Prof. (Dr.) Isha Malhotra

Sign.: ……………………. Sign.: …………………. Sign.: ………………….


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

TABLE OF CONTENTS

1. Vision and Mission of the Institute

2. Vision and Mission of the Department

3. Programme Educational Objectives (PEOs)

4. Programme Outcomes (POs)

5. Programme Specific Outcomes (PSOs)

6. University Syllabus

7. Course Outcomes (COs)

8. CO-PO and CO-PSO Mapping

9. Course Overview

10. List of Experiments

11. DOs and DON’Ts

12. General Safety Precautions

13. Guidelines for students for report preparation

14. Lab assessment criteria

15. Details of Conducted Experiments

16. Lab Experiments


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

VISION AND MISSION OF THE INSTITUTE

Vision:

“Empowering human values and advanced technical education to navigate and address global

challenges with excellence.”

Mission:

 M1: Seamlessly integrate human values with advanced technical education.

 M2: Supporting the cultivation of a new generation of innovators who are not only

skilled but also ethically responsible.

 M3: Inspire global citizens who are equipped to create positive and sustainable impact, driving
progress towards a more inclusive and harmonious world.
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

VISION AND MISSION OF THE DEPARTMENT

Vision:

“Steering the future of computer science through innovative advancements, fostering

ethical values and principles through technical education.”

Mission:

M1: Directing future innovations in computer science through revolutionary progress.

M2: Instilling a foundation of ethical values and principles in every technologist.

M3: Offering a comprehensive technical education to equip individuals for a meaningful

and influential future.


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

PROGRAMME EDUCATIONAL OBJECTIVES (PEOS)

PEO1: Apply the technical competence in Computer Science and Engineering for solving

problems in the real world.

PEO2: Carry out research and develop solutions on problems of social applications.

PEO3: Work in a corporate environment, demonstrating team skills, work morals, flexibility

and lifelong learning.


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

PROGRAMME OUTCOMES (POs)

PO1: Engineering knowledge: Apply the knowledge of mathematics, science, engineering

fundamentals, and an engineering specialization to the solution of complex engineering

problems.

PO2: Problem analysis: Identify, formulate, review research literature, and analyse complex

engineering problems reaching substantiated conclusions using first principles of

mathematics, natural sciences, and engineering sciences.

PO3: Design/development of solutions: Design solutions for complex engineering problems

and design system components or processes that meet the specified needs with appropriate

consideration for the public health and safety, and the cultural, societal, and environmental

considerations.

PO4: Conduct investigations of complex problems: Use research-based knowledge and

research methods including design of experiments, analysis and interpretation of data, and

synthesis of the information to provide valid conclusions.

PO5: Modern tool usage: Create, select, and apply appropriate techniques, resources, and

modern engineering and IT tools including prediction and modelling to complex engineering

activities with an understanding of the limitations.

PO6: The engineer and society: Apply reasoning informed by the contextual knowledge to

assess societal, health, safety, legal and cultural issues and the consequent responsibilities

relevant to the professional engineering practice.

PO7: Environment and sustainability: Understand the impact of the professional engineering

solutions in societal and environmental contexts, and demonstrate the knowledge of, and need

for sustainable development.


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

PO8: Ethics: Apply ethical principles and commit to professional ethics and responsibilities

and norms of the engineering practice.

PO9: Individual and team work: Function effectively as an individual, and as a member or

leader in diverse teams, and in multidisciplinary settings.

PO10: Communication: Communicate effectively on complex engineering activities with the

engineering community and with society at large, such as, being able to comprehend and

write effective reports and design documentation, make effective presentations, and give and

receive clear instructions.

PO11: Project management and finance: Demonstrate knowledge and understanding of the

engineering and management principles and apply these to one’s own work, as a member and

leader in a team, to manage projects and in multidisciplinary environments.

PO12: Life-long learning: Recognize the need for, and have the preparation and ability to

engage in independent and life-long learning in the broadest context of technological change.
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

PROGRAM SPECIFIC OUTCOMES (PSOs)

PSO1: Exhibit design and programming skills to develop and mechanize business solutions

using revolutionary technologies.

PSO2: Learn strong theoretical foundation leading to brilliance and enthusiasm towards

research, to provide well-designed solutions to complicated problems.

PSO3: Work effectively with diverse Engineering fields as a team to design, build and

develop system applications.


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

UNIVERSITY SYLLABUS

1. The probability that it is Friday and that a student is absent is 3 %. Since there are 5 school
days in a week, the probability that it is Friday is 20 %. What is theprobability that a student
is absent given that today is Friday?
Apply Baye’s rule in python to get the result. (Ans: 15%)

2. Extract the data from database using python

3. Implement k-nearest neighbours classification using python

4. Implement linear regression using python

[Link] K-Means_Clustering using python

6. Implement Naive Bayes Theorem to Classify the English Text using python

7. Implement an algorithm to demonstrate the significance of Genetic Algorithm in python

8. Implement an algorithm to demonstrate Back Propagation Algorithm in python

9. Implementing FIND-S algorithm using python

10. Implementing Candidate Elimination algorithm using python


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

COURSE OUTCOMES (COs)

Upon successful completion of the course, the students will:

1. To learn the basic concepts of machine learning and types of machine learning.

2. To design and analyze various machine learning algorithms and techniques with

a modern outlook focusing on recent advances.

3. Explore supervised and unsupervised learning paradigms of machine learning.

CO-PO Mapping:

PSO1 PSO2 PSO3 PSO4 PSO5 PSO6 PSO7 PSO8 PSO9 PSO10 PSO11 PSO12
C412.1 3 3 2 2 2 1 - 1 - 1 1 3
C412.2 3 3 3 2 3 1 - 1 - 1 1 3
C412.3 3 3 2 1 2 1 - 1 - 1 1 3

CO-PSO Mapping:

PSO1 PSO2 PSO3


C412.1 3 2 1
C412.2 3 3 2
C412.3 3 2 3
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

COURSE OVERVIEW

Machine Learning (ML) is basically that field of computer science with the help of which

computer systems can provide sense to data in much the same way as human beings do. In

simple words, ML is a type of artificial intelligence that extract patterns out of raw data by

using an algorithm or method. The key focus of ML is to allow computer systems to learn

from experience without being explicitly programmed or human intervention.


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

LIST OF EXPERIMENTS MAPPED WITH COs

[Link] Experiment Course Outcome Page No.

1 The probability that it is Friday and that a student is C412.1 1


absent is 3 %. Since there are 5 school days in a
week, the probability that it is Friday is 20 %. What
is theprobability that a student is absent given that
today is Friday?
Apply Baye’s rule in python to get the result. (Ans:
15%)
2 Extract the data from database using python C412.1 2

3 Implement k-nearest neighbours classification using C412.1, C412.2 5


python
4 Implement linear regression using python C412.1, C412.2 9

5 Implement K-Means_Clustering using python C412.1 11

6 Implement Naive Bayes Theorem to Classify the C412.3 13


English Text using python
7 Implement an algorithm to demonstrate the C412.1, C412.2 17
significance of Genetic Algorithm in python
8 Implement an algorithm to demonstrate Back C412.1 22
Propagation Algorithm in python
9 Implementing FIND-S algorithm using python C412.3 26

10 Implementing Candidate Elimination algorithm C412.3 28


using python
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

DOs and DON’Ts

DOs

1. Login-on with your username and password.

2. Log off the Computer every time when you leave the Lab.

3. Arrange your chair properly when you are leaving the lab.

4. Put your bags in the designated area.

5. Ask permission to print.

DON’Ts

1. Do not share your username and password.

2. Do not remove or disconnect cables or hardware parts.

3. Do not personalize the computer setting.

4. Do not run programs that continue to execute after you log off.

5. Do not download or install any programs, games or music on computer in Lab.

6. Personal Internet use chat room for Instant Messaging (IM) and Sites is strictly prohibited.

7. No Internet gaming activities allowed.

8. Tea, Coffee, Water & Eatables are not allowed in the Computer Lab.
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

GENERAL SAFETY PRECAUTIONS

Precautions (In case of Injury or Electric Shock)

1. To break the victim with live electric source, use an insulator such as fire wood or plastic

to break the contact. Do not touch the victim with bare hands to avoid the risk of electrifying

yourself.

2. Unplug the risk of faulty equipment. If main circuit breaker is accessible, turn the circuit

off.

3. If the victim is unconscious, start resuscitation immediately, use your hands to press the

chest in and out to continue breathing function. Use mouth-to-mouth resuscitation if

necessary.

4. Immediately call medical emergency and security. Remember! Time is critical; be best.

Precautions (In case of Fire)

1. Turn the equipment off. If power switch is not immediately accessible, take plug off.

2. If fire continues, try to curb the fire, if possible, by using the fire extinguisher or by

covering it with a heavy cloth if possible isolate the burning equipment from the other

surrounding equipment.

3. Sound the fire alarm by activating the nearest alarm switch located in the hallway.

4. Call security and emergency department immediately:

Emergency : 200 (Reception)

Security : 248 (Gate No.1)


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

GUIDELINES TO STUDENTS FOR REPORT PREPARATION

All students are required to maintain a record of the experiments conducted by them.

Guidelines for its preparation are as follows:-

1) All files must contain a title page followed by an index page. The files will not be signed

by the faculty without an entry in the index page.

2) Student’s Name, Roll number and date of conduction of experiment must be written on all

pages.

3) For each experiment, the record must contain the following

(i) Aim/Objective of the experiment

(ii) Pre-experiment work (as given by the faculty)

(iii) Lab assignment questions and their solutions

(iv) Test Cases (if applicable to the course)

(v) Results/ output

Note:

1. Students must bring their lab record along with them whenever they come for the lab.

2. Students must ensure that their lab record is regularly evaluated.


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

LAB ASSESSMENT CRITERIA

An estimated 10 lab classes are conducted in a semester for each lab course. These lab classes

are assessed continuously. Each lab experiment is evaluated based on 5 assessment criteria as

shown in following table. Assessed performance in each experiment is used to compute CO

attainment as well as internal marks in the lab course.

Grading Exemplary (4) Competent (3) Needs Poor (1)

Criteria Improvement (2)

AC1: Complete Underlined Not able to write Underlined

Pre-Lab written procedure with concept is written concept and concept is not

work (this may underlined but procedure is procedure clearly

be assessed concept is incomplete understood

through viva) properly written

AC2: Assigned problem Assigned problem Assigned problem Assigned

Program is properly is properly is properly problem is

Writing/ analyzed, correct analyzed, correct analyzed & properly

Modeling solution designed, solution designed, correct solution analyzed

appropriate appropriate designed

language language

constructs/ tools constructs/ tools

are applied, are applied

Program/solution

written is readable

AC3: Able to identify Able to identify Is dependent Unable to


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

Identification & errors/ bugs and errors/ bugs and totally on understand the

Removal of remove them remove them with someone for reason for errors/

errors/ bugs little bit of identification of bugs even after

guidance errors/ bugs and they are

their removal explicitly pointed

out

AC4:Execution All variants of All variants of Only few variants Solution is not

& Demonstration input /output are input /output are of input /output well

tested, Solution is not tested, are tested, demonstrated

well demonstrated However, solution Solution is well and implemented

and implemented is well demonstrated but concept is not

concept is clearly demonstrated and implemented clearly explained

explained implemented concept is not

concept is clearly clearly explained

explained

AC5:Lab Record All assigned More than 70 % of Less than 70 % of Less than 40 %

Assessment problems are well the assigned the assigned of the assigned

recorded with problems are well problems are well problems are

objective, design recorded with recorded with well recorded

constructs and objective, design objective, design with objective,

solution along contracts and contracts and design contracts

with solution along solution along and solution

Performance with with along with

analysis using all Performance Performance Performance

variants of input analysis is done analysis is done analysis is done


Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

and output with all variants of with all variants of with all variants

input and output input and output of input and

output
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

LAB EXPERIMENTS
LAB EXPERIMENT 1
OBJECTIVE:

The probability that it is Friday and that a student is absent is 3 %. Since there are 5 school
days in a week, the
probability that it is Friday is 20 %. What is theprobability that a student is absent given that
today is Friday?
Apply Baye’s rule in python to get the result. (Ans: 15%)

ALGORITHM:
Step 1: Calculate probability for each word in a text and filter the words which have a
probability less than threshold
probability. Words with probability less than threshold probability are irrelevant.
Step 2: Then for each word in the dictionary, create a probability of that word being in
insincere questions and its
probability insincere questions. Then finding the conditional probability to use in naive Bayes
classifier.
Step 3: Prediction using conditional probabilities.
Step 4: End.

PROGRAM:
} PFIA=float(input(“Enter probability that it is Friday and that a student is absent=”))
PF=float(input(“ probability that it is Friday=”))
PABF=PFIA / PF
print(“probability that a student is absent given that today is Friday using conditional
probabilities=”,PABF)

OUTPUT:
Enter probability that it is Friday and that a student is absent= 0.03
probability that it is Friday= 0.2
probability that a student is absent given that today is Friday using conditional probabilities=

0.15

1
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

LAB EXPERIMENT 2

OBJECTIVE:

Extract the data from database using python

ALGORITHM:
Step 1: Connect to MySQL from Python
Step 2: Define a SQL SELECT Query
Step 3: Get Cursor Object from Connection
Step 4: Execute the SELECT query using execute() method
Step 5: Extract all rows from a result
Step 6: Iterate each row
Step 7: Close the cursor object and database connection object
Step 8: End.

PROCEDURE
CREATING A DATABASE IN MYSQL AS FOLLOWS:
CREATE DATABASE myDB;
SHOW DATABASES;
USE myDB
CREATE TABLE MyGuests (id INT, name VARCHAR(20), email VARCHAR(20));
SHOW TABLES;
INSERT INTO MyGuests (id,name,email) VALUES(1,"sairam","xyz@[Link]");
… SELECT *
FROM authors;
We need to install mysql-connector to connect Python with MySQL. You can use the below
command to
install this in your system.
pip install mysql-connector-python-rf

PYTHON SOURCE CODE:


import [Link]
mydb = [Link](
host="localhost",
user="root",
password="",
database="myDB"

)
mycursor = [Link]()
[Link]("SELECT * FROM MyGuests")
myresult = [Link]()
for x in myresult:
print(x)

2
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

OUTPUT:

Extracting data from Excel sheet using Python


Step1: First convert dataset present in excel to CSV file using online resources, then execute
following
program:
consider dataset excel consists of 14 input columns and 3 output columns (C1, C2, C3)as
follows:
Python Souce Code:
import pandas as pd
dataset=pd.read_csv("Mul_Label_Dataset.csv", delimiter=',')
print(dataset) #Print entire dataset
X=

3
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

dataset[['Send','call','DC','IFMSCV','MSCV','BA','MBZ','TxO','RS','CA','AL','IFWL','WWL','
FWL']].values
Y = dataset[['C1','C2','C3']].values
print(Y) #Prints output values
print(X) #Prints intput values
X1 = dataset[['Send','call','DC','IFMSCV','MSCV']].values
print(X1) #Prints first 5 columns of intput values
print(X[0:5]) # Prints only first 5 rows of input values
OUTPUT SCREENS:
Excel Format: CSV

Format:

4
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

LAB EXPERIMENT 3

OBJECTIVE:

Implement k-nearest neighbours classification using python

ALGORITHM:
Step 1: Load the data
Step 2: Initialize the value of k
Step 3: For getting the predicted class, iterate from 1 to total number of training data points
i) Calculate the distance between test data and each row of training data. Here we will use
Euclidean
distance as our distance metric since it’s the most popular method. The other metrics that can
be
used are Chebyshev, cosine, etc.
ii) Sort the calculated distances in ascending order based on distance values 3. Get top k rows
from the
sorted array
iii) Get the most frequent class of these rows i.e. Get the labels of the selected K entries
iv) Return the predicted class If regression, return the mean of the K labels If
classification, return
the mode of the K labels
If regression, return the mean of the K labels
If classification, return the mode of the K labels
Step 4: End.
PROGRAM
import numpy as np
from sklearn import datasets
iris = datasets.load_iris()
data = [Link]
labels = [Link]
for i in [0, 79, 99, 101]:
print(f"index: {i:3}, features: {data[i]}, label: {labels[i]}")
[Link](42)
indices = [Link](len(data))
n_training_samples = 12
learn_data = data[indices[:-n_training_samples]]
learn_labels = labels[indices[:-n_training_samples]]

test_data = data[indices[-n_training_samples:]]
test_labels = labels[indices[-n_training_samples:]]
print("The first samples of our learn set:")
print(f"{'index':7s}{'data':20s}{'label':3s}")
for i in range(5):
print(f"{i:4d} {learn_data[i]} {learn_labels[i]:3}")
print("The first samples of our test set:")
print(f"{'index':7s}{'data':20s}{'label':3s}")
for i in range(5):
print(f"{i:4d} {learn_data[i]} {learn_labels[i]:3}")
5
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

#The following code is only necessary to visualize the data of our learnset
import [Link] as plt
from mpl_toolkits.mplot3d import Axes3D
colours = ("r", "b")
X = []
for iclass in range(3):
[Link]([[], [], []])
for i in range(len(learn_data)):
if learn_labels[i] == iclass:
X[iclass][0].append(learn_data[i][0])
X[iclass][1].append(learn_data[i][1])
X[iclass][2].append(sum(learn_data[i][2:]))

colours = ("r", "g", "y")


fig = [Link]()
ax = fig.add_subplot(111, projection='3d')
for iclass in range(3):
[Link](X[iclass][0], X[iclass][1], X[iclass][2], c=colours[iclass])
[Link]()
#
def distance(instance1, instance2):
""" Calculates the Eucledian distance between two instances"""
return [Link]([Link](instance1, instance2))
def get_neighbors(training_set, labels, test_instance, k, distance):
"""
get_neighors calculates a list of the k nearest neighbors of an instance 'test_instance'.
The function returns a list of k 3-tuples. Each 3-tuples consists of (index, dist, label)
"""
distances = []
for index in range(len(training_set)):
dist = distance(test_instance, training_set[index])
[Link]((training_set[index], dist, labels[index]))
[Link](key=lambda x: x[1])
neighbors = distances[:k]
return neighbors

for i in range(5):
neighbors = get_neighbors(learn_data, learn_labels, test_data[i], 3, distance=distance)
print("Index: ",i,'\n',
"Testset Data: ",test_data[i],'\n',
"Testset Label: ",test_labels[i],'\n',
"Neighbors: ",neighbors,'\n')

6
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

OUTPUT:

7
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

8
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

LAB EXPERIMENT 4

OBJECTIVE:

Implement linear regression using python

ALGORITHM:
Step 1: Create Database for Linear Regression
Step 2:Finding Hypothesis of Linear Regression
Step 3:Training a Linear Regression model
Step 4:Evaluating the model
Step 5: Scikit-learn implementation
Step 6: End
PROGRAM:
Write a program that implement Queue (its operations)using
# Importing Necessary Libraries
import numpy as np
import [Link] as plt
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score
# generate random data-set
[Link](0)
x = [Link](100, 1) #Generate a 2-D array with 100 rows, each row containing 1
random numbers:
y = 2 + 3 * x + [Link](100, 1)
regression_model = LinearRegression() # Model initialization
regression_model.fit(x, y) # Fit the data(train the model)
y_predicted = regression_model.predict(x) # Predict
# model evaluation
rmse = mean_squared_error(y, y_predicted)
r2 = r2_score(y, y_predicted)
# printing values
print('Slope:' ,regression_model.coef_)
print('Intercept:', regression_model.intercept_)

print('Root mean squared error: ', rmse)


print('R2 score: ', r2)
# plotting values # data points
[Link](x, y, s=10)
[Link]('x-Values from 0-1')
[Link]('y-values from 2-5')
# predicted values
[Link](x, y_predicted, color='r')
[Link]() )

9
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

OUTPUT:

LAB EXPERIMENT 5
10
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

OBJECTIVE:

Implement K-Means_Clustering using python

ALGORITHM:
Step 1: Read the Given data Sample to X
Step 2: Train Dataset with K=5
Step 3: Find optimal number of clusters(k) in a dataset using Elbow method
Step 4: Train Dataset with K=3 (optimal K-Value)
Step 4: Compare results
Step 6: End
PROGRAM:
#Import libraries
import numpy as np
import pandas as pd
import [Link] as plt
from [Link] import KMeans
from sklearn import datasets
#Read DataSet
df = datasets.load_iris()
x = [Link]
y = [Link]
print(x)
print(y)
#Lets try with k=5 initially
kmeans5 = KMeans(n_clusters=5)
y_kmeans5 = kmeans5.fit_predict(x)
print(y_kmeans5)
print(kmeans5.cluster_centers_)
# To find optimal number of clusters(k) in a dataset
Error =[ ]
for i in range(1, 11):
kmeans = KMeans(n_clusters = i).fit(x)
[Link](x)
[Link](kmeans.inertia_)
import [Link] as plt
[Link](range(1, 11), Error)

[Link]('Elbow method')
[Link]('No of clusters')
[Link]('Error')
[Link]()
#Now try with k=3 finally
kmeans3 = KMeans(n_clusters=3)
y_kmeans3 = kmeans3.fit_predict(x)
print(y_kmeans3)
print(kmeans3.cluster_centers_)

11
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

OUTPUT:

LAB EXPERIMENT 6

12
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

OBJECTIVE:

Implement Naive Bayes Theorem to Classify the English Text using python

The Naive Bayes algorithm

Bayes’ Theorem

feature matrix
response/target vector
Feature matrix

dependent features d X = (x1,x2,x2, xd).


Response/target vector class/group variable each
row of feature
matrix.
Now the “naïve” conditional independence assumptions come into play: assume that all
features
in X are mutually independent, conditional on the category y:
Dealing with text data

13
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

The values 0,1,2, encode the frequency of a word that appeared in the initial text data.

E.g [0 1 1 1 0 0 1 0 1] unique vocabulary [‘and’,


‘document’,
‘first’, ‘is’, ‘one’, ‘second’, ‘the’, ‘third’, ‘this’],

make_pipeline

using the transformed data

Source Code

14
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

15
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

OUTPUT:

16
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

LAB EXPERIMENT 7

OBJECTIVE:

Implement an algorithm to demonstrate the significance of Genetic Algorithm in python

ALGORITHM:
1. Individual in population compete for resources and mate
2. Those individuals who are successful (fittest) then mate to create more offspring than
others
3. Genes from “fittest” parent propagate throughout the generation, that is sometimes parents
create
offspring which is better than either parent.
4. Thus each successive generation is more suited for their environment.

Operators of Genetic Algorithms


Once the initial generation is created, the algorithm evolve the generation using following
operators –
1) Selection Operator: The idea is to give preference to the individuals with good fitness
scores and allow
them to pass there genes to the successive generations.
2) Crossover Operator: This represents mating between individuals. Two individuals are
selected using
selection operator and crossover sites are chosen randomly. Then the genes at these crossover
sites are
exchanged thus creating a completely new individual (offspring).
3) Mutation Operator: The key idea is to insert random genes in offspring to maintain the
diversity in
population to avoid the premature convergence.

17
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

Given a target string, the goal is to produce target string starting from a random string of the
same length. In
the following implementation, following analogies are made –
Characters A-Z, a-z, 0-9 and other special symbols are considered as genes
A string generated by these character is considered as chromosome/solution/Individual
Fitness score is the number of characters which differ from characters in target string at a
particular index. So
individual having lower fitness value is given more preference.
Source Code
# Python3 program to create target string, starting from
# random string using Genetic Algorithm
import random
# Number of individuals in each generation
POPULATION_SIZE = 100
# Valid genes
GENES = '''abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP
QRSTUVWXYZ 1234567890, .-;:_!"#%&/()=?@${[]}'''
# Target string to be generated
TARGET = "I love GeeksforGeeks"
class Individual(object):
'''
Class representing individual in population '''
def init (self, chromosome):
[Link] = chromosome
[Link] = self.cal_fitness()
@classmethod
def mutated_genes(self):

18
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

'''
create random genes for mutation
'''
global GENES
gene = [Link](GENES)
return gene
@classmethod
def create_gnome(self):
'''
create chromosome or string of genes
'''
global TARGET

gnome_len = len(TARGET)
return [self.mutated_genes() for _ in range(gnome_len)]
def mate(self, par2):
''' Perform mating and produce new offspring '''
# chromosome for offspring
child_chromosome = []
for gp1, gp2 in zip([Link], [Link]):
# random probability
prob = [Link]()
# if prob is less than 0.45, insert gene
# from parent 1
if prob < 0.45:
child_chromosome.append(gp1)
# if prob is between 0.45 and 0.90, insert
# gene from parent 2
elif prob < 0.90:
child_chromosome.append(gp2)
# otherwise insert random gene(mutate),
# for maintaining diversity
else:
child_chromosome.append(self.mutated_genes())
# create new Individual(offspring) using
# generated chromosome for offspring
return Individual(child_chromosome)
def cal_fitness(self):
''' Calculate fittness score, it is the number of
characters in string which differ from target string. '''
global TARGET
fitness = 0
for gs, gt in zip([Link], TARGET):
if gs != gt: fitness+= 1
return fitness
# Driver code
def main():
global POPULATION_SIZE
#current generation

19
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

generation = 1

found = False
population = []
# create initial population
for _ in range(POPULATION_SIZE):
gnome = Individual.create_gnome()
[Link](Individual(gnome))
while not found:
# sort the population in increasing order of fitness score
population = sorted(population, key = lambda x:[Link])
# if the individual having lowest fitness score ie.
# 0 then we know that we have reached to the target
# and break the loop
if population[0].fitness <= 0:
found = True
break
# Otherwise generate new offsprings for new generation
new_generation = []
# Perform Elitism, that mean 10% of fittest population
# goes to the next generation
s = int((10*POPULATION_SIZE)/100)
new_generation.extend(population[:s])
# From 50% of fittest population, Individuals
# will mate to produce offspring
s = int((90*POPULATION_SIZE)/100)
for _ in range(s):
parent1 = [Link](population[:50])
parent2 = [Link](population[:50])
child = [Link](parent2)
new_generation.append(child)
population = new_generation
print("Generation: {}\tString: {}\tFitness: {}".\
format(generation,
"".join(population[0].chromosome),
population[0].fitness))
generation += 1

print("Generation: {}\tString: {}\tFitness: {}".\


format(generation,
"".join(population[0].chromosome),
population[0].fitness))
if name == ' main ':
main()

OUTPUT:

20
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

21
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

LAB EXPERIMENT 8

OBJECTIVE:

Implement an algorithm to demonstrate Back Propagation Algorithm in python

ALGORITHM:
It is the most widely used algorithm for training artificial neural networks.
In the simplest scenario, the architecture of a neural network consists of some sequential
layers, where the
layer numbered i is connected to the layer numbered i+1. The layers can be classified into 3
classes:
1. Input
2. Hidden
3. Output

22
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

Usually, each neuron in the hidden layer uses an activation function like sigmoid or rectified
linear unit
(ReLU). This helps to capture the non-linear relationship between the inputs and their outputs.
The neurons in the output layer also use activation functions like sigmoid (for regression) or
SoftMax (for
classification).
To train a neural network, there are 2 passes (phases):
Forward
Backward
The forward and backward phases are repeated from some epochs. In each epoch, the
following occurs:
1. The inputs are propagated from the input to the output layer.
2. The network error is calculated.
3. The error is propagated from the output layer to the input layer.
Knowing that there’s an error, what should we do? We should minimize it. To minimize
network error, we
must change something in the network. Remember that the only parameters we can change
are the weights
and biases. We can try different weights and biases, and then test our network.

Source Code:
import numpy
import [Link] as plt
def sigmoid(sop):
return 1.0/(1+[Link](-1*sop))
def error(predicted, target):
return [Link](predicted-target, 2)
def error_predicted_deriv(predicted, target):
return 2*(predicted-target)
def sigmoid_sop_deriv(sop):
return sigmoid(sop)*(1.0-sigmoid(sop))
def sop_w_deriv(x):
return x
def update_w(w, grad, learning_rate):
return w - learning_rate*grad
x1=0.1
x2=0.4
target = 0.7
learning_rate = 0.01
w1=[Link]()
w2=[Link]()
print("Initial W : ", w1, w2)
predicted_output = []
network_error = []
old_err = 0
for k in range(80000):
# Forward Pass
y = w1*x1 + w2*x2
predicted = sigmoid(y)

23
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

err = error(predicted, target)


predicted_output.append(predicted)
network_error.append(err)
# Backward Pass

g1 = error_predicted_deriv(predicted, target)
g2 = sigmoid_sop_deriv(y)
g3w1 = sop_w_deriv(x1)
g3w2 = sop_w_deriv(x2)
gradw1 = g3w1*g2*g1
gradw2 = g3w2*g2*g1
w1 = update_w(w1, gradw1, learning_rate)
w2 = update_w(w2, gradw2, learning_rate)
#print(predicted)
[Link]()
[Link](network_error)
[Link]("Iteration Number vs Error")
[Link]("Iteration Number")
[Link]("Error")
[Link]()
[Link]()
[Link](predicted_output)
[Link]("Iteration Number vs Prediction")
[Link]("Iteration Number")
[Link]("Prediction")
[Link]()

OUTPUT:
Initial W : 0.08698924153243281 0.4532713230157145

24
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

25
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

LAB EXPERIMENT 9

OBJECTIVE:

Implementing FIND-S algorithm using python

Training Database

Algorithm
1. Initialize h to the most specific hypothesis in H
2. For each positive training instance x
26
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

For each attribute constraint a, in h


If the constraint a, is satisfied by x
Then do nothing
Else replace a, in h by the next more general constraint that is satisfied by x
3. Output hypothesis h

Hypothesis Construction

Source Code:
with open('[Link]', 'r') as csvfile:
for row in [Link](csvfile):
[Link](row)
print(a)
print("\n The total number of training instances are : ",len(a))
num_attribute = len(a[0])-1
print("\n The initial hypothesis is : ")
hypothesis = ['0']*num_attribute
print(hypothesis)
for i in range(0, len(a)):
if a[i][num_attribute] == 'TRUE': #for each positive example only
for j in range(0, num_attribute):
if hypothesis[j] == '0' or hypothesis[j] == a[i][j]:
hypothesis[j] = a[i][j]
else:
hypothesis[j] = '?'
print("\n The hypothesis for the training instance {} is : \n".format(i+1),hypothesis)
print("\n The Maximally specific hypothesis for the training instance is ")
print(hypothesis)

OUTPUT:

27
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

LAB EXPERIMENT 10

OBJECTIVE:

Implementing Candidate Elimination algorithm using python

Training Database

28
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

Algorithm

29
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

Source Code:
import csv
with open("[Link]") as f:
csv_file=[Link](f)
data=list(csv_file)
print(data)
print(" ")
s=data[1][:-1] #extracting one row or instance or record
g=[['?' for i in range(len(s))] for j in range(len(s))]
print(s)
print(" ")
print(g)
print(" ")
for i in data:
if i[-1]=="TRUE": # For each positive training record or instance
for j in range(len(s)):
if i[j]!=s[j]:
s[j]='?'
g[j][j]='?'
elif i[-1]=="FALSE": # For each negative training record or example
for j in range(len(s)):
if i[j]!=s[j]:
g[j][j]=s[j]

else:
g[j][j]="?"
print("\nSteps of Candidate Elimination Algorithm",[Link](i)+1)
print(s)
print(g)
gh=[]
for i in g:
for j in i:
if j!='?':
[Link](i)
break
print("\nFinal specific hypothesis:\n",s)
print("\nFinal general hypothesis:\n",gh)

OUTPUT:

30
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

31
Department of CSE ML WITH PYTHON LAB MANUAL (LC-CSE-412G) 2023-2024

This lab manual has been updated by

Prof. Sukrati Chaturvedi

([Link]@[Link])

Crosschecked By

HOD CSE

Please spare some time to provide your valuable feedback.

32

You might also like