-
Notifications
You must be signed in to change notification settings - Fork 0
/
Models.py
278 lines (226 loc) · 9.15 KB
/
Models.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import nltk
import random
from nltk.classify.scikitlearn import SklearnClassifier
import pickle
from sklearn.naive_bayes import MultinomialNB, BernoulliNB
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.svm import SVC, LinearSVC, NuSVC
from sklearn.metrics import classification_report
from nltk.classify import ClassifierI
from statistics import mode
import collections
class VoteClassifier(ClassifierI):
def __init__(self, *classifiers):
self._classifiers = classifiers
def classify(self, features):
votes = []
for c in self._classifiers:
v = c.classify(features)
votes.append(v)
try:
return mode(votes)
except Exception as e:
return votes[0]
def confidence(self, features):
votes = []
for c in self._classifiers:
v = c.classify(features)
votes.append(v)
try:
x = mode(votes)
except Exception as e:
x = votes[0]
choice_votes = votes.count(x)
conf = choice_votes / len(votes)
return conf
def precision(reference, test):
if len(test) == 0:
return None
else:
return len(reference.intersection(test)) / len(test)
def recall(reference, test):
if len(reference) == 0:
return None
else:
return len(reference.intersection(test)) / len(reference)
def f1_score(reference, test):
p = precision(reference, test)
r = recall(reference, test)
try:
return str(2 * ((p * r) / (p + r)))
except Exception:
return "None"
def get_model_f1_score(model, testing_set, model_name, logfile):
y_true = []
y_pred = []
for i, (feats, label) in enumerate(testing_set):
observed = model.classify(feats)
y_true.append(label)
y_pred.append(observed)
print("\n###\n" + model_name)
print(classification_report(y_true, y_pred))
logfile.write("\n###\n" + model_name + "\n" + classification_report(y_true, y_pred))
def train(labelled_features_file, test_name, ent):
inputFile = open(labelled_features_file[:-4] + ".pickle","rb")
featuresets = pickle.load(inputFile)
inputFile.close()
# inputFile2 = open(labelled_features_file[:-4] + "_extra.pickle","rb")
# featuresets2 = pickle.load(inputFile2)
# inputFile2.close()
# featuresets += featuresets2
fair = False
loops = 0
if ent == "ner":
while not fair:
loops += 1
random.shuffle(featuresets)
print("length of featuresets", len(featuresets))
new_featuresets = featuresets[:int(len(featuresets) * 1)]
print("NEW length of featuresets", len(new_featuresets))
training_set = new_featuresets[:int(len(new_featuresets) * 0.9)]
testing_set = new_featuresets[int(len(new_featuresets) * 0.9):]
tro = False
trbu = False
triu = False
trbn = False
trin = False
for featureset in training_set:
if featureset[1] == "o":
tro = True
elif featureset[1] == "Bu":
trbu = True
elif featureset[1] == "Iu":
triu = True
elif featureset[1] == "Bn":
trbn = True
elif featureset[1] == "In":
trin = True
teo = False
tebu = False
teiu = False
tebn = False
tein = False
for featureset in testing_set:
if featureset[1] == "o":
teo = True
elif featureset[1] == "Bu":
tebu = True
elif featureset[1] == "Iu":
teiu = True
elif featureset[1] == "Bn":
tebn = True
elif featureset[1] == "In":
tein = True
if tro and trbu and triu and trbn and trin and teo and tebu and teiu and tebn and tein:
print("FAIR")
fair = True
if loops > 500:
print("LOOOOPS")
fair = True
else:
while not fair:
loops += 1
random.shuffle(featuresets)
print("length of featuresets", len(featuresets))
new_featuresets = featuresets[:int(len(featuresets) * 1)]
print("NEW length of featuresets", len(new_featuresets))
training_set = new_featuresets[:int(len(new_featuresets) * 0.9)]
testing_set = new_featuresets[int(len(new_featuresets) * 0.9):]
tro = False
trr = False
for featureset in training_set:
if featureset[1] == "o":
tro = True
elif featureset[1] == "r":
trr= True
teo = False
ter = False
for featureset in testing_set:
if featureset[1] == "o":
teo = True
elif featureset[1] == "r":
ter = True
if tro and trr and teo and ter:
print("FAIR")
fair = True
if loops > 100:
print("LOOOOPS")
fair = True
####################################################
# [ (features , class) ]
# features = {feature:value, feature:value, ...}
####################################################
MNB_classifier = SklearnClassifier(MultinomialNB())
MNB_classifier.train(training_set)
with open("tests/" + test_name + '/MNB_' + ent + '.pickle', 'wb+') as f:
pickle.dump(MNB_classifier, f)
BernoulliNB_classifier = SklearnClassifier(BernoulliNB())
BernoulliNB_classifier.train(training_set)
with open("tests/" + test_name + '/BNB_' + ent + '.pickle', 'wb+') as f:
pickle.dump(BernoulliNB_classifier, f)
LogisticRegression_classifier = SklearnClassifier(LogisticRegression())
LogisticRegression_classifier.train(training_set)
with open("tests/" + test_name + '/LogisticRegression_' + ent + '.pickle', 'wb+') as f:
pickle.dump(LogisticRegression_classifier, f)
SGDClassifier_classifier = SklearnClassifier(SGDClassifier())
SGDClassifier_classifier.train(training_set)
with open("tests/" + test_name + '/SGD_' + ent + '.pickle', 'wb+') as f:
pickle.dump(SGDClassifier_classifier, f)
SVC_classifier = SklearnClassifier(SVC())
SVC_classifier.train(training_set)
with open("tests/" + test_name + '/SVC_' + ent + '.pickle', 'wb+') as f:
pickle.dump(SVC_classifier, f)
LinearSVC_classifier = SklearnClassifier(LinearSVC())
LinearSVC_classifier.train(training_set)
with open("tests/" + test_name + '/LinearSVC_' + ent + '.pickle', 'wb+') as f:
pickle.dump(LinearSVC_classifier, f)
# NuSVC_classifier = SklearnClassifier(NuSVC(nu=0.2))
# NuSVC_classifier.train(training_set)
# with open("tests/" + test_name + '/NuSVC_' + ent + '.pickle', 'wb+') as f:
# pickle.dump(NuSVC_classifier, f)
return testing_set
def test(labelled_features_file, test_name, ent, testing_set):
# inputFile = open(labelled_features_file[:-4] + ".pickle","rb")
# featuresets = pickle.load(inputFile)
# inputFile.close()
test_log = open("tests/" + test_name + "/test_log_" + ent + "_" + labelled_features_file[-5:-4] + ".txt", "w+")
#print(len(testing_set))
#training_set = featuresets[:int(len(featuresets) * 0.9)]
#testing_set = featuresets[int(len(featuresets) * 0.9):]
MNB_F = open("tests/" + test_name + "/MNB_" + ent + ".pickle","rb")
MNB = pickle.load(MNB_F)
MNB_F.close()
get_model_f1_score(MNB, testing_set, "MNB", test_log)
BNB_F = open("tests/" + test_name + "/BNB_" + ent + ".pickle","rb")
BNB = pickle.load(BNB_F)
BNB_F.close()
get_model_f1_score(BNB, testing_set, "BNB", test_log)
LR_F = open("tests/" + test_name + "/LogisticRegression_" + ent + ".pickle","rb")
LR = pickle.load(LR_F)
LR_F.close()
get_model_f1_score(LR, testing_set, "LR", test_log)
SGD_F = open("tests/" + test_name + "/SGD_" + ent + ".pickle","rb")
SGD = pickle.load(SGD_F)
SGD_F.close()
get_model_f1_score(SGD, testing_set, "SGD", test_log)
SVC_F = open("tests/" + test_name + "/SVC_" + ent + ".pickle","rb")
SVC = pickle.load(SVC_F)
SVC_F.close()
get_model_f1_score(SVC, testing_set, "SVC", test_log)
LSVC_F = open("tests/" + test_name + "/LinearSVC_" + ent + ".pickle","rb")
LSVC = pickle.load(LSVC_F)
LSVC_F.close()
get_model_f1_score(LSVC, testing_set, "LSVC", test_log)
# NuSVC_F = open("tests/" + test_name + "/NuSVC_" + ent + ".pickle","rb")
# NuSVC = pickle.load(NuSVC_F)
# NuSVC_F.close()
# get_model_f1_score(NuSVC, testing_set, "NuSVC", test_log)
VC = VoteClassifier(
LSVC,
SGD,
MNB,
BNB,
LR)
get_model_f1_score(VC, testing_set, "VC", test_log)