-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson3-SVM.py
62 lines (47 loc) · 1.84 KB
/
Lesson3-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
54
55
56
57
58
59
60
61
62
#!/usr/bin/python3
"""
This is the code to accompany the Lesson 2 (SVM) mini-project.
Use a SVM to identify emails from the Enron corpus by their authors:
Sara has label 0
Chris has label 1
"""
import sys
from time import time
sys.path.append("../tools/")
from email_preprocess import preprocess
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from collections import Counter
### features_train and features_test are the features for the training
### and testing datasets, respectively
### labels_train and labels_test are the corresponding item labels
features_train, features_test, labels_train, labels_test = preprocess()
#########################################################
### your code goes here ###
clf = SVC(kernel='linear', C=1)
features_train = features_train[:int(len(features_train)/100)]
labels_train = labels_train[:int(len(labels_train)/100)]
t0 = time()
clf.fit(features_train,labels_train)
print("Training Time:", round(time()-t0, 3), "s")
t1 = time()
pred = clf.predict(features_test)
print("Predicting Time:", round(time()-t0, 3), "s")
print("Accuracy:", accuracy_score(labels_test,pred))
print ("Predictions:")
print ("10:", pred[10])
print ("26:", pred[26])
print ("50:", pred[50])
c = Counter(pred)
print ("No of predictions for Chris(1):", c[1])
print('Number of events predicted in Chris class is', sum(clf.predict(features_test) ==1))
#########################################################
#########################################################
'''
You'll be Provided similar code in the Quiz
But the Code provided in Quiz has an Indexing issue
The Code Below solves that issue, So use this one
'''
# features_train = features_train[:int(len(features_train)/100)]
# labels_train = labels_train[:int(len(labels_train)/100)]
#########################################################