Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
26 views
2 pages
Ex 6
Uploaded by
senthil7111
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download
Save
Save Ex-6 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
26 views
2 pages
Ex 6
Uploaded by
senthil7111
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save Ex-6 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save Ex-6 For Later
You are on page 1
/ 2
Search
Fullscreen
Ex.No.
6 Naïve Bayesian Classifier
############### PROGRAM ###############
import csv
import random
import math
def loadcsv(filename):
with open(filename,"r") as file:
lines = csv.reader(file)
dataset = list(lines)
dataset = dataset[1:]
dataset = [[float(x)for x in row]for row in dataset]
return dataset
def splitdataset(dataset, splitratio):
trainsize = int(len(dataset) * splitratio);
trainset = []
copy = list(dataset);
while len(trainset) <trainsize:
index = random.randrange(len(copy));
trainset.append(copy.pop(index))
return [trainset, copy]
def separatebyclass(dataset):
separated = {}
for i in range(len(dataset)):
vector = dataset[i]
if (vector[-1] not in separated):
separated[vector[-1]] = []
separated[vector[-1]].append(vector)
return separated
def mean(numbers):
return sum(numbers)/float(len(numbers))
def stdev(numbers):
avg = mean(numbers)
variance = sum([pow(x-avg,2) for x in numbers])/float(len(numbers)-1)
return math.sqrt(variance)
def summarize(dataset):
summaries = [(mean(attribute), stdev(attribute)) for attribute in
zip(*dataset)];
del summaries[-1]
return summaries
def summarizebyclass(dataset):
separated = separatebyclass(dataset);
summaries = {}
for classvalue, instances in separated.items():
summaries[classvalue] = summarize(instances)
return summaries
def calculateprobability(x, mean, stdev):
exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(stdev,2))))
return (1 / (math.sqrt(2*math.pi) * stdev)) * exponent
def calculateclassprobabilities(summaries, inputvector):
probabilities = {}
for classvalue, classsummaries in summaries.items():
probabilities[classvalue] = 1
for i in range(len(classsummaries)):
mean, stdev = classsummaries[i]
x = inputvector[i]
probabilities[classvalue] *= calculateprobability(x, mean, stdev)
return probabilities
def predict(summaries, inputvector):
probabilities = calculateclassprobabilities(summaries, inputvector)
bestLabel, bestProb = None, -1
for classvalue, probability in probabilities.items():
if bestLabel is None or probability >bestProb:
bestProb = probability
bestLabel = classvalue
return bestLabel
def getpredictions(summaries, testset):
predictions = []
for i in range(len(testset)):
result = predict(summaries, testset[i])
predictions.append(result)
return predictions
def getaccuracy(testset, predictions):
correct = 0
for i in range(len(testset)):
if testset[i][-1] == predictions[i]:
correct += 1
return (correct/float(len(testset))) * 100.0
def main():
filename = 'D:\\New folder\\Ex6.csv'
splitratio = 0.67
dataset = loadcsv(filename)
trainingset, testset = splitdataset(dataset, splitratio)
print('Split {0} rows into train={1} and test={2} rows'.format(len(dataset),
len(trainingset), len(testset)))
summaries = summarizebyclass(trainingset);
predictions = getpredictions(summaries, testset)
accuracy = getaccuracy(testset, predictions)
print('Accuracy of the classifier is : {0}%'.format(accuracy))
main()
############### OUTPUT ###############
Split 9 rows into train=6 and test=3 rows
Accuracy of the classifier is : 33.33333333333333%
You might also like
NEXIV Tutorial en
PDF
No ratings yet
NEXIV Tutorial en
84 pages
Operating-System Structures: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts - 9 Edition
PDF
No ratings yet
Operating-System Structures: Silberschatz, Galvin and Gagne ©2013 Operating System Concepts - 9 Edition
28 pages
Uninstall and Remove Peachtree Complete 2010 Step by Step
PDF
No ratings yet
Uninstall and Remove Peachtree Complete 2010 Step by Step
16 pages
Quantum Users Guide-3
PDF
100% (2)
Quantum Users Guide-3
209 pages
Sicam Power Quality and Measurement: Catalog
PDF
100% (1)
Sicam Power Quality and Measurement: Catalog
42 pages
Hci-Unit 1
PDF
No ratings yet
Hci-Unit 1
228 pages
Hci-Unit 2
PDF
No ratings yet
Hci-Unit 2
190 pages
Shuttle - A14hv0x Rev 4.0 - 71r-A14hv6-T840
PDF
No ratings yet
Shuttle - A14hv0x Rev 4.0 - 71r-A14hv6-T840
40 pages
20ec755 Unit 3 Notes
PDF
No ratings yet
20ec755 Unit 3 Notes
21 pages
Introduction To Python Programming
PDF
No ratings yet
Introduction To Python Programming
69 pages
Program 6 Naive Bayes Classifier
PDF
No ratings yet
Program 6 Naive Bayes Classifier
2 pages
IGCSE Cs Lesson Plan
PDF
No ratings yet
IGCSE Cs Lesson Plan
8 pages
m1 Python Notes
PDF
No ratings yet
m1 Python Notes
51 pages
IRis
PDF
No ratings yet
IRis
19 pages
Assignment
PDF
No ratings yet
Assignment
5 pages
AI and ML Lab Ex3 To 12
PDF
No ratings yet
AI and ML Lab Ex3 To 12
27 pages
Programs Lab Bca
PDF
No ratings yet
Programs Lab Bca
16 pages
Unit 1
PDF
No ratings yet
Unit 1
34 pages
Unit 2
PDF
No ratings yet
Unit 2
54 pages
Null 0
PDF
No ratings yet
Null 0
6 pages
Cse Machine Learning Lab Manual
PDF
No ratings yet
Cse Machine Learning Lab Manual
22 pages
ML All Projectpdf Removed
PDF
No ratings yet
ML All Projectpdf Removed
41 pages
ML - LAB - 7 - Jupyter Notebook
PDF
100% (1)
ML - LAB - 7 - Jupyter Notebook
7 pages
ML Lab Record
PDF
No ratings yet
ML Lab Record
33 pages
Bacdeaf 23032025 115708 Split 1
PDF
No ratings yet
Bacdeaf 23032025 115708 Split 1
37 pages
Module II Python Notes
PDF
No ratings yet
Module II Python Notes
44 pages
ML Practical 205160694034
PDF
No ratings yet
ML Practical 205160694034
33 pages
ML Lab Manual PDF
PDF
No ratings yet
ML Lab Manual PDF
9 pages
Aiml Lab
PDF
No ratings yet
Aiml Lab
14 pages
Machine File
PDF
No ratings yet
Machine File
27 pages
Agile E1 (CBO) 60566
PDF
100% (1)
Agile E1 (CBO) 60566
2 pages
Advance Machine Learning
PDF
No ratings yet
Advance Machine Learning
28 pages
Aiml Practical
PDF
No ratings yet
Aiml Practical
17 pages
Department of Computer Engineering Academic Term: June-Nov 2021
PDF
No ratings yet
Department of Computer Engineering Academic Term: June-Nov 2021
6 pages
ML Lab PT
PDF
No ratings yet
ML Lab PT
25 pages
Introduction To SCada
PDF
No ratings yet
Introduction To SCada
18 pages
MACHINE LEARNING Manual
PDF
No ratings yet
MACHINE LEARNING Manual
36 pages
1
PDF
No ratings yet
1
13 pages
23BCE7199 ML Lab Assignment
PDF
No ratings yet
23BCE7199 ML Lab Assignment
15 pages
LAB-4 Report
PDF
No ratings yet
LAB-4 Report
21 pages
Unit 5
PDF
No ratings yet
Unit 5
25 pages
Atul MLT Exp 4-11
PDF
No ratings yet
Atul MLT Exp 4-11
17 pages
ML Lab Programs
PDF
No ratings yet
ML Lab Programs
18 pages
Unit 2 DBMS
PDF
No ratings yet
Unit 2 DBMS
35 pages
ML Lab P-1
PDF
No ratings yet
ML Lab P-1
10 pages
Cloud Enterprise Architecture
PDF
No ratings yet
Cloud Enterprise Architecture
3 pages
Naive Bayes Classification
PDF
No ratings yet
Naive Bayes Classification
8 pages
2 LS Nav - Setup
PDF
No ratings yet
2 LS Nav - Setup
41 pages
DM ML Practical
PDF
No ratings yet
DM ML Practical
13 pages
Gotive H42 Advanced User's Guide v1
PDF
No ratings yet
Gotive H42 Advanced User's Guide v1
23 pages
1st PGM
PDF
No ratings yet
1st PGM
10 pages
PCS - Process Control System ILTIS-PCS - Sistema Control de Procesos
PDF
No ratings yet
PCS - Process Control System ILTIS-PCS - Sistema Control de Procesos
9 pages
Pattern Recognition
PDF
No ratings yet
Pattern Recognition
26 pages
Aiml Programs
PDF
No ratings yet
Aiml Programs
12 pages
Import Numpy As NP
PDF
No ratings yet
Import Numpy As NP
4 pages
8&9 Assignment ADS
PDF
No ratings yet
8&9 Assignment ADS
20 pages
Remaining ML Program
PDF
No ratings yet
Remaining ML Program
12 pages
AI Assignment-6
PDF
No ratings yet
AI Assignment-6
7 pages
Btech1007022 Lab5.1
PDF
No ratings yet
Btech1007022 Lab5.1
9 pages
ML Lab Programs For Exam
PDF
No ratings yet
ML Lab Programs For Exam
10 pages
Implementation of Boolean Function Using 8:1 Multiplexer: Title of Project Report
PDF
No ratings yet
Implementation of Boolean Function Using 8:1 Multiplexer: Title of Project Report
15 pages
ML File
PDF
No ratings yet
ML File
13 pages
Machine Learning Lab New
PDF
No ratings yet
Machine Learning Lab New
14 pages
ML Lab Manual
PDF
No ratings yet
ML Lab Manual
12 pages
P 7
PDF
No ratings yet
P 7
5 pages
Naive Bayes Classifier 066
PDF
No ratings yet
Naive Bayes Classifier 066
14 pages
Module III NEW Python Notes
PDF
No ratings yet
Module III NEW Python Notes
8 pages
Machine Learning Laboratory (21AIL66)
PDF
No ratings yet
Machine Learning Laboratory (21AIL66)
7 pages
ML Lab Experiments (1) - Pages-3
PDF
No ratings yet
ML Lab Experiments (1) - Pages-3
11 pages
A Survey Paper On: Gmail API Services and Importing PDF'S.: Authors
PDF
No ratings yet
A Survey Paper On: Gmail API Services and Importing PDF'S.: Authors
15 pages
Step Broucher
PDF
No ratings yet
Step Broucher
16 pages
Dip Paper Ans
PDF
No ratings yet
Dip Paper Ans
8 pages
3 Naive Bayes Model
PDF
No ratings yet
3 Naive Bayes Model
3 pages
Ai Last 5
PDF
No ratings yet
Ai Last 5
4 pages
Naive Bayes Gaussian Table Tennis - Jupyter Notebook
PDF
No ratings yet
Naive Bayes Gaussian Table Tennis - Jupyter Notebook
6 pages
Introduction To Distributed Systems (DS) : INF5040/9040 Autumn 2014
PDF
No ratings yet
Introduction To Distributed Systems (DS) : INF5040/9040 Autumn 2014
14 pages
Aiml Ex 4-7
PDF
No ratings yet
Aiml Ex 4-7
8 pages
ML Lab
PDF
No ratings yet
ML Lab
7 pages
Mllabprog 5
PDF
No ratings yet
Mllabprog 5
6 pages
Siemens Case Study On AgilePoint
PDF
No ratings yet
Siemens Case Study On AgilePoint
1 page
Naive
PDF
No ratings yet
Naive
5 pages
Cfgpro01 PDF
PDF
No ratings yet
Cfgpro01 PDF
10 pages
Department of Computer Engineering: Experiment No.6
PDF
No ratings yet
Department of Computer Engineering: Experiment No.6
5 pages
Fuzzing With AFL Fuzz A Practical Example (AFL Vs Binutils)
PDF
No ratings yet
Fuzzing With AFL Fuzz A Practical Example (AFL Vs Binutils)
5 pages
Exp 5
PDF
No ratings yet
Exp 5
4 pages
Ex 3
PDF
No ratings yet
Ex 3
5 pages
Naivebayes Labprg2
PDF
No ratings yet
Naivebayes Labprg2
3 pages
ML Lab
PDF
No ratings yet
ML Lab
7 pages
The Exact lwm2m Request That Is Being Sent Like 3/4/1 Format N What Each Value Represents
PDF
No ratings yet
The Exact lwm2m Request That Is Being Sent Like 3/4/1 Format N What Each Value Represents
5 pages
Ex 4
PDF
No ratings yet
Ex 4
2 pages
Example - 1
PDF
No ratings yet
Example - 1
5 pages
Cisco - Phone - 7945, 7965, 7975 Factory Reset Procedure
PDF
No ratings yet
Cisco - Phone - 7945, 7965, 7975 Factory Reset Procedure
2 pages
Fall Semester 2020-21 AI With Python ECE-4031
PDF
No ratings yet
Fall Semester 2020-21 AI With Python ECE-4031
5 pages
Assignment#3 (Naive Bayes)
PDF
No ratings yet
Assignment#3 (Naive Bayes)
5 pages
Kamil Gryciuk CV
PDF
No ratings yet
Kamil Gryciuk CV
2 pages
Flyer CYSTO Engl.
PDF
No ratings yet
Flyer CYSTO Engl.
2 pages
FortiGate 40C
PDF
No ratings yet
FortiGate 40C
2 pages