# How to implement Naive Bayes classifier

I am working on implementing a Naive Bayes Classification algorithm. The problem requires classifying the following datasets:

Venue,color,Model,Category,Location,weight,Veriety,Material,Volume
1,6,4,4,4,1,1,1,6
2,5,4,4,4,2,6,1,1
1,6,2,1,4,1,4,2,4
1,6,2,1,4,1,2,1,2
2,6,5,5,5,2,2,1,2
1,5,4,4,4,1,6,2,2
1,3,3,3,3,1,6,2,2
1,5,2,1,1,1,2,1,2
1,4,4,4,1,1,5,3,6
1,4,4,4,4,1,6,4,6
2,5,4,4,4,2,4,4,1
2,4,3,3,3,2,1,1,1

Venue,color,Model,Category,Location,weight,Veriety,Material,Volume
2,6,4,4,4,2,2,1,1
1,2,4,4,4,1,6,2,6
1,5,4,4,4,1,2,1,6
2,4,4,4,4,2,6,1,4
1,4,4,4,4,1,2,2,2
2,4,3,3,3,2,1,1,1
1,5,2,1,4,1,6,2,6
1,2,3,3,3,1,2,1,6
2,6,4,4,4,2,3,1,1
1,4,4,4,4,1,2,1,6
1,5,4,4,4,1,2,1,4
1,4,5,5,5,1,6,2,4
2,5,4,4,4,2,3,1,1


The code that I am using for the implementation is

from numpy.core.defchararray import count
import pandas as pd
import numpy as np
import math
from sklearn.decomposition import PCA
from numpy import linalg as LA
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB

test_set_Bayes = pd.read_csv("Assignment 2--Training set for Bayes.csv")
training_set_Bayes = pd.read_csv("Assignment 2--Test set for Bayes.csv")

def prob(x, attribute, dataset):
# calcuulate the probability of the x for the given dataset
D = len(dataset[attribute].index)
valueCountX = len(dataset[dataset[attribute] == x].index)
return valueCountX/D

def prob_product(A, dataset, x):
# calculate prodcut of each record in the dataset for Ai attrbutes with class = x
return

def prob_continous_value(A, classAttribute, dataset, x):
# calcuate the average for all values of A in dataset with class = x
a = dataset[dataset[classAttribute] == x][A].mean()
# calculate the standard deviation for all values A in dataset with class = x
stdev = dataset[dataset[classAttribute] == x][A].std()
v = dataset[A].iloc[0]
p = math.sqrt(2*math.pi)*math.exp((v-a)*(v-a)/(2*stdev*stdev))
return p

def valueIsNotContinuous(A,dataset):
# check if value is continuous or not
x = dataset[A].iloc[0]
return type(x) == int or type(x) == float

def BayesClassifier(training_set,test_set):
classAttribute = 'Volume'
for x in training_set[classAttribute].unique():
for A, values in training_set.iteritems():
if not A == classAttribute:
p = None
prob_product = None
if(valueIsNotContinuous(A,training_set)):
p = prob(A, training_set, x)
print(f'p({x}) = {p}')
else:
p = prob_continous_value(A, classAttribute, training_set, x)
print(f'p({A}|{x}) = {p}')

prob_product = (A,training_set,x)

# prompt user to select either ID3 or Bayes classifier.
selection = "Bayes" #= input("Please enter your selection for either ID3 or Bayes classification: ")

if(selection == "Bayes"):
BayesClassifier(training_set_Bayes,test_set_Bayes)


The goal of this program is to classify the data based on the classAttribute Volume. Given a recorrd x I'd like to predict the class value for Volume. The

MAX(P(Volume=6|x),P(Volume=5|x),P(Volume=4|x),P(Volume=3|x),P(Volume=2|x),P(Volume=1|x))

Determines the class for x.

Step 1 is to calculate the probability for P(Volume=6| x) = P(Volume= 6) productOfProbabilitiesInDataSet(Ai = v | Volume = 6)/P(x)

Ai is the ith attrtibute of x, v is the value of Ai.

For example

productOfProbabilitiesInDataSet(Ai = v | Volume = 6) = P(Venue="1"|Volume="6")*P(Color="6"| Volume=6)*P(Model="1"| Volume=6)....

****** If the value of the attribute is a continuous value then use the probability density function, in this case they are all continuous values *****

The formula for the continuous value function requires

1. The average and the standard deviation for attribute value where the class value is equal to 6 for example.

2. The formula is 1/sqrt(2*pi)stdv * exp(attributeValue - average)^2/ (2stdv^2)

I have stubbed out the method for each of these functions, but I'd like to get feedback and advice on how to do this with pandas. Any help would be greatly appreciated. Thank you.

This produces the winner for classification

from numpy.core.defchararray import count, index
import pandas as pd
import numpy as np
import math
from sklearn.decomposition import PCA
from numpy import linalg as LA
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB

test_set_Bayes = pd.read_csv("Assignment 2--Training set for Bayes.csv")
training_set_Bayes = pd.read_csv("Assignment 2--Test set for Bayes.csv")

def prob_continous_value(A, v, classAttribute, dataset, x):
# calcuate the average for all values of A in dataset with class = x
a = dataset[dataset[classAttribute] == x][A].mean()
# calculate the standard deviation for all values A in dataset with class = x
stdev = 1
stdev = dataset[dataset[classAttribute] == x][A].std()
v = dataset[A].iloc[0]
if stdev == 0.0:
stdev = 0.00000000000001
return (1/(math.sqrt(2*math.pi)*stdev))*math.exp(-((v-a)*(v-a))/(2*stdev*stdev))

def BayesClassifier(training_set,test_set):
classAttribute = 'Volume'
products = []
max = -math.inf
classWithMaxValue = ""
for x in training_set[classAttribute].unique():
D = len(training_set[classAttribute].index)
d = len(training_set[training_set[classAttribute] == x].index)
pClassAttribute = d/D
print("********")
print(f'Step 1 calculate p({classAttribute}={x})={pClassAttribute}')
p = 0
probabilitiesProduct = 1
print("********")
print("Step 2 calculate product of probabilities")
for A, values in training_set.iteritems():
if not A == classAttribute:
v = training_set[A].iloc[0]
p = prob_continous_value(A, v, classAttribute, training_set, x)
print(f'p({A}={v}|{classAttribute}={x})={p}')
probabilitiesProduct *= p
print(f"probabilitiesProduct={probabilitiesProduct}")
print("********")
# products.append(probabilitiesProduct)
ptotal = pClassAttribute*probabilitiesProduct
print(f'p({classAttribute}={x}|x)={ptotal}')
if ptotal > max:
max = ptotal
classWithMaxValue = x
print(f"winner is {classAttribute}={classWithMaxValue}")

# prompt user to select either ID3 or Bayes classifier.
selection = "Bayes" #= input("Please enter your selection for either ID3 or Bayes classification: ")

if(selection == "Bayes"):
BayesClassifier(training_set_Bayes,test_set_Bayes)

$$$$
`