0% found this document useful (0 votes)
2 views63 pages

Ad24405 Ml Lab Record

The document is a practical record for the Machine Learning Laboratory course at GRT Institute of Engineering and Technology, detailing course objectives, experiments, and outcomes for the academic year 2025-2026. It includes a vision and mission statement for the Department of Artificial Intelligence and Data Science, along with program educational objectives and specific outcomes. Additionally, it outlines the machine learning lifecycle, types of machine learning, their benefits, challenges, and applications across various industries.

Uploaded by

kalavathy.d
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)
2 views63 pages

Ad24405 Ml Lab Record

The document is a practical record for the Machine Learning Laboratory course at GRT Institute of Engineering and Technology, detailing course objectives, experiments, and outcomes for the academic year 2025-2026. It includes a vision and mission statement for the Department of Artificial Intelligence and Data Science, along with program educational objectives and specific outcomes. Additionally, it outlines the machine learning lifecycle, types of machine learning, their benefits, challenges, and applications across various industries.

Uploaded by

kalavathy.d
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

GRT INSTITUTE OF

ENGINEERING AND
TECHNOLOGY, Tiruttani
(An Autonomous Institution)
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai.
Accredited by NAAC with “A++” Grade & An ISO 9001:2015 Certified Institution.

DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE

PRACTICAL RECORD

Year/Semester: II / 4

Subject Code/Name: AD24405 / MACHINE


LEARNING LABORATORY

Name:

Register No.:

2025 - 2026
GRT INSTITUTE OF
ENGINEERING AND
TECHNOLOGY, Tiruttani
(An Autonomous Institution)
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai.
Accredited by NAAC with “A++” Grade & An ISO 9001:2015 Certified Institution.

DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE

Bonafide Certificate
NAME: …………………………….…….. PROGRAMME: ……………..

REG. NO.: ………………………………. YEAR/SEM: …….…………..

Certified that this is a bonafide record of the practical work done by


the above student in AD24405 – MACHINE LEARNING
LABORATORY in the Department of ARTIFICIAL INTELLIGENCE
AND DATA SCIENCE at GRT INSTITUTE OF ENGINEERING AND
TECHNOLOGY during the academic year 2025 - 2026.

FACULTY IN CHARGE HEAD OF THE DEPARTMENT

Submitted for the University Practical Examination held


on……………………………

INTERNAL EXAMINER EXTERNAL EXAMINER


GRT INSTITUTE OF
ENGINEERING AND
TECHNOLOGY, Tiruttani
(An Autonomous Institution)
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai.
Accredited by NAAC with “A++” Grade & An ISO 9001:2015 Certified Institution.

DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE

DEPARTMENT VISION AND MISSION

VISION:

➢ To create high quality software engineers, academicians, researchers and entrepreneurs


for creating a value-based sustainable society in the field of Artificial Intelligence and Data
Science.

MISSION:

➢ Establishing a climate of academic success using the most effective teaching-learning


strategies.
➢ Creating individuals with practical training and expertise to satisfy corporate demands.
➢ To improve the Students Knowledge in Data Science Education, research and Industry
Collaboration in the field of AI and Data Science.
➢ To develop strong moral principles and problem-solving skills among students.

PROGRAM EDUCATIONAL OBJECTIVES (PEOS)

The Artificial Intelligence and Data Science Graduates will:


PEO1: Utilize their proficiencies in the fundamental knowledge of basic sciences,
mathematics, Artificial Intelligence, data science and statistics to build systems that require
management and analysis of large volumes of data.
PEO2: Design and model AI based solutions to critical problem domains in the real world.
PEO3: Exhibit innovative thoughts and creative ideas for effective contribution towards
economy building.

PROGRAM SPECIFIC OUTCOMES (PSOs)

A graduate of the Artificial Intelligence and Data Science Program will be able to:
PSO1: Develop data analytics and data visualization skills, skills pertaining to knowledge
acquisition, knowledge representation and knowledge engineering, and hence be capable of
coordinating complex projects.
PSO2: Create, select and apply the theoretical knowledge of AI and Data Analytics along
with practical Industrial tools and techniques to manage and solve wicked societal problems.
PSO3: Able to carry out fundamental research to cater the critical needs of the society
through cutting edge technologies of AI.
| AD24405 – MACHINE LEARNING LABORATORY

AD24405 – MACHINE LEARNING LABORATORY


(R2024)

SYLLABUS
COURSE OBJECTIVES:
The student should be made:
• To understand the data sets and apply suitable algorithms for selecting the appropriate features for
analysis.
• To learn to implement supervised machine learning algorithms on standard datasets and evaluate the
performance.
• To experiment the unsupervised machine learning algorithms on standard datasets and evaluate the
performance.
• To build the graph-based learning models for standard data sets.
• To compare the performance of different ML algorithms and select the suitable one based on the
application.

LIST OF EXPERIMENTS
1. For a given set of training data examples stored in a .CSV file, implement and demonstrate the
Candidate-Elimination algorithm to output a description of the set of all hypotheses consistent with the
training examples.
2. Write a program to demonstrate the working of the decision tree based ID3 algorithm. Use an
appropriate data set for building the decision tree and apply this knowledge to classify a new sample.
3. Build an Artificial Neural Network by implementing the Backpropagation algorithm and test the same
using appropriate data sets.
4. Write a program to implement the naïve Bayesian classifier for a sample training data set stored as a
.CSV file and compute the accuracy with a few test data sets.
5. Implement naïve Bayesian Classifier model to classify a set of documents and measure the accuracy,
precision, and recall.
6. Write a program to construct a Bayesian network to diagnose CORONA infection using standard WHO
Data Set.
7. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data set for clustering
using the k-Means algorithm. Compare the results of these two algorithms.
8. Write a program to implement k-Nearest Neighbour algorithm to classify the iris data set .print both
correct and wrong predictions.
9. Implement the non-parametric Locally Weighted Regression algorithm in order to fit data points. Select
an appropriate data set for your experiment and draw graphs.
10. Build a Support Vector Machine (SVM) model using the given dataset to classify the target variable.
Perform preprocessing, choose an appropriate kernel, train the model, and evaluate its performance
using suitable metrics.

COURSE OUTCOMES:
At the end of the course, the students will be able to:
CO1: Apply suitable algorithms for selecting the appropriate features for analysis.
CO2: Design Supervised machine learning algorithms on standard datasets and evaluate the performance.
CO3: Apply unsupervised machine learning algorithms on standard datasets and evaluate the performance.
CO4: Build the neural network learning models for standard data sets.
CO5: Assess and compare the performance of different ML algorithms and select the suitable one based on
the application.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

INDEX
SUBJECT CODE / TITLE: AD24405 / MACHINE LEARNING LABORATORY

EX. PAGE MARKS FACULTY


DATE NAME OF THE EXPERIMENT
NO. NUMBER (OUT OF 100) SIGNATURE

