0% found this document useful (0 votes)
11 views3 pages

3 Naive Bayes Model

The document outlines a Python program to implement the Naive Bayes model, detailing the steps from data loading and preprocessing to model training and evaluation. It includes code snippets for functions such as data encoding, splitting, and calculating probabilities, culminating in a model accuracy of 100%. The program successfully demonstrates the application of the Naive Bayes algorithm on a dataset.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

3 Naive Bayes Model

The document outlines a Python program to implement the Naive Bayes model, detailing the steps from data loading and preprocessing to model training and evaluation. It includes code snippets for functions such as data encoding, splitting, and calculating probabilities, culminating in a model accuracy of 100%. The program successfully demonstrates the application of the Naive Bayes algorithm on a dataset.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EX.

NO:3 IMPLEMENT NAIVE BAYES

Aim:
To write a python program to implement Naive Bayes model.

Algorithm:

1. Load the libraries: import the required libraries such as pandas, numpy, and sklearn.
2. Load the data into a pandas dataframe.
3. Clean and preprocess the data as necessary. For example, you can handle missing values,
convert categorical variables into numerical variables, and normalize the data.
4. Split the data into training and test sets using the train_test_split function from scikit-learn.
5. Train the Gaussian Naive Bayes model using the training data.
6. Evaluate the performance of the model using the test data and the accuracy_score
function from scikit-learn.
7. Finally, you can use the trained model to make predictions on new data.

Program:

import math
import random
import pandas as pd
import numpy as np
def encode_class(mydata):
classes = []
for i in range(len(mydata)):
if mydata[i][-1] not in classes:
classes.append(mydata[i][-1])
for i in range(len(classes)):
for j in range(len(mydata)):
if mydata[j][-1] == classes[i]:
mydata[j][-1] = i
return mydata
def splitting(mydata, ratio):
train_num = int(len(mydata) * ratio)
train = []
test = list(mydata)
while len(train) < train_num:
index = random.randrange(len(test))
train.append(test.pop(index))
return train, test
def groupUnderClass(mydata):
data_dict = {}
for i in range(len(mydata)):
if mydata[i][-1] not in data_dict:
data_dict[mydata[i][-1]] = []
data_dict[mydata[i][-1]].append(mydata[i])
return data_dict
def MeanAndStdDev(numbers):
avg = np.mean(numbers)
stddev = np.std(numbers)
return avg, stddev

def MeanAndStdDevForClass(mydata):
info = {}
data_dict = groupUnderClass(mydata)
for classValue, instances in data_dict.items():
info[classValue] = [MeanAndStdDev(attribute) for attribute in zip(*instances)]
return info
def calculateGaussianProbability(x, mean, stdev):
epsilon = 1e-10
expo = math.exp(-(math.pow(x - mean, 2) / (2 * math.pow(stdev + epsilon, 2))))
return (1 / (math.sqrt(2 * math.pi) * (stdev + epsilon))) * expo

def calculateClassProbabilities(info, test):


probabilities = {}
for classValue, classSummaries in info.items():
probabilities[classValue] = 1
for i in range(len(classSummaries)):
mean, std_dev = classSummaries[i]
x = test[i]
probabilities[classValue] *= calculateGaussianProbability(x, mean, std_dev)
return probabilities
def predict(info, test):
probabilities = calculateClassProbabilities(info, test)
bestLabel = max(probabilities, key=probabilities.get)
return bestLabel

def getPredictions(info, test):


predictions = [predict(info, instance) for instance in test]
return predictions
def accuracy_rate(test, predictions):
correct = sum(1 for i in range(len(test)) if test[i][-1] == predictions[i])
return (correct / float(len(test))) * 100.0
df = pd.read_csv('heart.csv')
mydata = df.values.tolist()

# Encode classes and convert attributes to float


mydata = encode_class(mydata)
for i in range(len(mydata)):
for j in range(len(mydata[i]) - 1):
mydata[i][j] = float(mydata[i][j])
ratio = 0.7
train_data, test_data = splitting(mydata, ratio)

print('Total number of examples:', len(mydata))


print('Training examples:', len(train_data))
print('Test examples:', len(test_data))
info = MeanAndStdDevForClass(train_data)
# Test the model
predictions = getPredictions(info, test_data)
accuracy = accuracy_rate(test_data, predictions)
print('Accuracy of the model:', accuracy)

Output:
Total number of examples: 1025
Training examples: 717
Test examples: 308
Accuracy of the model: 100.0

Result:
Thus the Python program for implementing Naive Bayes model was developed and
the output was verified successfully.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy