-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVM.py
53 lines (43 loc) · 1.48 KB
/
SVM.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
import pickle
import copy
import numpy as np
from collections import Counter
from sklearn.svm import LinearSVC
from sklearn.metrics import confusion_matrix
def dirty_SVM_predict_free_pizza():
pass
def train_svm_on_emails(emails, labels):
dictionary = make_Dictionary(emails)
train_matrix = extract_features(emails, dictionary)
model = LinearSVC()
model.fit(train_matrix, labels)
result = model.predict(train_matrix)
pickle.dump(model, open("text_classifier.pkl","wb"))
print(confusion_matrix(train_matrix, result))
def make_Dictionary(emails):
all_words = []
for email in emails:
words = email.split()
all_words += words
dictionary = Counter(all_words)
#list_to_remove = copy.deepcopy(dictionary.keys())
#for item in list_to_remove:
# if item.isalpha() == False:
# del dictionary[item]
# elif len(item) == 1:
# del dictionary[item]
dictionary = dictionary.most_common(3000)
return dictionary
def extract_features(emails, dictionary):
features_matrix = np.zeros((len(emails), 3000))
docID = 0
for email in emails:
words = email.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