Implementation of Candidate –
1
Elimination algorithm
Implementation of Decision tree in
2
ID3 algorithm
Implementation of back propagation
3 algorithm to build an artificial neural
network
Implementation of Naïve Bayesian
4 classifier for a sample training
dataset and to compute accuracy
Implementation of naïve Bayesian
Classifier model to classify a set of
5
documents and to measure the
accuracy, precision, and recall
Construction of a Bayesian network
6 to diagnose CORONA infection
using standard WHO Data Set
Comparison of clustering in EM
7 algorithm and k-means algorithm
using the same data set
Implementation of k-Nearest
8 Neighbour algorithm to classify the
iris data set
Implementation of the non-
parametric Locally Weighted
9
Regression algorithm in order to fit
data points
Implementation of Support Vector
10 Machine (SVM) model to classify
the target variable.
CONTENT BEYOND SYLLABUS

1 Least Square Regression Model

2 Rule Based Classifier Model

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

INTRODUCTION TO MACHINE LEARNING

MACHINE LEARNING
A subset of artificial intelligence known as machine learning focuses primarily on
the creation of algorithms that enable a computer to independently learn from data and
previous experiences, improve performance from experiences, and predict things.

MACHINE LEARNING LIFECYCLE:


The lifecycle of a machine learning project involves a series of steps that include:
1. Study the Problems: The first step is to study the problem. This step involves
understanding the business problem and defining the objectives of the model.
2. Data Collection: When the problem is well-defined, we can collect the relevant data
required for the model. The data could come from various sources such as databases,
APIs, or web scraping.
3. Data Preparation: When our problem-related data is collected. then it is a good idea
to check the data properly and make it in the desired format so that it can be used by
the model to find the hidden patterns. This can be done in the following steps:
• Data cleaning
• Data Transformation
• Explanatory Data Analysis and Feature Engineering
• Split the dataset for training and testing.
4. Model Selection: The next step is to select the appropriate machine learning
algorithm that is suitable for our problem. This step requires knowledge of the
strengths and weaknesses of different algorithms. Sometimes we use multiple models
and compare their results and select the best model as per our requirements.
5. Model building and Training: After selecting the algorithm, we have to build the
model.
• In the case of traditional machine learning building mode is easy it is just a few
hyperparameter tunings.
• In the case of deep learning, we have to define layer-wise architecture along with
input and output size, number of nodes in each layer, loss function, gradient
descent optimizer, etc.
• After that model is trained using the preprocessed dataset.
6. Model Evaluation: Once the model is trained, it can be evaluated on the test dataset
to determine its accuracy and performance using different techniques like
classification report, F1 score, precision, recall, ROC Curve, Mean Square error,
absolute error, etc.
7. Model Tuning: Based on the evaluation results, the model may need to be tuned or
optimized to improve its performance. This involves tweaking the hyperparameters of
the model.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

8. Deployment: Once the model is trained and tuned, it can be deployed in a production
environment to make predictions on new data. This step requires integrating the
model into an existing software system or creating a new system for the model.
9. Monitoring and Maintenance: Finally, it is essential to monitor the model’s
performance in the production environment and perform maintenance tasks as
required. This involves monitoring for data drift, retraining the model as needed, and
updating the model as new data becomes available.

TYPES OF MACHINE LEARNING

• Supervised Machine Learning


• Unsupervised Machine Learning
• Reinforcement Machine Learning
• Supervised Machine Learning
Supervised learning is defined as when a model gets trained on a "Labeled Dataset".
Labelled datasets have both input and output parameters. In Supervised Learning algorithms
learn to map points between inputs and correct outputs. It has both training and validation
datasets labelled.

Example: If you train a model using labeled images of cats and dogs, it learns the features of
each. When shown a new image, it predicts whether it’s a cat or a dog.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

There are two main categories of supervised learning that are mentioned below:
Classification - predicts categorical outputs, meaning it assigns data into predefined classes
like spam/non-spam emails or disease risk categories. These algorithms learn to map input
features to discrete labels.
Here are some classification algorithms:
• Logistic Regression
• Decision Tree
• Random Forest
• K-Nearest Neighbors (KNN)
• Naive Bayes
• Support Vector Machine (SVM)

Regression - predicts continuous values, such as house prices or product sales. It learns the
relationship between input features and a numerical target variable.
Here are some regression algorithms:
• Linear Regression
• Polynomial Regression
• Ridge Regression
• Lasso Regression
• Decision tree
• Random Forest

• Unsupervised Machine Learning


Unsupervised Learning works with unlabeled data, meaning there are no predefined
outputs. The algorithm finds hidden patterns, groups or relationships within the data on its
own. It is mainly used for clustering, dimensionality reduction and data visualization.

Example: If you have customer data without labels, the algorithm can group similar
customers based on purchase behavior useful for segmentation and marketing.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

There are two main categories of unsupervised learning that are mentioned below:
Clustering - the process of grouping data points into clusters based on their similarity. This
technique is useful for identifying patterns and relationships in data without the need for
labeled examples.
Common techniques include:
• K-Means
• DBSCAN
• Mean-shift

Dimensionality reduction - helps reduce the number of features while preserving important
information.
Common techniques include:
• Principal Component Analysis
• Independent Component Analysis

Association rule learning - a technique for discovering relationships between items in a


dataset. It identifies rules that indicate the presence of one item implies the presence of
another item with a specific probability.
Common techniques include:
• Apriori
• FP-growth
• Eclat

• Reinforcement Learning
Reinforcement learning trains an agent to make a sequence of decisions through trial and
error. The agent interacts with the environment, receives feedback in the form of rewards or
penalties and learns optimal actions over time.

