/
NLTK_NaiveBayes.py
205 lines (151 loc) · 7.12 KB
/
NLTK_NaiveBayes.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import random
from nltk import NaiveBayesClassifier
import Preprocessor
spam_word_count = {}
ham_word_count = {}
words_in_spam = 0
words_in_ham = 0
raw_spam_prob = {}
raw_ham_prob = {}
def train(samples_proportion=0.7):
global words_in_ham, ham_word_count, words_in_spam, spam_word_count, raw_ham_prob, raw_spam_prob
ham, spam = read_spam_ham()
print ("Spam size: " + str(len(spam)) + " Ham size: " + str(len(ham)))
all_emails = append_ham_and_spam(ham, spam)
random.shuffle(all_emails)
print('Corpus size = ' + str(len(all_emails)) + ' emails')
features = [(Preprocessor.get_features(email, ' '), label) for (email, label) in all_emails]
print('Collected ' + str(len(features)) + ' feature sets')
'''
# define Support value in %
support = 10
spam_support_count = (spam_size * 10) / 100;
ham_support_count = (ham_size * 10) / 100;
print('Spam support count:' + str(spam_support_count))
print('Ham support count:' + str(ham_support_count))
# get the spam frequent itemset and ham frequent itemset
# spam_frequent, ham_frequent = get_frequent(all_features, spam_support_count, ham_support_count)
# train the our own naivebayes classifier and collect dictionary of raw probabilities of words
'''
train_size = int(len(features) * samples_proportion)
train_set, test_set = features[:train_size], features[train_size:]
ham_mail_count, spam_mail_count = mails_in_ham_spam(train_set)
spam_prior = 1.0 * spam_mail_count / len(train_set)
ham_prior = 1.0 * ham_mail_count / len(train_set)
words_in_ham, words_in_spam = frequency_in_ham_spam(train_set)
spam_vocab = len(spam_word_count)
ham_vocab = len(ham_word_count)
t = get_probabilities_in_each_class(ham_prior, words_in_ham, ham_vocab, ham_word_count, raw_ham_prob, raw_spam_prob,
spam_prior, words_in_spam, spam_vocab, spam_word_count, test_set, train_set)
ham_prior, words_in_ham, ham_vocab, raw_ham_prob, raw_spam_prob, spam_prior, words_in_spam, spam_vocab, test_set, train_set = get_parameters(
t)
#print("Train Size:" + str(len(train_set)) + str(' Test size:') + str(len(test_set)))
#evaluate(train_set, test_set, raw_spam_prob, raw_ham_prob, words_in_spam, words_in_ham, spam_vocab, ham_vocab,
# spam_prior,
# ham_prior)
classifier = NaiveBayesClassifier(list(spam_word_count),list(ham_word_count))
t = classifier.prob_classify(classifier, train_set).max()
def get_probabilities_in_each_class(ham_prior, ham_total, ham_vocab, ham_word_count, raw_ham_prob, raw_spam_prob,
spam_prior, spam_total, spam_vocab, spam_word_count, test_set, train_set):
for (words, label) in train_set:
if label == 'spam':
for word in words:
raw_spam_prob[word] = (float)((spam_word_count.setdefault(word, 0)) / (spam_total))
else:
for word in words:
raw_ham_prob[word] = (float)((ham_word_count.setdefault(word, 0)) / (ham_total))
return [train_set, test_set, raw_spam_prob, raw_ham_prob, spam_total, ham_total, spam_vocab, ham_vocab, spam_prior,
ham_prior]
def frequency_in_ham_spam(train_set):
global spam_word_count, ham_word_count
spam_total = 0
ham_total = 0
for (frequency, label) in train_set:
if label == 'spam':
for key in frequency:
spam_word_count.setdefault(key, 0.0)
spam_word_count[key] += frequency[key]
spam_total += 1
else:
for key in frequency:
ham_word_count.setdefault(key, 0.0)
ham_word_count[key] += frequency[key]
ham_total += 1
return ham_total, spam_total
def mails_in_ham_spam(train_set):
spam_count = 0
ham_count = 0
for (words, label) in train_set:
if (label == 'spam'):
spam_count += 1
else:
ham_count += 1
return ham_count, spam_count
def classify(data_set, raw_spam_prob, raw_ham_prob, spam_total, ham_total, spam_vocab, ham_vocab, spam_prior,
ham_prior):
total_mail = 0
correct_count = 0.0000000
for (features, label) in data_set:
spam_prob = spam_prior
ham_prob = ham_prior
is_spam = check_spam(features, ham_prior, ham_prob, ham_total, ham_vocab, raw_ham_prob, raw_spam_prob,
spam_prior, spam_prob, spam_total, spam_vocab)
if (label == 'spam') and is_spam:
correct_count += 1
if (label == 'ham') and not (is_spam):
correct_count += 1
total_mail += 1
print('correct count' + str(correct_count))
return correct_count / total_mail
def check_spam(features, ham_prior, ham_prob, ham_total, ham_vocab, raw_ham_prob, raw_spam_prob, spam_prior, spam_prob,
spam_total, spam_vocab):
for word in features:
# Handling probability of non occuring words with laplaces
try:
spam_prob = raw_spam_prob[word] * spam_prob
except KeyError:
raw_spam_prob[word] = (1 / (spam_total + spam_vocab + ham_vocab))
spam_prob = raw_spam_prob[word] * spam_prob
try:
ham_prob = raw_ham_prob[word] * ham_prob
except KeyError:
raw_ham_prob[word] = (1 / (ham_total + ham_vocab + spam_vocab))
ham_prob = raw_ham_prob[word] * ham_prob
if spam_prob > ham_prob:
is_spam = True
else:
is_spam = False
return is_spam
def evaluate(train_set, test_set, raw_spam_prob, raw_ham_prob, spam_total, ham_total, spam_vocab, ham_vocab, spam_prior,
ham_prior):
train_accuracy = classify(train_set, raw_spam_prob, raw_ham_prob, spam_total, ham_total, spam_vocab, ham_vocab,
spam_prior, ham_prior)
test_accuracy = classify(test_set, raw_spam_prob, raw_ham_prob, spam_total, ham_total, spam_vocab, ham_vocab,
spam_prior, ham_prior)
print('Accuracy on the training set = ' + str(train_accuracy))
print('Accuracy of the test set = ' + str(test_accuracy))
#TODO: check which words are most informative for the classifier
def read_spam_ham():
spam = Preprocessor.init_lists('enron1 (copy)/spam/')
ham = Preprocessor.init_lists('enron1 (copy)/ham/')
return ham, spam
def get_parameters(t):
train_set = t[0]
test_set = t[1]
raw_spam_prob = t[2]
raw_ham_prob = t[3]
spam_total = t[4]
ham_total = t[5]
spam_vocab = t[6]
ham_vocab = t[7]
spam_prior = t[8]
ham_prior = t[9]
return ham_prior, ham_total, ham_vocab, raw_ham_prob, raw_spam_prob, spam_prior, spam_total, spam_vocab, test_set, train_set
def append_ham_and_spam(ham, spam):
all_emails = [(email, 'spam') for email in spam]
all_emails += [(email, 'ham') for email in ham]
return all_emails
def get_spam_ham_features(ham_emails, spam_emails):
spam_features = [(Preprocessor.get_features(email, ' '), label) for (email, label) in spam_emails]
ham_features = [(Preprocessor.get_features(email, ' '), label) for (email, label) in ham_emails]
return ham_features, spam_features