3 Naive Bayes Model
3 Naive Bayes Model
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
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.