Example: An AI agent learning to play chess gets positive feedback for good moves and
negative for poor ones. Over time, it learns strategies to win more often.
Here are some of most common reinforcement learning algorithms:
• Q-learning
• SARSA (State-Action-Reward-State-Action
• Deep Q-learning

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Benefits of Machine Learning


Machine Learning improves processes by automating tasks and extracting insights from data,
making systems smarter and more efficient.
• Automates repetitive tasks and improves productivity
• Finds patterns in large data for better decisions
• Provides customized recommendations and experiences
• Enables robots and systems to perform complex tasks accurately

Challenges
• ML models learn from training data and if the data is biased, model’s decisions can
be unfair so it is important to select and monitor data carefully.
• Since it depends on large amounts of data, there is a risk of sensitive information
being exposed so protecting privacy is important.
• Complex ML models can be difficult to understand which makes it difficult to explain
why they make certain decisions. This can affect trust and accountability.
• Automation may replace some jobs so retraining and helping workers learn new skills
is important to adapt to these changes.

Applications
Machine Learning is used across industries to solve problems and improve services.
• In healthcare it diagnoses diseases, predicts outcomes and personalizes treatments
• In finance detects fraud, supports trading and assesses credit risk
• Recommends products, forecasts demand and analyzes customer behavior
• Powers self-driving cars, optimizes routes and predicts maintenance
• Recommends content and enables image/speech recognition
• Detects defects and predicts machine failures

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 1 IMPLEMENTATION OF CANDIDATE –ELIMINATION


Date: ALGORITHM

AIM:
To implement and demonstrate the Candidate-Elimination algorithm, for a given set
of training data examples stored in a .CSV file, to output a description of the set of all
hypotheses consistent with the training examples.

ALGORITHM:
1. Load Data set.
2. Initialize General Hypothesis and Specific Hypothesis.
3. For each training example
if example is Positive example
if attribute_value == hypothesis_value:
Do Nothig
else:
replace attribute value with '?' (Basically generalizing it)
if example is Negative example
Make generalize hypothesis more specific.

DATA SET:
[Link]

sky airTemp humidity wind water forecast enjoySport


Sunny Warm Normal Strong Warm Same Yes
Sunny Warm High Strong Warm Same Yes
Rainy Cold High Strong Warm Change No
Sunny Warm High Strong Cool Change Yes

PROGRAM:
import numpy as np
import pandas as pd

data = [Link](data=pd.read_csv('[Link]'))
print(data)

concepts = [Link]([Link][:,0:-1])
print(concepts)

target = [Link]([Link][:,-1])
print(target)
def learn(concepts, target):
specific_h = concepts[0].copy()
print("\nInitialization of specific_h and general_h")
print(specific_h)

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

general_h = [["?" for i in range(len(specific_h))] for i in range(len(specific_h))]


print(general_h)

for i, h in enumerate(concepts):
if target[i] == "Yes":
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
specific_h[x] = '?'
general_h[x][x] = '?'
if target[i] == "No":
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
general_h[x][x] = specific_h[x]
else:
general_h[x][x] = '?'

print("\nSteps of Candidate Elimination Algorithm",i+1)


print(specific_h)
print(general_h)

indices = [i for i, val in enumerate(general_h) if val == ['?', '?', '?', '?', '?', '?']]
for i in indices:
general_h.remove(['?', '?', '?', '?', '?', '?'])
return specific_h, general_h

s_final, g_final = learn(concepts, target)


print("\nFinal Specific_h:", s_final, sep="\n")
print("\nFinal General_h:", g_final, sep="\n")

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

OUTPUT:

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

RESULT:
Thus the Candidate-Elimination algorithm, to test all the hypotheses with the
training sets using python was executed and verified successfully.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 2
IMPLEMENTATION OF DECISION TREE IN ID3 ALGORITHM
Date:

AIM:
To build Decision tree in ID3 algorithm to classify a new sample using python.

ALGORITHM:
1. Observe the dataset.
2. Import the necessary basic python libraries.
3. Read the dataset.
4. Calculate the Entropy of the whole dataset.
5. Calculate the Entropy of the filtered dataset.
6. Calculate the Information gain for the feature(outlook).
7. Finding the most informative feature (feature with highest information gain).
8. Adding a node to the tree.
9. Perform ID3 algorithm and generate a tree.
10. Finding unique classes of the label.
11. Predict from the tree.
12. Evaluate the test dataset.
13. Check the test dataset.

DATA SET:
[Link]

Outlook Temperature Humidity Windy PlayTennis


Sunny Hot High FALSE No
Sunny Hot High TRUE No
Overcast Hot High FALSE Yes
Rainy Mild High FALSE Yes
Rainy Cool Normal FALSE Yes
Rainy Cool Normal TRUE No
Overcast Cool Normal TRUE Yes
Sunny Mild High FALSE No
Sunny Cool Normal FALSE Yes
Rainy Mild Normal FALSE Yes
Sunny Mild Normal TRUE Yes
Overcast Mild High TRUE Yes
Overcast Hot Normal FALSE Yes
Rainy Mild High TRUE No

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

PROGRAM:
import numpy as np
import math
import csv

def read_data(filename):
with open(filename, 'r') as csvfile:
datareader = [Link](csvfile, delimiter=',')
headers = next(datareader)
metadata = []
traindata = []

for name in headers:


[Link](name)
for row in datareader:
[Link](row)

return (metadata, traindata)

class Node:
def __init__(self, attribute):
[Link] = attribute
[Link] = []
[Link] = ""

def __str__(self):
return [Link]

def subtables(data, col, delete):


dict = {}
items = [Link](data[:, col])
count = [Link](([Link][0], 1), dtype=np.int32)

for x in range([Link][0]):
for y in range([Link][0]):
if data[y, col] == items[x]:
count[x] += 1

for x in range([Link][0]):
dict[items[x]] = [Link]((int(count[x]), [Link][1]), dtype="|S32")
pos = 0
for y in range([Link][0]):
if data[y, col] == items[x]:
dict[items[x]][pos] = data[y]
pos += 1
if delete:
dict[items[x]] = [Link](dict[items[x]], col, 1)

return items, dict

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

def entropy(S):
items = [Link](S)

if [Link] == 1:
return 0

counts = [Link](([Link][0], 1))


sums = 0

for x in range([Link][0]):
counts[x] = sum(S == items[x]) / ([Link] * 1.0)

for count in counts:


sums += -1 * count * [Link](count, 2)
return sums

def gain_ratio(data, col):


items, dict = subtables(data, col, delete=False)

total_size = [Link][0]
entropies = [Link](([Link][0], 1))
intrinsic = [Link](([Link][0], 1))

for x in range([Link][0]):
ratio = dict[items[x]].shape[0]/(total_size * 1.0)
entropies[x] = ratio * entropy(dict[items[x]][:, -1])
intrinsic[x] = ratio * [Link](ratio, 2)

total_entropy = entropy(data[:, -1])


iv = -1 * sum(intrinsic)

for x in range([Link][0]):
total_entropy -= entropies[x]

return total_entropy / iv

def create_node(data, metadata):


if ([Link](data[:, -1])).shape[0] == 1:
node = Node("")
[Link] = [Link](data[:, -1])[0]
return node

gains = [Link](([Link][1] - 1, 1))

for col in range([Link][1] - 1):


gains[col] = gain_ratio(data, col)

split = [Link](gains)

node = Node(metadata[split])

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

metadata = [Link](metadata, split, 0)


items, dict = subtables(data, split, delete=True)

for x in range([Link][0]):
child = create_node(dict[items[x]], metadata)
[Link]((items[x], child))

return node

def empty(size):
s = ""
for x in range(size):
s += " "
return s

def print_tree(node, level):


if [Link] != "":
print(empty(level), [Link])
return
print(empty(level), [Link])
for value, n in [Link]:
print(empty(level + 1), value)
print_tree(n, level + 2)

metadata, traindata = read_data("[Link]")


data = [Link](traindata)
node = create_node(data, metadata)
print_tree(node, 0)

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

OUTPUT:

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

RESULT:
Thus the program to implement decision tree based ID3 algorithm using python was
executed and verified successfully.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 3 IMPLEMENTATION OF BACK PROPAGATION ALGORITHM


TO BUILD AN ARTIFICIAL NEURAL NETWORK
Date:

AIM:
To implement the Back Propagation algorithm to build an Artificial Neural Network.

ALGORITHM:
1. Inputs X, arrive through the preconnected path.
2. Input is modelled using real weights W. The weights are usually randomly selected.
3. Calculate the output for every neuron from the input layer, to the hidden layers, to the
output layer.
4. Calculate the error in the outputs.
5. Travel back from the output layer to the hidden layer to adjust the weights such that the
errors is decreased.
6. Keep repeating the process until the desired output is achieved.

PROGRAM:
import numpy as np

X = [Link](([2, 9], [1, 5], [3, 6]), dtype=float) # X = (hours sleeping, hours studying)
y = [Link](([92], [86], [89]), dtype=float) # y = score on test

# scale units
X = X/[Link](X, axis=0) # maximum of X array
y = y/100 # max test score is 100

class Neural_Network(object):
def __init__(self):
[Link] = 2
[Link] = 1
[Link] = 3
self.W1 = [Link]([Link], [Link])
self.W2 = [Link]([Link], [Link])

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

def forward(self, X):


self.z = [Link](X, self.W1)
self.z2 = [Link](self.z)
self.z3 = [Link](self.z2, self.W2)
o = [Link](self.z3)
return o

def sigmoid(self, s):


return 1/(1+[Link](-s)) # activation function

def sigmoidPrime(self, s):


return s * (1 - s) # derivative of sigmoid

def backward(self, X, y, o):


self.o_error = y - o # error in output
self.o_delta = self.o_error*[Link](o)
self.z2_error = self.o_delta.dot(self.W2.T)
self.z2_delta = self.z2_error*[Link](self.z2)
self.W1 += [Link](self.z2_delta)
self.W2 += [Link](self.o_delta)

def train (self, X, y):


o = [Link](X)
[Link](X, y, o)

NN = Neural_Network()
print ("\nInput: \n" + str(X))
print ("\nActual Output: \n" + str(y))
print ("\nPredicted Output: \n" + str([Link](X)))
print ("\nLoss: \n" + str([Link]([Link](y - [Link](X)))))
[Link](X, y)

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

OUTPUT:

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

RESULT:
Thus the Back propagation algorithm to build an Artificial Neural network was
implemented successfully.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 4 IMPLEMENTATION OF NAÏVE BAYESIAN CLASSIFIER


FOR A SAMPLE TRAINING DATASET AND
Date: TO COMPUTE ACCURACY

AIM:
To implement Naïve Bayesian classifier for Tennis data set and to compute the
accuracy with few datasets.

ALGORITHM:
1. Convert the data set into a frequency table.
2. Create likelihood table by finding the probabilities like overcast probability = 0.29 and
probability of plating is 0.64.
3. Now, use Naive Bayesian equation to calculate the posterior probability for each class.
The class with the highest posterior probability is the outcome of prediction.
Problem:
Players will play if weather is sunny. Is this statement is correct?
We can solve it using above discussed method of posterior probability.
P(Yes | Sunny) = P( Sunny | Yes) * P(Yes) / P (Sunny)
Here we have
P (Sunny |Yes) = 3/9 = 0.33,
P(Sunny) = 5/14 = 0.36,
P( Yes)= 9/14 = 0.64
Now, P (Yes | Sunny) = 0.33 * 0.64 / 0.36 = 0.60, which has higher probability.

DATA SET:
[Link]

Outlook Temperature Humidity Windy PlayTennis


Sunny Hot High FALSE No
Sunny Hot High TRUE No
Overcast Hot High FALSE Yes
Rainy Mild High FALSE Yes
Rainy Cool Normal FALSE Yes
Rainy Cool Normal TRUE No
Overcast Cool Normal TRUE Yes
Sunny Mild High FALSE No
Sunny Cool Normal FALSE Yes
Rainy Mild Normal FALSE Yes
Sunny Mild Normal TRUE Yes
Overcast Mild High TRUE Yes
Overcast Hot Normal FALSE Yes
Rainy Mild High TRUE No

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

PROGRAM:
# import necessary libarities
import pandas as pd
from sklearn import tree
from [Link] import LabelEncoder
from sklearn.naive_bayes import GaussianNB

# load data from CSV


data = pd.read_csv('[Link]')
print("THe first 5 values of data is :\n",[Link]())

# obtain Train data and Train output


X = [Link][:,:-1]
print("\nThe First 5 values of train data is\n",[Link]())

y = [Link][:,-1]
print("\nThe first 5 values of Train output is\n",[Link]())

# Convert then in numbers


le_outlook = LabelEncoder()
[Link] = le_outlook.fit_transform([Link])

le_Temperature = LabelEncoder()
[Link] = le_Temperature.fit_transform([Link])

le_Humidity = LabelEncoder()
[Link] = le_Humidity.fit_transform([Link])

le_Windy = LabelEncoder()
[Link] = le_Windy.fit_transform([Link])

print("\nNow the Train data is :\n",[Link]())

le_PlayTennis = LabelEncoder()
y = le_PlayTennis.fit_transform(y)
print("\nNow the Train output is\n",y)

from sklearn.model_selection import train_test_split


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

classifier = GaussianNB()
[Link](X_train,y_train)

from [Link] import accuracy_score


print("Accuracy is:",accuracy_score([Link](X_test),y_test))

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

OUTPUT:

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

RESULT:
Thus, the program to implement Naïve Bayesian classifier to compute the accuracy
with few datasets using python was executed and verified successfully.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 5 IMPLEMENTATION OF NAÏVE BAYESIAN CLASSIFIER


MODEL TO CLASSIFY A SET OF DOCUMENTS AND TO
Date: MEASURE THE ACCURACY, PRECISION, AND RECALL

AIM:
To classify a set of documents using Naïve Bayesian classifier and to measure the
accuracy, precision and recall.

ALGORITHM:
1. Import basic libraries.
2. Import the dataset.
3. Perform Data preprocessing.
4. Train the model.
5. Test and evaluate the model.
6. Visualizing the model.
7. Print the performance indicators.

DATA SET:

[Link]

I love this sandwich pos


This is an amazing place pos
I feel very good about these beers pos
This is my best work pos
What an awesome view pos
I do not like this restaurant neg
I am tired of this stuff neg
I can't deal with this neg
He is my sworn enemy neg
My boss is horrible neg
This is an awesome place pos
I do not like the taste of this juice neg
I love to dance pos
I am sick and tired of this place neg
What a great holiday pos
That is a bad locality to stay neg
We will have good fun tomorrow pos
I went to my enemy's house today neg

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

PROGRAM:
import pandas as pd
msg = pd.read_csv('[Link]', names=['message', 'label'])
print("Total Instances of Dataset: ", [Link][0])
msg['labelnum'] = [Link]({'pos': 1, 'neg': 0})

X = [Link]
y = [Link]
from sklearn.model_selection import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y)
from sklearn.feature_extraction.text import CountVectorizer

