-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaive_bayes_test.py
67 lines (53 loc) · 1.88 KB
/
naive_bayes_test.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
#!/usr/bin/env python
# Naive Bayes Implementation
from mllib.utils import *
import csv
from mllib.naive_bayes.Pool import Pool
import os, sys
def report(correctCount, wrongCount):
print("Total Cases tested : %d"%(correctCount + wrongCount))
print("Correct cases", correctCount)
print("Wrong cases ", wrongCount)
print("Accuracy: ", correctCount*1.0/ (correctCount + wrongCount)*1.0)
def naive_bayes():
correctCount = 0
wrongCount = 0
for label in labels:
if os.path.isdir(base_dir + label):
dir = os.listdir(base_dir + label)
for file in dir:
res = p.probability(base_dir + label + "/" + file)
if label == res[0][0]:
correctCount += 1
else:
wrongCount += 1
#print( label + ": " + file + ": " + str(res))
return (correctCount, wrongCount)
def logistics_regression():
correctCount = 0
wrongCount = 0
p.train(labels[0])
for label in labels:
if os.path.isdir(base_dir + label):
dir = os.listdir(base_dir + label)
for file in dir:
res = p.regression(base_dir + label + "/" + file, labels[0], labels[1])
if label == res[0][0]:
correctCount += 1
else:
wrongCount += 1
#print( label + ": " + file + ": " + str(res))
return (correctCount, wrongCount)
if __name__ == '__main__':
labels = ["spam", "ham"]
base_dir = "data/train/"
p = Pool()
for label in labels:
p.learn(base_dir + label, label)
base_dir = "data/test/"
stop_words = "data/stopwords_en.txt"
p.read_stop_words(stop_words)
#(correctCount, wrongCount) = naive_bayes()
#report(correctCount, wrongCount)
(correctCount, wrongCount) = logistics_regression()
report(correctCount, wrongCount)