-
Notifications
You must be signed in to change notification settings - Fork 1
/
baselines.py
780 lines (574 loc) · 22.2 KB
/
baselines.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
"""Some baselines to compare to the more advanced models.
Contains the following models:
1) minority classifier
2) majority classifier
3) uniform classifier
4) per-language classifier
5) Naive Bayes (Gaussian & Multinomial)
TODO:
6) Decision Tree
7) SVM
Usage:
python baselines.py data/combined/traindev_data.jsonl
data/combined/test_data.jsonl nb
"""
import argparse
import csv
import datetime
import json
import sys
import wandb
from collections import namedtuple
from sklearn import tree
from sklearn.base import BaseEstimator
from sklearn.base import TransformerMixin
from sklearn.dummy import DummyClassifier
from sklearn.feature_extraction import DictVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.metrics import plot_precision_recall_curve
from sklearn.metrics import roc_auc_score
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import FeatureUnion
from sklearn.pipeline import Pipeline
from sklearn.svm import LinearSVC
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
import numpy as np
import fasttext
import fasttext.util
import stopwordsiso as stopwords
import data
from utils import training_utils as train_utils
from utils import visualizations
from utils.embedder import MeanEmbeddingVectorizer
wandb.init(project="final_binary_classification")
Scores = namedtuple("scores", ["y_true", "y_score", "y_pred", "metrics"])
class CustomTransformer(BaseEstimator, TransformerMixin):
"""Extracts language and post length from each document."""
def __init__(self):
"""..."""
pass
def fit(self, x, y=None):
"""..."""
return self
def transform(self, data):
"""..."""
return self.extract_language_and_length(data)
def extract_language_and_length(self, data):
"""..."""
feature_dicts = []
for doc in data:
feature_dict = {
"language": doc["language"],
"length": len(doc["text"].strip().split(" ")),
}
feature_dicts.append(feature_dict)
return feature_dicts
class ItemSelector(BaseEstimator, TransformerMixin):
"""..."""
def __init__(self, key):
"""..."""
self.key = key
def fit(self, x, y=None):
"""..."""
return self
def transform(self, data):
"""..."""
return [x[self.key] for x in data]
def run_majority_classifier(train_data, train_labels, test_data, test_labels):
"""..."""
print(
"Run random classifier with most_frequent strategy: always predicts "
"the most frequent label in the training set.."
)
train_data = [doc["text"] for doc in train_data]
test_data = [doc["text"] for doc in test_data]
clf = Pipeline(
[
("vect", TfidfVectorizer()),
("clf", DummyClassifier(strategy="most_frequent", random_state=42)),
]
)
clf.fit(train_data, train_labels)
y_pred = clf.predict(test_data)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
y_scores = clf.predict_proba(test_data)
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def run_uniform_classifier(train_data, train_labels, test_data, test_labels):
"""..."""
print(
"Run random classifier with uniform strategy: generates predictions "
"uniformly at random."
)
X_train = [x["text"] for x in train_data]
X_test = [x["text"] for x in test_data]
clf = Pipeline(
[
("vect", TfidfVectorizer()),
("clf", DummyClassifier(strategy="uniform", random_state=42)),
]
)
clf.fit(X_train, train_labels)
y_pred = clf.predict(X_test)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
y_scores = clf.predict_proba(X_test)
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def run_minority_classifier(train_data, train_labels, test_data, test_labels):
"""..."""
print(
"Run random classifier with constant strategy: always predicts the "
"minority label in the training set.."
)
X_train = [x["text"] for x in train_data]
X_test = [x["text"] for x in test_data]
clf = Pipeline(
[
("vect", TfidfVectorizer()),
("clf", DummyClassifier(strategy="constant", random_state=42, constant=0)),
]
)
clf.fit(X_train, train_labels)
y_pred = clf.predict(X_test)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
y_scores = clf.predict_proba(X_test)
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def predict_according_to_language(test_data, test_labels, language):
"""..."""
print(
"Run classifier that predicts '1' for English, '0' for all other " "languages."
)
y_pred = []
for i in range(len(test_data)):
if language[i] == "english":
y_pred.append(1)
else:
y_pred.append(0)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
# y_scores = [float(x) for x in y_pred]
# print(y_scores)
y_scores = np.empty(shape=(len(y_pred), 2))
for i, x in enumerate(y_pred):
if x == 1:
y_scores[i] = [0, 1]
else:
y_scores[i] = [1, 0]
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def get_lang_len_features(data, language, language_dict):
"""Convert the data to two-data (language and post length) only."""
X = []
for i, post in enumerate(data):
# get post length
post_len = len(post["text"].strip().split(" "))
# get language index
lang_idx = language_dict[language[i]]
X.append([post_len, lang_idx])
return X
def run_gaussian_naive_bayes(
train_data, train_labels, train_lang, test_data, test_labels, test_lang
):
"""Train a two-features only Naive Bayes classifier."""
print(
"\nRun Gaussian Naive Bayes with two features: " "language and post length ..."
)
lang_dict = {}
for j, lang in enumerate(set(train_lang)):
lang_dict[lang] = j
X_train = get_lang_len_features(
data=train_data, language=train_lang, language_dict=lang_dict
)
X_test = get_lang_len_features(
data=test_data, language=test_lang, language_dict=lang_dict
)
clf = GaussianNB()
clf.fit(X_train, train_labels)
y_pred = clf.predict(X_test)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
y_scores = clf.predict_proba(X_test)
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def run_multinomial_naive_bayes(
train_data, train_labels, train_lang, test_data, test_labels, test_lang
):
"""..."""
print("\nRun multinomial Naive Bayes ...")
X_train = [x["text"] for x in train_data]
X_test = [x["text"] for x in test_data]
clf = Pipeline(
[
("vect", TfidfVectorizer(analyzer="word")),
("clf", MultinomialNB()),
]
)
clf.fit(X_train, train_labels)
y_pred = clf.predict(X_test)
results = train_utils.get_results(y_true=train_labels, y_pred=y_pred)
y_scores = clf.predict_proba(X_test)
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def run_NB_on_tfidf_lang_and_len(train_data, train_labels, test_data, test_labels):
"""..."""
print("\nRun multinomial Naive Bayes on cleaned words + language + length " "...")
bow_pipeline = Pipeline(
steps=[
("selector", ItemSelector(key="text")),
# TODO: get stop word lists
("tfidf", TfidfVectorizer(analyzer="word")),
]
)
manual_pipeline = Pipeline(
steps=[
("language_length", CustomTransformer()),
("dict_vect", DictVectorizer()),
]
)
combined_features = FeatureUnion(
transformer_list=[
("bow", bow_pipeline),
("manual", manual_pipeline),
]
)
final_pipeline = Pipeline(
steps=[
("combined_features", combined_features),
("clf", MultinomialNB()),
],
verbose=False,
)
final_pipeline.fit(train_data, train_labels)
y_pred = final_pipeline.predict(test_data)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
y_scores = final_pipeline.predict_proba(test_data)
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def run_multinomial_nb(train_data, train_labels, test_data, test_labels):
"""..."""
print("Run multinomial Naive Bayes ...")
X_train = [x["text"] for x in train_data]
X_test = [x["text"] for x in test_data]
clf = Pipeline(
[
("vect", TfidfVectorizer(analyzer="char")),
("clf", MultinomialNB()),
]
)
clf.fit(X_train, train_labels)
y_pred = clf.predict(X_test)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
y_scores = clf.predict_proba(X_test)
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def plot_tree(clf, features):
"""..."""
import graphviz
dot_data = tree.export_graphviz(
clf, out_file=None, filled=True, class_names=["0", "1"], feature_names=features
)
graph = graphviz.Source(dot_data)
graph.render("DecisionTree")
def run_decision_tree_classifier(train_data, train_labels, test_data, test_labels):
"""..."""
X_train = [x["text"] for x in train_data]
X_test = [x["text"] for x in test_data]
decision_tree_pipeline = Pipeline(
[
("vect", TfidfVectorizer(analyzer="char")),
(
"clf",
DecisionTreeClassifier(
max_leaf_nodes=20, random_state=42, min_samples_split=100
),
),
]
)
decision_tree_pipeline.fit(X_train, train_labels)
y_pred = decision_tree_pipeline.predict(X_test)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
word2id = decision_tree_pipeline["vect"].vocabulary_
print(f"#words in vocab: {len(word2id)}")
id2word = {idx: word for word, idx in word2id.items()}
feature_importances = decision_tree_pipeline["clf"].feature_importances_
most_imp = np.argpartition(feature_importances, -20)[-20:]
y_scores = decision_tree_pipeline.predict_proba(X_test)
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def run_svm_on_embeddings(
train_data, train_labels, test_data, test_labels, aligned_embeds=True, seed=42
):
"""Run a SVM on aligned embeddings."""
available_languages = []
for doc in train_data + test_data:
available_languages.append(doc["language"])
embedder = MeanEmbeddingVectorizer(
use_aligned_embeddings=aligned_embeds, languages=set(available_languages)
)
pipeline = Pipeline(
[
("embedder", embedder),
("clf", SVC(probability=True, class_weight="balanced", random_state=seed)),
]
)
pipeline.fit(train_data, train_labels)
y_pred = pipeline.predict(test_data)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
y_scores = pipeline.predict_proba(test_data)
print(f"y_scores: {y_scores}")
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def run_svm_on_embeddings_sweep(
train_data, train_labels, test_data, test_labels, aligned_embeds=True, seed=42
):
"""Run a SVM on aligned embeddings."""
available_languages = []
for doc in train_data + test_data:
available_languages.append(doc["language"])
embedder = MeanEmbeddingVectorizer(
use_aligned_embeddings=aligned_embeds, languages=set(available_languages)
)
pipeline = Pipeline(
[
("embedder", embedder),
("clf", SVC(class_weight="balanced", random_state=seed)),
]
)
pipeline.fit(train_data, train_labels)
y_pred = pipeline.predict(test_data)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
# y_scores = pipeline.predict_proba(test_data)
# return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
return results, y_pred
def run_svm_on_bow(train_data, train_labels, test_data, test_labels):
"""SVM Classifier on bag-of-words."""
X_train = [doc["text"] for doc in train_data]
X_test = [doc["text"] for doc in test_data]
pipeline = Pipeline(
[
("vect", CountVectorizer()),
("clf", SVC(probability=True, class_weight="balanced")),
]
)
pipeline.fit(X_train, train_labels)
y_pred = pipeline.predict(X_test)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
y_scores = pipeline.predict_proba(X_test)
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def run_svm_on_tfidf(train_data, train_labels, test_data, test_labels):
"""SVM Classifier on bag-of-words."""
X_train = [doc["text"] for doc in train_data]
X_test = [doc["text"] for doc in test_data]
pipeline = Pipeline(
[
("vect", TfidfVectorizer()),
("clf", SVC(probability=True, class_weight="balanced")),
]
)
pipeline.fit(X_train, train_labels)
y_pred = pipeline.predict(X_test)
results = train_utils.get_results(y_true=test_labels, y_pred=y_pred)
y_scores = pipeline.predict_proba(X_test)
return Scores(y_true=test_labels, y_score=y_scores, y_pred=y_pred, metrics=results)
def plot_results(results_list, descriptions):
"""..."""
visualizations.plot_precision_recall_curve(results_list, descriptions, "baselines")
visualizations.plot_roc_curve(results_list, descriptions, "baselines")
def write_predictions_to_file(predictions_dict, docs_test, labels_test):
"""..."""
assert len(labels_test) == len(docs_test)
fieldnames = ["gold"] + list(predictions_dict.keys()) + ["text"]
date = datetime.datetime.now().strftime("%d_%m_%y_%H_%M")
with open(f"predictions_baselines_{date}.csv", "w") as write_handle:
writer = csv.DictWriter(write_handle, fieldnames=fieldnames, delimiter="\t")
writer.writeheader()
for i, doc in enumerate(docs_test):
row = {"text": doc["text"], "gold": labels_test[i]}
for classif, predictions in predictions_dict.items():
row[classif] = predictions[i]
writer.writerow(row)
if __name__ == "__main__":
choices = [
"majority",
"minority",
"uniform",
"language",
"nb_tfidf_lang_len",
"nb_lang_len",
"mnb",
"dt",
"svm_aligned",
"svm_separated",
"svm_bow",
"svm_tfidf",
"all",
]
parser = argparse.ArgumentParser()
parser.add_argument("train_dev_data", help=("Path to train/dev data."))
parser.add_argument("test_data", default=None, help="Path to test data.")
parser.add_argument(
"--mode", "-m", choices=choices, action="append", help="The sort of classifier."
)
parser.add_argument(
"-p", "--plot", action="store_true", help="Plot precision-recall & roc curve"
)
parser.add_argument(
"-sp",
"--store_predictions",
action="store_true",
help="Store predictions of all classifiers in a file.",
)
args = parser.parse_args()
print(
"\n\n########### Running multiple classifiers at once does not work "
"with wandb! ###########\n\n"
)
min_num_tokens = 4
max_num_tokens = 300
wandb.config.cross_val = False
wandb.config.epochs = False
wandb.config.train_data = args.train_dev_data
wandb.config.test_data = args.test_data
wandb.config.max_length = max_num_tokens
wandb.config.min_length = min_num_tokens
print("Preparing training data ...")
(docs_train, labels_train, lang_train, _) = data.prepare_data(
args.train_dev_data,
min_num_tokens=min_num_tokens,
max_num_tokens=max_num_tokens,
)
print("\nPreparing test data ...")
(docs_test, labels_test, lang_test, _) = data.prepare_data(
args.test_data, min_num_tokens=min_num_tokens, max_num_tokens=max_num_tokens
)
results = []
classifier_descriptions = []
predictions_dict = {}
classifiers = args.mode
if "all" in args.mode:
classifiers = choices
classifiers.remove("all")
for classifier in classifiers:
# will be overwritten when using more than one model
wandb.config.update({"model_name": classifier}, allow_val_change=True)
if classifier == "majority":
result = run_majority_classifier(
train_data=docs_train,
train_labels=labels_train,
test_data=docs_test,
test_labels=labels_test,
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append("Classification according to majority class")
if classifier == "minority":
result = run_minority_classifier(
train_data=docs_train,
train_labels=labels_train,
test_data=docs_test,
test_labels=labels_test,
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append("Classification according to minority class")
if classifier == "uniform":
result = run_uniform_classifier(
train_data=docs_train,
train_labels=labels_train,
test_data=docs_test,
test_labels=labels_test,
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append(
"Classification according to uniform distribution"
)
if classifier == "language":
result = predict_according_to_language(
test_data=docs_test, test_labels=labels_test, language=lang_test
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append("Classification according to language")
if classifier == "nb_lang_len":
result = run_gaussian_naive_bayes(
train_data=docs_train,
train_labels=labels_train,
train_lang=lang_train,
test_data=docs_test,
test_labels=labels_test,
test_lang=lang_test,
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append("Gaussian NB on lang & len")
if classifier == "mnb":
result = run_multinomial_nb(
train_data=docs_train,
train_labels=labels_train,
test_data=docs_test,
test_labels=labels_test,
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append("Multinomial NB on char tf-idf")
if classifier == "nb_tfidf_lang_len":
result = run_NB_on_tfidf_lang_and_len(
train_data=docs_train,
train_labels=labels_train,
test_data=docs_test,
test_labels=labels_test,
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append("Multinomial NB on tf-idf, lang & length")
if classifier == "dt":
result = run_decision_tree_classifier(
train_data=docs_train,
train_labels=labels_train,
test_data=docs_test,
test_labels=labels_test,
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append("Decision Tree on char tf-idf")
if classifier == "svm_aligned":
result = run_svm_on_embeddings(
train_data=docs_train,
train_labels=labels_train,
test_data=docs_test,
test_labels=labels_test,
aligned_embeds=True,
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append("SVM on aligned embeddings")
if classifier == "svm_separated":
result = run_svm_on_embeddings(
train_data=docs_train,
train_labels=labels_train,
test_data=docs_test,
test_labels=labels_test,
aligned_embeds=False,
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append("SVM on 'separate' embeddings")
if classifier == "svm_bow":
result = run_svm_on_bow(
train_data=docs_train,
train_labels=labels_train,
test_data=docs_test,
test_labels=labels_test,
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append("SVM on BOW.")
if classifier == "svm_tfidf":
result = run_svm_on_tfidf(
train_data=docs_train,
train_labels=labels_train,
test_data=docs_test,
test_labels=labels_test,
)
predictions_dict[classifier] = result.y_pred
results.append(result)
classifier_descriptions.append("SVM on tf-idf")
if args.plot:
plot_results(results_list=results, descriptions=classifier_descriptions)
if args.store_predictions:
write_predictions_to_file(predictions_dict, docs_test, labels_test)