count_v = CountVectorizer()
Xtrain_dm = count_v.fit_transform(Xtrain)
Xtest_dm = count_v.transform(Xtest)

df = [Link](Xtrain_dm.toarray(),columns=count_v.get_feature_names_out())
print(df[0:5])

from sklearn.naive_bayes import MultinomialNB


clf = MultinomialNB()
[Link](Xtrain_dm, ytrain)
pred = [Link](Xtest_dm)

for doc, p in zip(Xtrain, pred):


p = 'pos' if p == 1 else 'neg'
print("%s -> %s" % (doc, p))

from [Link] import accuracy_score, confusion_matrix, precision_score, recall_score


print('Accuracy Metrics: \n')
print('Accuracy: ', accuracy_score(ytest, pred))
print('Recall: ', recall_score(ytest, pred))
print('Precision: ', precision_score(ytest, pred))
print('Confusion Matrix: \n', confusion_matrix(ytest, pred))

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

OUTPUT:

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

RESULT:
Thus the classification of a set of documents using Naïve Bayesian classifier was
done successfully and measured the accuracy, precision and recall.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 6 CONSTRUCTION OF A BAYESIAN NETWORK TO


DIAGNOSE CORONA INFECTION USING
Date: STANDARD WHO DATA SET

AIM:
To construct a Bayesian network to diagnose corona infection using WHO data set.

