-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathid3.py
More file actions
436 lines (328 loc) · 12.2 KB
/
id3.py
File metadata and controls
436 lines (328 loc) · 12.2 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import os
import numpy as np
import re
from copy import deepcopy
np.seterr(divide='ignore', invalid='ignore')
# Tree node class
class Node(object):
def __init__(self, parent=None, ham_prob=None, spam_prob=None):
self.parent = parent
self.ham_prob = ham_prob
self.spam_prob = spam_prob
self.left = None
self.right = None
self.word = None
def print(self):
print(self.get_probs())
if self.left is not None:
self.left.print()
if self.right is not None:
self.right.print()
def name(self, word):
self.word = word
def insert_left(self, node):
self.left = node
def insert_right(self, node):
self.right = node
def get_left(self):
return self.left
def get_right(self):
return self.right
def get_probs(self):
return self.ham_prob, self.spam_prob
def get_word(self):
return self.word
def get_parent(self):
return self.parent
# All ham emails
ham_emails = []
# All spam emails
spam_emails = []
# Words that occur in ham emails and their population
ham_occurs = dict()
# Words that occur in spam emails and their population
spam_occurs = dict()
# Words that occur in all emails and their population
all_words = dict()
# Total number of emails
total_count = 0
# Total number of ham emails
ham_count = 0
# Total number of spam emails
spam_count = 0
# Entropy is entrop[0], global variable
entrop = []
# Function that reads all the emails, breaks the message in single words, counts the files and the occurrences of the words in the emails, save all the email as an example for id3
def readWords(ham_loc, spam_loc):
# Ham and spam email numbers
ham_count = len(os.listdir(os.getcwd()+ham_loc))
spam_count = len(os.listdir(os.getcwd()+spam_loc))
# For every file read the message break it down to single words (no duplicates of words) and update the dictionaries
for fname in os.listdir(os.getcwd()+ham_loc):
filename = os.getcwd()+ham_loc+"/"+fname
file = open(filename, "r")
# message of mail as string
message = file.read()
# list of words
contents = re.split("\s", message)
# list that has only the words and not the duplicates
one_occur = []
for word in contents:
if word not in one_occur:
one_occur.append(word)
for word in one_occur:
if word not in ham_occurs:
ham_occurs.update({word: 1})
else:
ham_occurs[word] = ham_occurs.get(word) + 1
ham_emails.append(one_occur)
file.close()
for fname in os.listdir(os.getcwd()+spam_loc):
filename = os.getcwd()+spam_loc+"/"+fname
file = open(filename, "r")
message = file.read()
contents = re.split("\s", message)
one_occur = []
for word in contents:
if word not in one_occur:
one_occur.append(word)
for word in one_occur:
if word not in spam_occurs:
spam_occurs.update({word: 1})
else:
spam_occurs[word] = spam_occurs.get(word) + 1
spam_emails.append(one_occur)
file.close()
analogy_spam = 1
analogy_ham = 1
if spam_count < ham_count:
analogy_spam = ham_count / spam_count
else:
analogy_ham = spam_count / ham_count
for word in ham_occurs:
all_words[word] = ham_occurs.get(word)*analogy_ham
for word in spam_occurs:
if word not in all_words:
all_words[word] = spam_occurs.get(word)*analogy_spam
else:
all_words[word] = np.abs(all_words.get(word) - spam_occurs.get(word)*analogy_spam)
# Calculate the best words for the tree creation
to_desc = sorted(all_words, key=all_words.get, reverse=True)
all_words_sorted = dict()
for word in to_desc:
all_words_sorted[word] = all_words.get(word)
percent = int(0.003 * len(all_words_sorted))
most_imp = dict()
for i in range(0, percent):
word = to_desc[i]
most_imp[word] = all_words.get(word)
return ham_count, spam_count, most_imp
# Function util, calculates & keep the emails that a word exists in ham & spam AND keep the emails that a wods DOES NOT exists in ham & spam emails
def util(word, ham_emails, spam_emails):
ham_in = []
ham_not_in = []
spam_in = []
spam_not_in = []
if word is not None:
for e in ham_emails:
if word in e:
ham_in.append(e)
else:
ham_not_in.append(e)
for e in spam_emails:
if word in e:
spam_in.append(e)
else:
spam_not_in.append(e)
all_words.pop(word, None)
ham_count = len(ham_in)
spam_count = len(spam_in)
ham_occurs.clear()
spam_occurs.clear()
for i in ham_in:
for word in i:
if word not in ham_occurs:
ham_occurs.update({word: 1})
else:
ham_occurs[word] = ham_occurs.get(word) + 1
for i in spam_in:
for word in i:
if word not in spam_occurs:
spam_occurs.update({word: 1})
else:
spam_occurs[word] = spam_occurs.get(word) + 1
return ham_count, spam_count, ham_in, ham_not_in, spam_in, spam_not_in
# Function that calculates the entropy
def entropy(ham_total, spam_total):
return -spam_total / (spam_total + ham_total) * np.log2(spam_total / (spam_total + ham_total)) - ham_total / (
spam_total + ham_total) * np.log2(ham_total / (spam_total + ham_total))
# Function that calculates the informaion gain for a specific word
def ig(ham_total, spam_total, entropy, word):
word_sum = 2
if word in ham_occurs:
word_sum += ham_occurs.get(word)
if word in spam_occurs:
word_sum += spam_occurs.get(word)
total_emails = spam_total + ham_total + 0
ham = 0
spam = 0
if word in ham_occurs:
ham += ham_occurs.get(word)
if word in spam_occurs:
spam += spam_occurs.get(word)
word_exists = word_sum/total_emails
word_not_exists = (total_emails-word_sum)/total_emails
not_exists_ham = (ham_total-ham)+1
not_exists_spam = (spam_total-spam)+1
temp_sum = not_exists_ham + not_exists_spam + 2
not_exists_ham = (not_exists_ham)/(temp_sum)
not_exists_spam = (not_exists_spam)/(temp_sum)
exists_ham = (ham+1)/word_sum
exists_spam = (spam+1)/word_sum
c_not_ham = not_exists_ham*np.log2(not_exists_ham)
c_not_spam = not_exists_spam*np.log2(not_exists_spam)
c_ham = exists_ham*np.log2(exists_ham)
c_spam = exists_spam*np.log2(exists_spam)
not_sum = -(c_not_ham + c_not_spam)
sum = -(c_ham + c_spam)
ex = word_exists*sum
not_ex = word_not_exists*not_sum
ig = entropy - (ex + not_ex)
return ig
# Function that returns from the most valuable words the on with the best information gain
def best_info_gain(ham_count, spam_count, most_imp):
info_gain = dict()
sorted_info_gain = dict()
for word in most_imp:
info_gain[word] = ig(ham_count, spam_count, entrop[0], word)
sorted_words = sorted(info_gain, key=info_gain.get, reverse=True)
for word in sorted_words:
sorted_info_gain[word] = info_gain.get(word)
best = list(sorted_info_gain)[0]
return best
# Function that builds recursively the id3 decision tree
def id3(ham_emails, spam_emails, most_imp, root):
ham_total = len(ham_emails)
spam_total = len(spam_emails)
if ham_total <= 0 or spam_total <= 0:
return root
if len(most_imp) == 0:
return root
if (ham_total + spam_total) < 100:
return root
best = best_info_gain(ham_total, spam_total, most_imp)
ham_count, spam_count, ham_in, ham_not_in, spam_in, spam_not_in = util(best, ham_emails, spam_emails)
if len(ham_in) == 0 and len(spam_in) == 0:
return root
if len(ham_not_in) == 0 and len(spam_not_in) == 0:
return root
root.name(best)
most_imp.pop(best, None)
# Left include word, X = 0
prob_ham_left = (len(ham_in))/((len(ham_in)+len(spam_in)))
left = Node(root, prob_ham_left, 1 - prob_ham_left)
if prob_ham_left >= 0.95 or prob_ham_left <= 0.05:
root.insert_left(left)
else:
left = id3(ham_in, spam_in, most_imp, left)
root.insert_left(left)
# Right not include word, X = 0
prob_ham_right = len(ham_not_in)/(len(ham_not_in)+len(spam_not_in))
right = Node(root, prob_ham_right, 1 - prob_ham_right)
if prob_ham_right >= 0.95 or prob_ham_right <= 0.05:
root.insert_right(right)
else:
right = id3(ham_not_in, spam_not_in, most_imp, right)
root.insert_right(right)
return root
# Helper function that is call from analysis contains the locations of the ham & spam trains, test & validation
def init(ham_train, spam_train, test, validation):
ham_count, spam_count, most_imp = readWords(ham_train, spam_train)
total_count = ham_count + spam_count
ham_freq = ham_count / total_count
spam_freq = spam_count / total_count
ent = entropy(ham_count, spam_count)
entrop.append(ent)
root = Node(None, ham_freq, spam_freq)
root = id3(ham_emails, spam_emails, most_imp, root)
test_size = len(os.listdir(os.getcwd() + test))
correct_test = 0
true_positive = 0
false_positive = 0
false_negative = 0
for filename in os.listdir(os.getcwd() + test):
new_tree = root
file = open(os.getcwd() + test + filename, "r")
message = file.read()
testing = re.split(" | \r\n", message)
file_w = []
for i in testing:
if i in testing and i not in file_w:
file_w.append(i)
file.close()
while new_tree is not None:
if new_tree.get_word() in file_w:
new_tree = new_tree.get_left()
if new_tree.get_right() is None or new_tree.get_left() is None:
break
else:
new_tree = new_tree.get_right()
if new_tree.get_right() is None or new_tree.get_left() is None:
break
ham, spam = new_tree.get_probs()
if ham >= spam:
res = 1
else:
res = 0
if "spam" in filename and res == 0:
correct_test += 1
true_positive += 1
if "ham" in filename and res == 1:
correct_test += 1
true_positive += 1
if "spam" in filename and res == 1:
false_positive += 1
if "ham" in filename and res == 0:
false_positive += 1
validation_size = len(os.listdir(os.getcwd() + validation))
correct_validation = 0
for filename in os.listdir(os.getcwd() + validation):
new_tree = root
file = open(os.getcwd() + validation + filename, "r")
message = file.read()
testing = re.split(" | \r\n", message)
file_w = []
for i in testing:
if i in testing and i not in file_w:
file_w.append(i)
file.close()
while new_tree is not None:
if new_tree.get_word() in file_w:
new_tree = new_tree.get_left()
if new_tree.get_right() is None or new_tree.get_left() is None:
break
else:
new_tree = new_tree.get_right()
if new_tree.get_right() is None or new_tree.get_left() is None:
break
ham, spam = new_tree.get_probs()
if ham >= spam:
res = 1
else:
res = 0
if "spam" in filename and res == 0:
correct_validation += 1
true_positive += 1
if "ham" in filename and res == 1:
correct_validation += 1
true_positive += 1
if "spam" in filename and res == 1:
false_negative += 1
if "ham" in filename and res == 0:
false_negative += 1
accuracy = correct_test / test_size*100
error = 100 - accuracy
precision = true_positive / (true_positive + false_positive)*100
recall = true_positive / (true_positive + false_negative)*100
f1 = 2 * ((precision * recall) / (precision + recall))
return accuracy, error, precision, recall, f1