-
Notifications
You must be signed in to change notification settings - Fork 0
/
example4.py
74 lines (63 loc) · 2.4 KB
/
example4.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
import os
import numpy as np
from collections import Counter
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import LinearSVC
from sklearn.metrics import confusion_matrix
def make_Dictionary(train_dir):
emails = [os.path.join(train_dir, f) for f in os.listdir(train_dir)]
all_words = []
for mail in emails:
with open(mail) as m:
for i, line in enumerate(m):
if i == 2:
words = line.split()
all_words += words
dictionary = Counter(all_words)
# list_to_remove = dictionary.keys()
# for item in list_to_remove: # this works with python 2.x version
for item in list(dictionary): # this works with python 3.x version
if item.isalpha() == False:
del dictionary[item]
elif len(item) == 1:
del dictionary[item]
dictionary = dictionary.most_common(3000)
return dictionary
def extract_features(mail_dir):
files = [os.path.join(mail_dir, fi) for fi in os.listdir(mail_dir)]
features_matrix = np.zeros((len(files), 3000))
docID = 0
for fil in files:
with open(fil) as fi:
for i, line in enumerate(fi):
if i == 2:
words = line.split()
for word in words:
wordID = 0
for i, d in enumerate(dictionary):
if d[0] == word:
wordID = i
features_matrix[docID, wordID] = words.count(word)
docID = docID + 1
return features_matrix
# Create a dictionary of words with its frequency
train_dir = 'ling-spam\\train-mails'
dictionary = make_Dictionary(train_dir)
# Prepare feature vectors per training mail and its labels
train_labels = np.zeros(702) # y=0, ham
train_labels[351:701] = 1 # y=1, spam
train_matrix = extract_features(train_dir)
# Training SVM and Naive bayes classifier and its variants
model1 = LinearSVC()
model2 = MultinomialNB()
model1.fit(train_matrix, train_labels)
model2.fit(train_matrix, train_labels)
# Test the unseen mails for Spam
test_dir = 'ling-spam\\test-mails'
test_matrix = extract_features(test_dir)
test_labels = np.zeros(260)
test_labels[130:260] = 1
result1 = model1.predict(test_matrix)
result2 = model2.predict(test_matrix)
print(confusion_matrix(test_labels, result1))
print(confusion_matrix(test_labels, result2))