ALGORITHM:
This Naive Bayes is broken down into 5 parts:
1. Separate by Class.
2. Summarize Dataset.
3. Summarize Data by Class.
4. Gaussian Probability Density Function.
5. Class Probabilities.

PROGRAM:
import pandas as pd
covid_19_data=pd.read_csv("/content/[Link]")
print(covid_19_data)

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

import warnings
[Link]("ignore",category=FutureWarning)
covid_19_data=pd.read_csv("/content/[Link]")
print(f'The shape of the dataframe is {covid_19_data.shape}')
print()
The shape of the dataframe is (5, 11)
print(covid_19_data.info())
print()
import numpy as np
covid_19_data.replace(to_replace='?',value=[Link],inplace=True)
print(covid_19_data.describe(include='all'))
print()

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

print(covid_19_data["Loss of Taste/Smell"].value_counts())
print(covid_19_data.isnull().sum())

import seaborn as sns


[Link](x="Loss of Taste/Smell",data=covid_19_data,linewidth=3)

import [Link] as plt


covid_19_data[['Patient ID','Age','Gender','Fever','Cough','Sore Throat']].hist(bins=50,figsize=(1 5,8))
[Link]()
covid_19_data['Patient ID'].fillna(covid_19_data['Sore Throat'].mode()[0],inplace=True)
X=covid_19_data.drop(['Travel History'],axis=1)
y=covid_19_data.Cough
X=X[['Patient ID','Age']]
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2)
from sklearn.naive_bayes import GaussianNB
NB_classifier=GaussianNB()
NB_classifier.fit(X_test,y_test)
y_predict=NB_classifier.predict(X_test)
from [Link] import confusion_matrix
cm=confusion_matrix(y_test,y_predict)
[Link](cm,annot=True,cmap='Blues')
from [Link] import classification_report
print(classification_report(y_test,y_predict))

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

OUTPUT:

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

RESULT:
Thus the python program to diagnose corona infection using Bayesian network was
implemented successfully.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 7 COMPARISON OF CLUSTERING IN EM ALGORITHM AND


Date: K-MEANS ALGORITHM USING THE SAME DATA SET

AIM:
To compare the clustering in EM algorithm and K-means algorithm using the same data
set.

ALGORITHM:
The K-means implementation is as follows:
1. Choose the number of clusters k.
2. Select k random points from the data as centroids.
3. Assign all the points to the closest cluster centroid.
4. Recompute the centroids of newly formed clusters.
5. Repeat steps 3 and 4.

The EM implementation is as follows:


1. Expectation step (E - step): It involves the estimation (guess) of all missing values in
the dataset so that after completing this step, there should not be any missing value.
2. Maximization step (M - step): This step involves the use of estimated data in the E-step
and updating the parameters.
3. Repeat E-step and M-step until the convergence of the values occur.

PROGRAM:
from [Link] import KMeans
from sklearn import preprocessing
from [Link] import GaussianMixture
from [Link] import load_iris
import [Link] as sm
import pandas as pd
import numpy as np
import [Link] as plt

dataset=load_iris()
# print(dataset)

