-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
136 lines (97 loc) · 3.27 KB
/
main.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
import operator
from math import log
M = 1
def importData(file):
"""
This function imports the data into a list form a file name passed as an argument.
The file should only the data seperated by a space.
"""
hamData = {}
spamData = {}
vocabulary = {}
ham = 0
spam = 0
f = open(str(file), 'r')
for line in f:
current = line.split()
if current[1] == 'ham':
ham += 1
for i in range(2, len(current), 2):
vocabulary[current[i]] = 1
if hamData.has_key(current[i]):
hamData[current[i]] = int(hamData.get(current[i])) + int(current[i+1])
else:
hamData[current[i]] = int(current[i+1])
elif current[1] == 'spam':
spam += 1
for i in range(2, len(current), 2):
vocabulary[current[i]] = 1
if spamData.has_key(current[i]):
spamData[current[i]] = int(spamData.get(current[i])) + int(current[i+1])
else:
spamData[current[i]] = int(current[i+1])
f.close()
Pham = float(ham) / (ham + spam)
Pspam = float(spam) / (ham + spam)
print ham + spam
print Pham
print Pspam
return hamData, spamData, vocabulary, ham, spam
def getConditionalProbablities(total, hamData, spamData):
hamProbablity ={}
spamProbablity ={}
totalHam = 0
totalSpam = 0
for word in hamData:
totalHam += hamData.get(word)
for word in spamData:
totalSpam += spamData.get(word)
for word in hamData:
hamProbablity[word] = float(hamData.get(word) + float(M/total)) / (totalHam + M)
for word in spamData:
spamProbablity[word] = float(spamData.get(word) + float(M/total)) / (totalSpam + M)
maxHam = dict(sorted(hamProbablity.iteritems(), key=operator.itemgetter(1), reverse=True)[:5])
maxSpam = dict(sorted(spamProbablity.iteritems(), key=operator.itemgetter(1), reverse=True)[:5])
print maxHam
print maxSpam
return hamProbablity, spamProbablity, totalHam, totalSpam
def test(TESTNAME, hamProbablity, spamProbablity, totalHam, totalSpam, total):
correct = 0
testtotal = 0
f = open(str(TESTNAME), 'r')
for line in f:
spam = float(0)
ham = float(0)
current = line.split()
for i in range(2, len(current),2):
# print hamProbablity.get(current[i], float(1) / (totalHam + total)),
# print log1p(hamProbablity.get(current[i], float(1) / (totalHam + total)))
# print spamProbablity.get(current[i], float(1) / (totalSpam + total)),
# print log1p(spamProbablity.get(current[i], float(1) / (totalSpam + total)))
ham += log(hamProbablity.get(current[i], (1 + float(M/total)) / (totalHam + M))) * int(current[i+1])
spam += log(spamProbablity.get(current[i], (1 + float(M/total)) / (totalSpam + M))) * int(current[i+1])
print ham, spam, current[1]
if ham > spam:
if current[1] == 'ham':
correct += 1
else:
if current[1] == 'spam':
correct += 1
testtotal += 1
f.close()
accuracy = float(correct) / testtotal * 100
print M
return accuracy
def main(FILENAME, TESTNAME):
hamData, spamData, vocabulary, ham, spam = importData(FILENAME)
total = len(vocabulary.keys())
hamProbablity, spamProbablity, totalHam, totalSpam = getConditionalProbablities(total, hamData, spamData)
print test(TESTNAME, hamProbablity, spamProbablity, totalHam, totalSpam, total)
if __name__ == '__main__':
"""
Main driver function for the experiment.
"""
FILENAME = 'nbctrain'
TESTNAME = 'nbctest'
M = 996
main(FILENAME, TESTNAME)