-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathdt_author_id.py
86 lines (58 loc) · 2.49 KB
/
dt_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
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
#!/usr/bin/python
"""
This is the code to accompany the Lesson 3 (decision tree) mini-project.
Use a Decision Tree to identify emails from the Enron corpus by author:
Sara has label 0
Chris has label 1
"""
import sys
from time import time
sys.path.append("./tools/")
from email_preprocess import preprocess
### 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 ###
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# Create a Decision Tree Classifier (DT) object
t0 = time()
clf = DecisionTreeClassifier(random_state=0, min_samples_split=40)
clf.fit(features_train, labels_train)
print("Training Time:", round(time()-t0, 3), "s")
#########################################################
pred = clf.predict(features_test)
accuracy = clf.score(features_test, labels_test)
acc = accuracy_score(pred, labels_test)
print("Accuracy:", round(accuracy,3))
print("Metrics Accuracy:", round(acc, 3))
#########################################################
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import accuracy_score
# Create a AdaBoost Classifier (AB) object
t0 = time()
clf = AdaBoostClassifier(n_estimators=100, random_state=0)
clf.fit(features_train, labels_train)
print("Training Time:", round(time()-t0, 3), "s")
acc = accuracy_score(clf.predict(features_test), labels_test)
print("Metrics Accuracy:", round(acc, 3))
#########################################################
from sklearn.neighbors import KNeighborsClassifier
# Create a KNeighbors Classifier (KNN) object
t0 = time()
clf = KNeighborsClassifier(n_neighbors=3)
clf.fit(features_train, labels_train)
print("Training Time:", round(time()-t0, 3), "s")
acc = accuracy_score(clf.predict(features_test), labels_test)
print("Metrics Accuracy:", round(acc, 3))
#########################################################
from sklearn.ensemble import RandomForestClassifier
# Create a RandomForest Classifier (RF) object
t0 = time()
clf = RandomForestClassifier(n_estimators=100, random_state=0)
clf.fit(features_train, labels_train)
print("Training Time:", round(time()-t0, 3), "s")
acc = accuracy_score(clf.predict(features_test), labels_test)
print("Metrics Accuracy:", round(acc, 3))