X=[Link]([Link])
[Link]=['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y=[Link]([Link])
[Link]=['Targets']
# print(X)

[Link](figsize=(14,7))
colormap=[Link](['red','lime','black'])

# REAL PLOT
[Link](1,3,1)
[Link](X.Petal_Length,X.Petal_Width,c=colormap[[Link]],s=40)
[Link]('Real')

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

# K-PLOT
[Link](1,3,2)
model=KMeans(n_clusters=3)
[Link](X)
predY=[Link](model.labels_,[0,1,2]).astype(np.int64)
[Link](X.Petal_Length,X.Petal_Width,c=colormap[predY],s=40)
[Link]('KMeans')

# GMM PLOT
scaler=[Link]()
[Link](X)
xsa=[Link](X)
xs=[Link](xsa,columns=[Link])
gmm=GaussianMixture(n_components=3)
[Link](xs)

y_cluster_gmm=[Link](xs)
[Link](1,3,3)
[Link](X.Petal_Length,X.Petal_Width,c=colormap[y_cluster_gmm],s=40)
[Link]('GMM Classification')
[Link]()

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

OUTPUT:

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

RESULT:
Thus the program to compare clustering in EM and K-means algorithm with the
same dataset was performed successfully.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 8 IMPLEMENTATION OF K-NEAREST NEIGHBOUR


Date: ALGORITHM TO CLASSIFY THE IRIS DATA SET

AIM:
To implement K-Nearest Neighbour algorithm to classify iris data set.

ALGORITHM:
1. Calculate the Information Gain of each feature.
2. Considering that all rows don’t belong to the same class, split the dataset S into subsets
using the feature for which the Information Gain is maximum.
3. Make a decision tree node using the feature with the maximum Information gain.
4. If all rows belong to the same class, make the current node as a leaf node with the class
as its label.
5. Repeat for the remaining features until we run out of all features, or the decision tree
has all leaf nodes.

PROGRAM:
from [Link] import load_iris
from [Link] import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import numpy as np

dataset=load_iris()
#print(dataset)
X_train,X_test,y_train,y_test=train_test_split(dataset["data"],dataset["target"],random_state=0)

kn=KNeighborsClassifier(n_neighbors=1)
[Link](X_train,y_train)

for i in range(len(X_test)):
x=X_test[i]
x_new=[Link]([x])
prediction=[Link](x_new)
print("TARGET=",y_test[i],dataset["target_names"][y_test[i]],"PREDICTED=",prediction,
dataset["target_names"][prediction])
print([Link](X_test,y_test))

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

OUTPUT:

RESULT:
Thus the python program for K-Nearest Neighbour algorithm to classify the iris
data set was implemented successfully.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 9 IMPLEMENTATION OF THE NON-PARAMETRIC LOCALLY


WEIGHTED REGRESSION ALGORITHM IN ORDER TO FIT
Date: DATA POINTS

AIM:
To implement the non-parametric Locally Weighted Regression algorithm in order to fit
data points.

ALGORITHM:
1. Read the Given data Sample to X and the curve (linear or non linear) to Y.
2. Set the value for Smoothening parameter or Free parameter say τ.
3. Set the bias /Point of interest set x0 which is a subset of X.
4. Determine the weight matrix using :

5. Determine the value of model term parameter β using:

6. Prediction = x0*β

PROGRAM:
from math import ceil
import numpy as np
from scipy import linalg

def lowess(x, y, f, iterations):


n = len(x)
r = int(ceil(f * n))
h = [[Link]([Link](x - x[i]))[r] for i in range(n)]
w = [Link]([Link]((x[:, None] - x[None, :]) / h), 0.0, 1.0)
w = (1 - w ** 3) ** 3
yest = [Link](n)
delta = [Link](n)

for iteration in range(iterations):


for i in range(n):
weights = delta * w[:, i]
b = [Link]([[Link](weights * y), [Link](weights * y * x)])
A = [Link]([[[Link](weights), [Link](weights * x)],[[Link](weights * x),
[Link](weights * x * x)]])
beta = [Link](A, b)
yest[i] = beta[0] + beta[1] * x[i]

residuals = y - yest
s = [Link]([Link](residuals))

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

delta = [Link](residuals / (6.0 * s), -1, 1)


delta = (1 - delta ** 2) ** 2

return yest

import math
n = 100
x = [Link](0, 2 * [Link], n)
y = [Link](x) + 0.3 * [Link](n)
f =0.25
iterations=3
yest = lowess(x, y, f, iterations)

import [Link] as plt


[Link](x,y,"r.")
[Link](x,yest,"b-")
[Link]()

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

OUTPUT:

RESULT:
Thus the non-parametric Locally Weighted Regression algorithm to fit data points
was implemented successfully.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 10 IMPLEMENTATION OF SUPPORT VECTOR MACHINE (SVM)


Date: MODEL TO CLASSIFY THE TARGET VARIABLE

AIM:
To Build a Support Vector Machine (SVM) model using the given dataset to classify the
target variable. Perform preprocessing, choose an appropriate kernel, train the model, and
evaluate its performance using suitable metrics.

ALGORITHM:
1. Data Preprocessing: The data will be cleaned, and features will be scaled (a highly
recommended step for SVMs, as they are not scale-invariant).
2. Choose an Appropriate Kernel: Depending on whether the data is linearly separable,
I will select an appropriate kernel (e.g., 'linear', 'rbf' for non-linear data, or 'poly').
3. Train the Model: The data will be split into training and testing sets, and the SVM
model will be trained using the training data.
4. Evaluate Performance: The model's performance will be evaluated on the test set
using suitable metrics such as accuracy, precision, recall, and F1-score.

PROGRAM:
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from [Link] import SVC
from [Link] import StandardScaler
from [Link] import accuracy_score, classification_report
# 1. Load the dataset (replace with your data loading) Example uses the Iris dataset from sklearn
iris = datasets.load_iris()
X = [Link]
y = [Link]
target_names = iris.target_names
# 2. Preprocess Data: Split and Scale
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = [Link](X_test)
# 3. Choose and Train the SVM Model 'rbf' (Radial Basis Function) kernel is a common choice for non-linear data
svm_model = SVC(kernel='rbf', random_state=42)
svm_model.fit(X_train_scaled, y_train)

# 4. Evaluate the Model


y_pred = svm_model.predict(X_test_scaled)
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred, target_names=target_names)
print(f"Accuracy: {accuracy:.2%}")
print("\nClassification Report:")
print(report)

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

OUTPUT:

RESULT:
Thus the python code for building the Support Vector Machine (SVM) model using
the given dataset to classify the target variable was done and Performed preprocessing, chosen
the appropriate kernel, trained the model, and evaluated its performance using the metrics
accuracy, precision, f1 score and recall.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

CONTENT BEYOND SYLLABUS

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 1
LEAST SQUARE REGRESSION MODEL
Date:

AIM:
To Implement Least Square Regression Model.

ALGORITHM:
Step 1: Data Loading and Preprocessing
1. Import necessary libraries (pandas, numpy, [Link], LinearRegression
from sklearn).
2. Read the data from a CSV file ([Link]) and separate it into two variables:
X: the independent variable (the input values).
Y: the dependent variable (the target values).
Step 2: Manual Calculation of the Regression Line (Optional)
1. Calculate the mean of X and Y
2. Compute the slope (m)
3. Compute the intercept (c)
Step 3: Using sklearn Linear Regression (Modern Approach)
1. Initialize a LinearRegression model from sklearn.
2. Fit the model using X and Y.
3. The model computes the slope (m) and intercept (c) automatically using its built-in
method. model.coef_[0] gives the slope (m).
model.intercept_ gives the intercept (c).
Step 4: Making Predictions
Use the model to predict Y_pred (predicted Y values) based on the X values.
Step 5: Visualization
1. Plot the original data points (X, Y).
2. Plot the regression line by drawing a line for Y_pred (predicted values) against X.
Step 6: Output
1. Print the slope and intercept of the regression line.
2. Show a scatter plot of the data points along with the regression line to visualize the
model.

DATASET:

[Link]

32.50235 31.70701
53.4268 68.7776
61.53036 62.56238
47.47564 71.54663
59.81321 87.23093
55.14219 78.21152
52.2118 79.64197
39.29957 59.17149
48.10504 75.33124

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

52.55001 71.30088
45.41973 55.16568
54.35163 82.47885
44.16405 62.00892
58.16847 75.39287
56.72721 81.43619
48.95589 60.7236
44.6872 82.8925
60.29733 97.3799
45.61864 48.84715
38.81682 56.87721
66.18982 83.87856
65.41605 118.5912
47.48121 57.25182
41.57564 51.39174
51.84519 75.38065
59.37082 74.76556
57.31 95.45505
63.61556 95.22937
46.73762 79.05241
50.55676 83.43207
52.224 63.35879
35.56783 41.41289
42.43648 76.61734
58.16454 96.76957
57.50445 74.08413
45.44053 66.58814
61.89622 77.76848
33.09383 50.71959
36.43601 62.12457
37.67565 60.81025
44.55561 52.68298
43.31828 58.56982
50.07315 82.90598
43.87061 61.42471
62.99748 115.2442
32.66904 45.57059
40.1669 54.08405
53.57508 87.99445
33.86421 52.72549
64.70714 93.57612
38.11982 80.16628
44.50254 65.10171
40.59954 65.5623

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

41.72068 65.28089
51.08863 73.43464
36.43601 62.12457
41.10269 59.35885
41.18202 61.68404
50.18639 69.8476
52.37845 86.09829
50.13549 59.10884
33.64471 69.89968
39.5579 44.86249
56.13039 85.49807
57.36205 95.53669
60.26921 70.25193
35.67809 52.72173
31.58812 50.39267
53.66093 63.6424
46.68223 72.24725
43.10782 57.81251
70.34608 104.2571
44.49286 86.64202
57.50453 91.48678
36.93008 55.23166
55.80573 79.55044
38.95477 44.84712
56.90121 80.20752
56.8689 83.14275
34.33312 55.72349
59.04974 77.63418
57.78822 99.05141
54.28233 79.12065
51.08872 69.5889
50.28284 69.5105
44.21174 73.68756
38.00549 61.3669
32.94048 67.17066
53.69164 85.6682
68.76573 114.8539
46.23097 90.12357
68.31936 97.91982
50.03017 81.53699
49.23977 72.11183
50.03958 85.23201
48.14986 66.22496

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

