-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaiveBayesClassifier.py
148 lines (113 loc) · 3.58 KB
/
naiveBayesClassifier.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# author: Shamiul Hasan
import math
with open('train.txt') as f:
lines = [line.rstrip() for line in f]
first_line = lines[0].split()
numberOfFeatures = int(first_line[0])
numberOfClasses = int(first_line[1])
numberOfSamples = int(first_line[2])
print('input: ', lines)
dataset = []
for line in lines[1:]:
line = line.split()
data = []
for number in line:
data.append(float(number))
dataset.append(data)
print('dataset: ', dataset)
uniqueClassList = []
for row in dataset:
uniqueClassList.append(int(row[len(row) - 1]))
uniqueClassList = list(set(uniqueClassList))
uniqueClassNo = len(uniqueClassList)
print('Unique Classes: ', uniqueClassList)
totalSampleNo = len(dataset)
p_class = dict()
for className in uniqueClassList:
classCount = 0
for row in dataset:
if row[len(row) - 1] == className:
classCount += 1
p_class[className] = classCount / totalSampleNo
print('priori : ', p_class)
# featureIdx = 0
mean_dict = dict()
for className in uniqueClassList:
if className not in mean_dict:
mean_dict[className] = dict()
for featureIdx in range(numberOfFeatures):
# if className not in mean_dict:
# mean_dict[className][featureIdx] = dict()
count = 0
sum = 0
mean = 0
for row in dataset:
if row[len(row) - 1] == className:
count += 1
sum += row[featureIdx]
mean = sum / count
mean_dict[className][featureIdx] = mean
print('mean dict: ', mean_dict)
sigma_dict = dict()
for className in uniqueClassList:
if className not in sigma_dict:
sigma_dict[className] = dict()
for featureIdx in range(numberOfFeatures):
count = 0
sum = 0
mean = 0
for row in dataset:
if row[len(row) - 1] == className:
count += 1
# print(mean_dict[className][featureIdx])
# print('jlsd')
sum = sum + ((row[featureIdx] - mean_dict[className][featureIdx]) * (
row[featureIdx] - mean_dict[className][featureIdx]))
mean = sum / count
mean = math.sqrt(mean)
sigma_dict[className][featureIdx] = mean
print('sigma dict: ', sigma_dict)
##################
# test
with open('test.txt') as f:
lines = [line.rstrip() for line in f]
print('test set: ', lines)
dataset_test = []
for line in lines:
line = line.split()
data = []
for number in line:
data.append(float(number))
dataset_test.append(data)
print('dataset: ', dataset_test)
def gaussian(className, featureIdx, featureVal):
p = 1 / math.sqrt(2 * math.pi * sigma_dict[className][featureIdx] ** 2)
p *= (math.exp(
-((featureVal - mean_dict[className][featureIdx]) ** 2) / (2 * (sigma_dict[className][featureIdx] ** 2))))
return p
print('Result Probability: ')
ans = []
total_test_sample = len(dataset_test)
count = 0
all = []
right = 0
wrong = 0
for row in dataset_test:
ans.clear()
for className in uniqueClassList:
mul = 1
for featureIdx in range(int(numberOfFeatures)):
featureVal = row[featureIdx]
mul *= gaussian(className, featureIdx, featureVal)
mul *= p_class[className]
ans.append(mul)
pd = 1 + ans.index(max(ans))
all.append(pd)
if pd == dataset_test[count][len(row) - 1]:
right += 1
else:
wrong += 1
print('Predicted Class for ', count, ': ', 1 + ans.index(max(ans)))
count += 1
print('right percentage: ', right * 100 / (right + wrong))
print('wrong percentage: ', wrong * 100 / (right + wrong))