-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnb_author_id.py
40 lines (31 loc) · 1.12 KB
/
nb_author_id.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
#!/usr/bin/python
"""
authors and labels:
- Enrique has label 0
- Juan has label 1
"""
import sys
from time import time
sys.path.append("../tools/")
from email_preprocess import preprocess
from sklearn.naive_bayes import GaussianNB
def get_model_train(features_train, labels_train):
clf = GaussianNB()
clf.fit(features_train, labels_train)
return clf
def predict(clf, features_test):
return clf.predict(features_test)
def get_accuracy(clf, features_test, labels_test):
return clf.score(features_test, labels_test)
if __name__ == "__main__":
### 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()
t0 = time()
clf = get_model_train(features_train, labels_train)
print("training time: {} s".format(round(time()-t0, 3)))
t0 = time()
print("Prediction: {}".format(predict(clf, features_test)))
print("Accuracy: {}".format(get_accuracy(clf, features_test, labels_test)))
print("prediction time: {} s".format(round(time()-t0, 3)))