PROGRAM:

# Making imports import pandas as pd import numpy as np


import [Link] as plt
[Link]['[Link]'] = (12.0, 9.0)

# Preprocessing Input data


data = pd.read_csv("[Link]")
X = [Link][:, 0]
Y = [Link][:, 1]
[Link](X, Y)
[Link]()

#Building the model


X_mean = [Link](X)
Y_mean = [Link](Y)
num = 0
den = 0
for i in range(len(X)):
num += (X[i] - X_mean)*(Y[i] - Y_mean)
den += (X[i] - X_mean)**2
m = num/den
c = Y_mean - (m * X_mean)

print(m, c)
1.287357370010931 9.908606190326509

# Making predictions
Y_pred = m*X + c [Link](X, Y)
[Link](X, Y_pred, color='red')
# [Link]([min(X), max(X)], [min(Y_pred), max(Y_pred)], color='red')
[Link]()

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

RESULT:
Thus the Least Square Regression Model implemented successfully.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Ex. No.: 2
RULE BASED CLASSIFIER MODEL
Date:

AIM:
To Implement Rule Based Classifier Model.

PROBLEM STATEMENT:
Potential Customer Yield Calculation by Rule Based Classification
Problem
"A game company wants to create level-based new customer definitions (personas) by using
some features of its customers, and to create segments according to these new customer
definitions and to estimate how much the company can earn on average from new customers
based on these segments."
"For example: The company wants to determine how much a 25-year-old male user from
Turkey, who is an IOS user, can earn on average to the company."
Dataset
[Link] - data set contains the prices of the products sold by an international game
company and some demographic information of the users who buy these products. The data set
consists of records created in each sales transaction. This means that the table is not
singularized. In other words, a user with certain demographic characteristics may have made
more than one purchase.
DATASET:
[Link]

PROGRAM:
import pandas as pd
import [Link] as plt
import seaborn as sns
df = pd.read_csv("[Link]", encoding='utf-8')

def quick_info(dataframe):
print(" HEAD ")
print([Link](10))
print(" SHAPE ")
print([Link])
print(" COLUMNS ")
print([Link])
print(" DESRIBE ")
print([Link]().T)
print(" INFO ")
print([Link]())
print(" ANY NULL VALUES ")
print([Link]().[Link]())
print(" SUM OF NULL VALUES ")
print([Link]().sum())
quick_info(df)

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

def dataframe_summary(dataframe, cat_threshold = 5, card_threshold=15):


#Categorical Variables
cat_cols = [col for col in [Link] if str(dataframe[col].dtypes) in ["object"]]
#['SOURCE', 'SEX', 'COUNTRY']
num_but_cat = [col for col in [Link] if dataframe[col].nunique() < cat_threshold
and dataframe[col].dtypes in ["int"]]
#['PRICE']

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

cat_but_car = [col for col in [Link] if dataframe[col].nunique() > card_threshold


and str(dataframe[col].dtypes) in ["object"]]
#[]
cat_cols = cat_cols + num_but_cat #['SOURCE', 'SEX', 'COUNTRY', 'PRICE']
cat_cols = [col for col in cat_cols if col not in cat_but_car] #['SOURCE', 'SEX', 'COUNTRY',
'PRICE']
#Numerical Variables
num_cols = [col for col in [Link] if dataframe[col].dtypes in ['int']]
#['PRICE', 'AGE'] num_cols = [col for col in num_cols if col not in cat_cols] #['AGE']
print(f"Observations: {[Link][0]}\n", f"Variables: {[Link][1]}\n",
f'cat_cols:
{len(cat_cols)}\n', f'num_cols: {len(num_cols)}\n', f'cat_but_car: {len(cat_but_car)}\n',
f'num_but_cat: {len(num_but_cat)}')
return cat_cols, num_cols
cat_cols, num_cols = dataframe_summary(dataframe=df)
Observations: 5000
Variables: 5
cat_cols: 3
num_cols: 2
cat_but_car: 0
num_but_cat: 0
#Defined to get summary of categorical variables
def cat_summary(dataframe, col_name, plot=False):
print([Link]({col_name: dataframe[col_name].value_counts(), "Ratio":100 *
dataframe[col_name].value_counts() / len(dataframe)}))
if plot:
[Link](x=dataframe[col_name], data=dataframe) [Link]() [Link]()
#Defined to get summary of numerical variables
def num_summary(dataframe, numerical_col, plot=False): quantiles = [0, .25, .50, .75 ,1.]
print(dataframe[numerical_col].describe(quantiles).T)
if plot:
dataframe[numerical_col].hist() [Link](numerical_col) [Link](numerical_col) [Link]()
for col in cat_cols: cat_summary(df, col, plot=True)
SOURCE Ratio android 2974 59.48
ios 2026 40.52

SEX Ratio
female 2621 52.42
male 2379 47.58

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

COUNTRY Ratio
usa 2065 41.30
bra 1496 29.92
deu 455 9.10
tur 451 9.02
fra 303 6.06
can 230 4.60

for col in num_cols: num_summary(df, col, plot=True)

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

count 5000.000000
mean 23.581400
std 8.995908
min 15.000000

0% 15.000000
25% 17.000000
50% 21.000000
75% 27.000000
100% 66.000000
max 66.000000
Name: AGE, dtype: float64

