-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gaussian_Naive_Bayes_Algorithm.py
107 lines (86 loc) · 2.52 KB
/
Gaussian_Naive_Bayes_Algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import math
file = open('TrainingSet.csv', 'r')
training = file.readlines()
for i in range(0, len(training)):
training[i] = training[i][0:-1]
training[i] = training[i].split(',')
MeanHM = 0.0
MeanHW = 0.0
MeanWM = 0.0
MeanWW = 0.0
MeanAM = 0.0
MeanAW = 0.0
VarHM = 0.0
VarHW = 0.0
VarWM = 0.0
VarWW = 0.0
VarAM = 0.0
VarAW = 0.0
TM = 0.0
TW = 0.0
for i in range(0, len(training)):
if training[i][3] == 'M':
TM += 1
MeanHM += int(training[i][0])
MeanWM += int(training[i][1])
MeanAM += int(training[i][1])
else:
TW += 1
MeanHW += int(training[i][0])
MeanWW += int(training[i][1])
MeanAW += int(training[i][1])
PM = TM / len(training)
PW = TW / len(training)
MeanHM = MeanHM / TM
MeanHW = MeanHW / TW
MeanWM = MeanWM / TM
MeanWW = MeanWW / TW
MeanAM = MeanAM / TM
MeanAW = MeanAW / TW
for i in range(0, len(training)):
if training[i][3] == 'M':
VarHM += pow((int(training[i][0]) - MeanHM), 2)
VarWM += pow((int(training[i][1]) - MeanWM), 2)
VarAM += pow((int(training[i][2]) - MeanAM), 2)
else:
VarHW += pow((int(training[i][0]) - MeanHW), 2)
VarWW += pow((int(training[i][1]) - MeanWW), 2)
VarAW += pow((int(training[i][2]) - MeanAW), 2)
VarHM /= (TM-1)
VarWM /= (TM-1)
VarAM /= (TM-1)
VarHW /= (TW-1)
VarWW /= (TW-1)
VarAW /= (TW-1)
CHM = (1 / math.sqrt(2*math.pi*VarHM))
CWM = (1 / math.sqrt(2*math.pi*VarWM))
CAM = (1 / math.sqrt(2*math.pi*VarAM))
CHW = (1 / math.sqrt(2*math.pi*VarHW))
CWW = (1 / math.sqrt(2*math.pi*VarWW))
CAW = (1 / math.sqrt(2*math.pi*VarAW))
CommonM = math.log(PM, math.e)+math.log(CHM, math.e) + \
math.log(CWM, math.e)+math.log(CAM, math.e)
CommonW = math.log(PW, math.e)+math.log(CHW, math.e) + \
math.log(CWW, math.e)+math.log(CAW, math.e)
condition = True
while condition:
M = 0
W = 0
case = input("1: Gaussian Naive Bayes")
if case == '1':
print("Enter data for prediction.")
h = int(input("Height : "))
w = int(input("Weight : "))
a = int(input("Age : "))
exp = (-0.5)*((pow((h-MeanHM), 2)/VarHM) +
(pow((w-MeanWM), 2)/VarWM) + (pow((a-MeanAM), 2)/VarAM))
ProbM = CommonM+exp
exp = (-0.5)*((pow((h-MeanHW), 2)/VarHW) +
(pow((w-MeanWW), 2)/VarWW) + (pow((a-MeanAW), 2)/VarAW))
ProbW = CommonW+exp
if ProbM > ProbW:
print("\n\nClass : M\n\n")
else:
print("\n\nClass : W\n\n")
else:
print("Incorrect choice.")