Return average earnings in terms of break down of COUNTRY, SOURCE, SEX, AGE in
descending order
agg_df =
[Link](["COUNTRY","SOURCE","SEX","AGE"]).agg({"PRICE":'mean'}).sort_values('P
RIC E',ascending=False)
#Convert index names to variable names by resetting index agg_
df.reset_index(inplace=True)
agg_df
COUNTRY SOURCE SEX AGE PRICE
0 bra android male 46 59.0
1 usa android male 36 59.0
2 fra android female 24 59.0
3 usa ios male 32 54.0
4 deu android female 36 49.0
... ... ... ... ... ...
343 usa ios female 38 19.0
344 usa ios female 30 19.0
345 can android female 27 19.0
346 fra android male 18 19.0
347 deu android male 26 9.0

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

#Set intervals of AGE variable to be splitted


bins = [agg_df["AGE"].min(), 18, 23, 30, 40, agg_df["AGE"].max()]
#Set labels to correspond to splitted parts
labels = [str(agg_df["AGE"].min()) + '_18', '19_23', '24_30', '31_40', '41_' +
str(agg_df["AGE"].max())]
#Split AGE variable according to bins & Assign it to a new column in agg_df named
"AGE_CAT"

#inclue_lowest made the first interval be left inclusive which allowed us to get the minimum
value (which is 15)
agg_df["AGE_CAT"] = [Link](agg_df["AGE"], bins=bins, labels=labels,
include_lowest=True) """

NOTE: This code snippet returns min, max values according to grouped 'AGE_CAT' and
useful for giving consistent intervals on both cut & qcut. (in terms of labelling) """

agg_df.groupby("AGE_CAT").agg({"AGE":['min','max']})

AGE
min max

AGE_CAT
15_18 15 18
19_23 19 23
24_30 24 30
31_40 31 40
41_66 41 66

agg_df

COUNTRY SOURCE SEX AGE PRICE AGE_CAT


0 bra android male 46 59.0 41_66
1 usa android male 36 59.0 31_40
2 fra android female 24 59.0 24_30
3 usa ios male 32 54.0 31_40
4 deu android female 36 49.0 31_40
... ... ... ... ... ... ...
343 usa ios female 38 19.0 31_40
344 usa ios female 30 19.0 24_30
345 can android female 27 19.0 24_30
346 fra android male 18 19.0 15_18
347 deu android male 26 9.0 24_30

348 rows × 6 columns

#Requested to define new level based customers and add them as variables to the dataset.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

#Defined a new variable called 'CUSTOMER_LEVEL_BASED' & Assigned as a variable to


the dataset (agg_df) under the name "CUSTOMER_LEVEL_BASED"

#["COUNTRY","SOURCE","SEX","AGE_CAT"] columns are combined with "_" by column


axis by lambda function

agg_df['CUSTOMER_LEVEL_BASED'] =

agg_df[["COUNTRY","SOURCE","SEX","AGE_CAT"]].apply(lambda x:
"_".join([Link](str)), axis=1)

#Since it has been said before that this dataset is not singularized, we need to singularize it.
agg_df = agg_df.groupby("CUSTOMER_LEVEL_BASED").agg({"PRICE":'mean'})
agg_df.reset_index(inplace=True)
#We had to reset index since we merged different columns into one (i.e. 'Singularization
process')

agg_df["CUSTOMER_LEVEL_BASED"].value_counts()

#Proves that singularization process was successful


bra_android_female_15_18 1
tur_android_female_15_18 1
tur_ios_female_19_231
tur_ios_female_15_181
tur_android_male_41_66 1
..
can_ios_male_41_66 1
can_ios_male_31_40 1
can_ios_male_24_30 1
can_ios_male_15_18 1
usa_ios_male_41_66 1
Name: CUSTOMER_LEVEL_BASED, Length: 109, dtype: int64

agg_df
CUSTOMER_LEVEL_BASED PRICE
0 bra_android_female_15_18 35.645303
1 bra_android_female_19_23 34.077340
2 bra_android_female_24_30 33.863946
3 bra_android_female_31_40 34.898326
4 bra_android_female_41_66 36.737179
... ... ...
104 usa_ios_male_15_18 33.983495
105 usa_ios_male_19_23 34.901872
106 usa_ios_male_24_30 34.838143
107 usa_ios_male_31_40 36.206324
108 usa_ios_male_41_66 35.750000
109 rows × 2 columns

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

#Requested to split new customers into "SEGMENTS" according to "PRICE" & Assign to
agg_df with a new column named "SEGMENTS"
labels = ['A', "B", 'C', "D", 'E']
#NOTE: qcut creates unequal size bins but frequency of samples is equal in each bin
agg_df["SEGMENT"] = [Link](agg_df['PRICE'], 5, labels=labels[::-1])
agg_df.sort_values(by="PRICE", ascending=False)
#Indicates that the 'SEGMENT' distribution is made according to the 'PRICE' distribution.

CUSTOMER_LEVEL_BASED PRICE SEGMENT


54 fra_android_female_24_30 45.428571 A
86 tur_ios_male_24_30 45.000000 A
87 tur_ios_male_31_40 42.333333 A
72 tur_android_female_31_40 41.833333 A
25 can_android_male_19_23 40.111111 A
86 tur_ios_male_24_30 45.000000 A
87 tur_ios_male_31_40 42.333333 A
22 can_android_female_24_30 19.000000 E
41 deu_android_male_24_30 19.000000 E
72 tur_android_female_31_40 41.833333 A
25 can_android_male_19_23 40.111111 A
41 deu_android_male_24_30 19.000000 E
88 tur_ios_male_41_66 25.052632 E
85 tur_ios_male_19_23 24.833333 E
56 fra_android_male_15_18 24.625000 E
22 can_android_female_24_30 19.000000 E
41 deu_android_male_24_30 19.000000 E
109 rows × 3 columns
#Requested to classify the new customers and estimate how much income they can bring to the
company
def predict_income(country,device,sex,age):
"""
This function takes 4 parameters about the customer to be searched, respectively, country,
device, gender and age.
Next, first of all iterates over "CUSTOMER_LEVEL_BASED" to unpack, and second of all
iterates over it again to access each element of the "CUSTOMER_LEVEL_BASED" column.
Then, selects every row based on its index and splits with underscore in the text. Since age
intervals indices are known, they were called according to their indices and assigned to
min_age, max_age respectively. After that, query control made with age parameters and if it is
satisfied, combines each given element, performs requested query and returns prediction of the
given user.

GRT Institute of Engineering and Technology, Tiruttani Page |


| AD24405 – MACHINE LEARNING LABORATORY

Parameters
country: origin of the customer device: customer's device
sex: customer's gender
age: customer's age Returns

new_customer: prediction of the given user

Note
It is possible to do more checks before querying the input (For example, it could be extended
by checking whether all entries given in the dataset are valid)
"""
for _ in agg_df["CUSTOMER_LEVEL_BASED"]:
for ind, line in enumerate(agg_df["CUSTOMER_LEVEL_BASED"]):
min_age = agg_df['CUSTOMER_LEVEL_BASED'][ind].split('_')[3]
max_age = agg_df['CUSTOMER_LEVEL_BASED'][ind].split('_')[4]

if min_age <= age <= max_age:


new_customer = '_'.join([[Link](), [Link](), [Link](),
min_age, max_age])

return agg_df[agg_df['CUSTOMER_LEVEL_BASED'] == new_customer]


#Which segment does a 33-year-old Turkish female using ANDROID belong to and how much
income is expected to earn on average?
predict_income("tur",'ios','female', '33')
CUSTOMER_LEVEL_BASED PRICE SEGMENT
82 tur_ios_female_31_40 32.333333 D
#Which segment does a 35-year-old French female using IOS belong to and how much income
is expected to earn on average?
predict_income("fra",'ios','female', '35')
CUSTOMER_LEVEL_BASED PRICE SEGMENT
63 fra_ios_female_31_40 32.818182 D

#Which segment does a 20-year-old American male using IOS belong to and how much
income is expected to earn on average?
predict_income("usa",'ios','male', '20')
CUSTOMER_LEVEL_BASED PRICE SEGMENT

105 usa_ios_male_19_23 34.901872 B

#Which segment does a 55-year-old Canadian male using ANDROID belong to and how much
income is expected to earn on average?
predict_income("can",'android','male', '50')
CUSTOMER_LEVEL_BASED PRICE SEGMENT

27 can_android_male_41_66 37.571429 A

RESULT:
Thus the Rule based Classifier model was implemented successfully.

GRT Institute of Engineering and Technology, Tiruttani Page |

You might also like