-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlydia_feng_assignment2.py
778 lines (623 loc) · 41.5 KB
/
lydia_feng_assignment2.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
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import GridSearchCV
from sklearn import svm
from nltk.stem.porter import *
from nltk.corpus import stopwords
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn.model_selection import StratifiedKFold
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import VotingClassifier
import nltk
import matplotlib.pyplot as plt
import seaborn as sns
#from IPython.display import Image
st = stopwords.words('english')
stemmer = PorterStemmer()
def loadDataAsDataFrame(f_path):
df = pd.read_csv(f_path)
return df
word_clusters = {}
def loadwordclusters():
infile = open('./50mpaths2.txt')
for line in infile:
items = str.strip(line).split()
class_ = items[0]
term = items[1]
word_clusters[term] = class_
return word_clusters
def getclusterfeatures(sent):
sent = sent.lower()
terms = nltk.word_tokenize(sent)
cluster_string = ''
for t in terms:
if t in word_clusters.keys():
cluster_string += 'clust_' + word_clusters[t] + '_clust '
return str.strip(cluster_string)
def preprocess_text(raw_text):
#stemming and lowercasing (no stopword removal
words = [stemmer.stem(w) for w in raw_text.lower().split()]
return (" ".join(words))
def grid_search_hyperparam_space(params, pipeline, folds, training_texts, training_classes):#folds, x_train, y_train, x_validation, y_validation):
grid_search = GridSearchCV(estimator=pipeline, param_grid=params, refit=True, cv=folds, return_train_score=False, scoring='f1_micro',n_jobs=-1)
grid_search.fit(training_texts, training_classes)
return grid_search
if __name__ == '__main__':
# Load the data
f_path = './pdfalls.csv'
data = loadDataAsDataFrame(f_path)
texts = data['fall_description']
classes = data['fall_class']
classes = classes.replace("BoS", "Other") #binary classification
age = data['age']
location = data['fall_location']
# ADD GENDER AS A FEATURE
gender = []
for row in data['female']:
if row == 'Female':
gender.append(str(0))
else:
gender.append(str(1))
#gender = pd.Series(gender)
# SPLIT THE DATA
training_set_size = int(0.8 * len(data))
training_data = data[:training_set_size]
training_texts = texts[:training_set_size]
training_classes = classes[:training_set_size]
training_age = age[:training_set_size]
training_gender = gender[:training_set_size]
training_location = location[:training_set_size]
test_data = data[training_set_size:]
test_texts = texts[training_set_size:]
test_classes = classes[training_set_size:]
test_age = age[training_set_size:]
test_gender = gender[training_set_size:]
test_location = location[training_set_size:]
# PREPROCESS THE DATA
training_texts_preprocessed = []
test_texts_preprocessed = []
test_clusters = []
training_clusters = []
training_length = []
test_length = []
training_age_preprocessed = []
test_age_preprocessed = []
word_clusters = loadwordclusters()
for tr in training_texts:
# you can do more with the training text here and generate more features...
training_texts_preprocessed.append(preprocess_text(tr))
training_clusters.append(getclusterfeatures(tr))
training_length.append(str(len(tr)))
for tt in test_texts:
test_texts_preprocessed.append(preprocess_text(tt))
test_clusters.append(getclusterfeatures(tt))
test_length.append(str(len(tt)))
for tr in training_age:
training_age_preprocessed.append(str(tr))
for tt in test_age:
test_age_preprocessed.append(str(tt))
# 10-FOLD CROSS VALIDATION
skf = StratifiedKFold(n_splits=10)
# FEATURE: NGRAMS
print("-------NGRAMS-------")
skf.get_n_splits(training_texts_preprocessed, training_classes)
for train_index, test_index in skf.split(training_texts_preprocessed, training_classes):
training_texts_preprocessed_train = map(training_texts_preprocessed.__getitem__, train_index)
training_texts_preprocessed_dev = map(training_texts_preprocessed.__getitem__, test_index)
ttp_train, ttp_test = training_classes[train_index], training_classes[test_index]
# VECTORIZER
vectorizer = CountVectorizer(ngram_range=(1, 3), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000)
training_data_vectors = vectorizer.fit_transform(training_texts_preprocessed_train).toarray()
test_data_vectors = vectorizer.transform(training_texts_preprocessed_dev).toarray()
print(".......")
# NAIVE BAYES CLASSIFIER
gnb = GaussianNB()
gnb_classifier = gnb.fit(training_data_vectors, ttp_train)
gnb_predictions = gnb.predict(test_data_vectors)
print("NAIVE BAYES f1-micro:", f1_score(ttp_test, gnb_predictions, average = 'micro'))
print("NAIVE BAYES f1-macro:", f1_score(ttp_test, gnb_predictions, average = 'macro'))
print("NAIVE BAYES accuracy:", accuracy_score(ttp_test, gnb_predictions))
# UNOPTIMIZED SVM CLASSIFIER
svm_unop = svm.SVC(C=1, cache_size=200,
coef0=0.0, degree=3, gamma='auto', kernel='linear', max_iter=-1, probability=True,
random_state=None, shrinking=True, tol=0.001, verbose=False)
svm_unop_classifier = svm_unop.fit(training_data_vectors, ttp_train)
svm_unop_predictions = svm_unop.predict(test_data_vectors)
print("UNOPTIMIZED SVM f1-micro:", f1_score(ttp_test, svm_unop_predictions, average = 'micro'))
print("UNOPTIMIZED SVM f1-macro:", f1_score(ttp_test, svm_unop_predictions, average = 'macro'))
print("UNOPTIMIZED SVM accuracy:", accuracy_score(ttp_test, svm_unop_predictions))
# RANDOM FOREST CLASSIFIER
rf = RandomForestClassifier(n_estimators=20, random_state=1)
rf_classifier = rf.fit(training_data_vectors, ttp_train)
rf_predictions = rf.predict(test_data_vectors)
print("RANDOM FOREST f1-micro:", f1_score(ttp_test, rf_predictions, average = 'micro'))
print("RANDOM FOREST f1-macro:", f1_score(ttp_test, rf_predictions, average = 'macro'))
print("RANDOM FOREST accuracy:", accuracy_score(ttp_test, rf_predictions))
# K NEAREST NEIGHBORS CLASSIFIER
grid_params = {
'knn__n_neighbors': [1, 2, 3, 4, 5],
}
knn = KNeighborsClassifier()
folds = 10
pipeline = Pipeline(steps=[('vec', vectorizer), ('knn', knn)])
grid = grid_search_hyperparam_space(grid_params, pipeline, folds, training_texts_preprocessed, training_classes)
n_neighbors_ = grid.best_params_['knn__n_neighbors']
knn_classifier = knn.fit(training_data_vectors, ttp_train)
knn_predictions = knn.predict(test_data_vectors)
print("K NEAREST NEIGHBORS f1-micro:", f1_score(ttp_test, knn_predictions, average = 'micro'))
print("K NEAREST NEIGHBORS f1-macro:", f1_score(ttp_test, knn_predictions, average = 'macro'))
print("K NEAREST NEIGHBORS accuracy:", accuracy_score(ttp_test, knn_predictions))
# LOGISTIC REGRESSION CLASSIFIER
lr = LogisticRegression(random_state=0)
lr_classifier = lr.fit(training_data_vectors, ttp_train)
lr_predictions = lr.predict(test_data_vectors)
print("LOGISTIC REGRESSION f1-micro:", f1_score(ttp_test, lr_predictions, average = 'micro'))
print("LOGISTIC REGRESSION f1-macro:", f1_score(ttp_test, lr_predictions, average = 'macro'))
print("LOGISTIC REGRESSION accuracy:", accuracy_score(ttp_test, lr_predictions))
# NEURAL NETWORK
nn = MLPClassifier(random_state=1, max_iter=300)
nn_classifier = nn.fit(training_data_vectors, ttp_train)
nn_predictions = nn.predict(test_data_vectors)
print("NEURAL NETWORK f1-micro:", f1_score(ttp_test, nn_predictions, average = 'micro'))
print("NEURAL NETWORK f1-macro:", f1_score(ttp_test, nn_predictions, average = 'macro'))
print("NEURAL NETWORK accuracy:", accuracy_score(ttp_test, nn_predictions))
# VOTING CLASSIFIER
ens = VotingClassifier(estimators=[('rf', rf), ('gnb', gnb), ('svm_unop', svm_unop)], voting='hard')
ens_classifier = ens.fit(training_data_vectors, ttp_train)
ens_predictions = ens.predict(test_data_vectors)
print("VOTING ENSEMBLE f1-micro:", f1_score(ttp_test, ens_predictions, average = 'micro'))
print("VOTING ENSEMBLE f1-macro:", f1_score(ttp_test, ens_predictions, average = 'macro'))
print("VOTING ENSEMBLE accuracy:", accuracy_score(ttp_test, ens_predictions))
# FEATURE: GENDER
print("-------GENDER-------")
skf.get_n_splits(training_gender, training_classes)
for train_index, test_index in skf.split(training_gender, training_classes):
training_gender_train = map(training_gender.__getitem__, train_index)
training_gender_dev = map(training_gender.__getitem__, test_index)
ttp_train, ttp_test = training_classes[train_index], training_classes[test_index]
vectorizer = CountVectorizer(ngram_range=(1, 1), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000, stop_words=None, token_pattern=r"(?u)\b\w+\b")
training_gender_vectors = vectorizer.fit_transform(training_gender_train).toarray()
test_gender_vectors = vectorizer.transform(training_gender_dev).toarray()
print(".......")
# NAIVE BAYES CLASSIFIER
gnb_gender_classifier = gnb.fit(training_gender_vectors, ttp_train)
gnb_gender_predictions = gnb.predict(test_gender_vectors)
print("NAIVE BAYES f1-micro:", f1_score(ttp_test, gnb_gender_predictions, average='micro'))
print("NAIVE BAYES f1-macro:", f1_score(ttp_test, gnb_gender_predictions, average='macro'))
print("NAIVE BAYES accuracy:", accuracy_score(ttp_test, gnb_gender_predictions))
# UNOPTIMIZED SVM CLASSIFIER
svm_unop_gender_classifier = svm_unop.fit(training_gender_vectors, ttp_train)
svm_unop_gender_predictions = svm_unop.predict(test_gender_vectors)
print("UNOPTIMIZED SVM f1-micro:", f1_score(ttp_test, svm_unop_gender_predictions, average='micro'))
print("UNOPTIMIZED SVM f1-macro:", f1_score(ttp_test, svm_unop_gender_predictions, average='macro'))
print("UNOPTIMIZED SVM accuracy:", accuracy_score(ttp_test, svm_unop_gender_predictions))
# RANDOM FOREST CLASSIFIER
rf_gender_classifier = rf.fit(training_gender_vectors, ttp_train)
rf_gender_predictions = rf.predict(test_gender_vectors)
print("RANDOM FOREST f1-micro:", f1_score(ttp_test, rf_gender_predictions, average='micro'))
print("RANDOM FOREST f1-macro:", f1_score(ttp_test, rf_gender_predictions, average='macro'))
print("RANDOM FOREST accuracy:", accuracy_score(ttp_test, rf_gender_predictions))
# K NEAREST NEIGHBORS CLASSIFIER
grid_params = {
'knn__n_neighbors': [1, 2, 3, 4, 5],
}
folds = 10
pipeline = Pipeline(steps=[('vec', vectorizer), ('knn', knn)])
grid = grid_search_hyperparam_space(grid_params, pipeline, folds, training_gender, training_classes)
n_neighbors_ = grid.best_params_['knn__n_neighbors']
knn_gender_classifier = knn.fit(training_gender_vectors, ttp_train)
knn_gender_predictions = knn.predict(test_gender_vectors)
print("K NEAREST NEIGHBORS f1-micro:", f1_score(ttp_test, knn_gender_predictions, average='micro'))
print("K NEAREST NEIGHBORS f1-macro:", f1_score(ttp_test, knn_gender_predictions, average='macro'))
print("K NEAREST NEIGHBORS accuracy:", accuracy_score(ttp_test, knn_gender_predictions))
print(n_neighbors_) # optimal n_neighbors = 3
# LOGISTIC REGRESSION CLASSIFIER
lr_gender_classifier = lr.fit(training_gender_vectors, ttp_train)
lr_gender_predictions = lr.predict(test_gender_vectors)
print("LOGISTIC REGRESSION f1-micro:", f1_score(ttp_test, lr_gender_predictions, average='micro'))
print("LOGISTIC REGRESSION f1-macro:", f1_score(ttp_test, lr_gender_predictions, average='macro'))
print("LOGISTIC REGRESSION accuracy:", accuracy_score(ttp_test, lr_gender_predictions))
# NEURAL NETWORK
nn_gender_classifier = nn.fit(training_gender_vectors, ttp_train)
nn_gender_predictions = nn.predict(test_gender_vectors)
print("NEURAL NETWORK f1-micro:", f1_score(ttp_test, nn_gender_predictions, average='micro'))
print("NEURAL NETWORK f1-macro:", f1_score(ttp_test, nn_gender_predictions, average='macro'))
print("NEURAL NETWORK accuracy:", accuracy_score(ttp_test, nn_gender_predictions))
# VOTING CLASSIFIER
ens = VotingClassifier(estimators=[('rf', rf), ('gnb', gnb), ('svm_unop', svm_unop)], voting='hard')
ens_gender_classifier = ens.fit(training_gender_vectors, ttp_train)
ens_gender_predictions = ens.predict(test_gender_vectors)
print("VOTING ENSEMBLE f1-micro:", f1_score(ttp_test, ens_gender_predictions, average='micro'))
print("VOTING ENSEMBLE f1-macro:", f1_score(ttp_test, ens_gender_predictions, average='macro'))
print("VOTING ENSEMBLE accuracy:", accuracy_score(ttp_test, ens_gender_predictions))
# FEATURE: CLUSTERS
print("-------CLUSTERS-------")
skf.get_n_splits(training_clusters, training_classes)
for train_index, test_index in skf.split(training_clusters, training_classes):
training_cluster_train = map(training_clusters.__getitem__, train_index)
training_cluster_dev = map(training_clusters.__getitem__, test_index)
ttp_train, ttp_test = training_classes[train_index], training_classes[test_index]
clustervectorizer = CountVectorizer(ngram_range=(1, 1), max_features=10000, stop_words=None, token_pattern=r"(?u)\b\w+\b")
training_cluster_vectors = clustervectorizer.fit_transform(training_cluster_train).toarray()
test_cluster_vectors = clustervectorizer.transform(training_cluster_dev).toarray()
print(".......")
# NAIVE BAYES CLASSIFIER
gnb_cluster_classifier = gnb.fit(training_cluster_vectors, ttp_train)
gnb_cluster_predictions = gnb.predict(test_cluster_vectors)
print("NAIVE BAYES f1-micro:", f1_score(ttp_test, gnb_cluster_predictions, average='micro'))
print("NAIVE BAYES f1-macro:", f1_score(ttp_test, gnb_cluster_predictions, average='macro'))
print("NAIVE BAYES accuracy:", accuracy_score(ttp_test, gnb_cluster_predictions))
# UNOPTIMIZED SVM CLASSIFIER
svm_unop_cluster_classifier = svm_unop.fit(training_cluster_vectors, ttp_train)
svm_unop_cluster_predictions = svm_unop.predict(test_cluster_vectors)
print("UNOPTIMIZED SVM f1-micro:", f1_score(ttp_test, svm_unop_cluster_predictions, average='micro'))
print("UNOPTIMIZED SVM f1-macro:", f1_score(ttp_test, svm_unop_cluster_predictions, average='macro'))
print("UNOPTIMIZED SVM accuracy:", accuracy_score(ttp_test, svm_unop_cluster_predictions))
# RANDOM FOREST CLASSIFIER
rf_cluster_classifier = rf.fit(training_cluster_vectors, ttp_train)
rf_cluster_predictions = rf.predict(test_cluster_vectors)
print("RANDOM FOREST f1-micro:", f1_score(ttp_test, rf_cluster_predictions, average='micro'))
print("RANDOM FOREST f1-macro:", f1_score(ttp_test, rf_cluster_predictions, average='macro'))
print("RANDOM FOREST accuracy:", accuracy_score(ttp_test, rf_cluster_predictions))
# K NEAREST NEIGHBORS CLASSIFIER
grid_params = {
'knn__n_neighbors': [1, 2, 3, 4, 5],
}
folds = 10
pipeline = Pipeline(steps=[('vec', vectorizer), ('knn', knn)])
grid = grid_search_hyperparam_space(grid_params, pipeline, folds, training_clusters, training_classes)
n_neighbors_ = grid.best_params_['knn__n_neighbors']
knn_cluster_classifier = knn.fit(training_cluster_vectors, ttp_train)
knn_cluster_predictions = knn.predict(test_cluster_vectors)
print("K NEAREST NEIGHBORS f1-micro:", f1_score(ttp_test, knn_cluster_predictions, average='micro'))
print("K NEAREST NEIGHBORS f1-macro:", f1_score(ttp_test, knn_cluster_predictions, average='macro'))
print("K NEAREST NEIGHBORS accuracy:", accuracy_score(ttp_test, knn_cluster_predictions))
print(n_neighbors_) # optimal n_neighbors = 3
# LOGISTIC REGRESSION CLASSIFIER
lr_cluster_classifier = lr.fit(training_cluster_vectors, ttp_train)
lr_cluster_predictions = lr.predict(test_cluster_vectors)
print("LOGISTIC REGRESSION f1-micro:", f1_score(ttp_test, lr_cluster_predictions, average='micro'))
print("LOGISTIC REGRESSION f1-macro:", f1_score(ttp_test, lr_cluster_predictions, average='macro'))
print("LOGISTIC REGRESSION accuracy:", accuracy_score(ttp_test, lr_cluster_predictions))
# NEURAL NETWORK
nn_cluster_classifier = nn.fit(training_cluster_vectors, ttp_train)
nn_cluster_predictions = nn.predict(test_cluster_vectors)
print("NEURAL NETWORK f1-micro:", f1_score(ttp_test, nn_cluster_predictions, average='micro'))
print("NEURAL NETWORK f1-macro:", f1_score(ttp_test, nn_cluster_predictions, average='macro'))
print("NEURAL NETWORK accuracy:", accuracy_score(ttp_test, nn_cluster_predictions))
# VOTING CLASSIFIER
ens = VotingClassifier(estimators=[('rf', rf), ('gnb', gnb), ('svm_unop', svm_unop)], voting='hard')
ens_cluster_classifier = ens.fit(training_cluster_vectors, ttp_train)
ens_cluster_predictions = ens.predict(test_cluster_vectors)
print("VOTING ENSEMBLE f1-micro:", f1_score(ttp_test, ens_cluster_predictions, average='micro'))
print("VOTING ENSEMBLE f1-macro:", f1_score(ttp_test, ens_cluster_predictions, average='macro'))
print("VOTING ENSEMBLE accuracy:", accuracy_score(ttp_test, ens_cluster_predictions))
# FEATURE: length
print("-------LENGTH-------")
skf.get_n_splits(training_length, training_classes)
for train_index, test_index in skf.split(training_length, training_classes):
training_length_train = map(training_length.__getitem__, train_index)
training_length_dev = map(training_length.__getitem__, test_index)
ttp_train, ttp_test = training_classes[train_index], training_classes[test_index]
# VECTORIZER
vectorizer = CountVectorizer(ngram_range=(1, 1), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000, token_pattern=r"(?u)\b\w+\b")
training_length_vectors = vectorizer.fit_transform(training_length_train).toarray()
test_length_vectors = vectorizer.transform(training_length_dev).toarray()
print(".......")
# NAIVE BAYES CLASSIFIER
gnb_length_classifier = gnb.fit(training_length_vectors, ttp_train)
gnb_length_predictions = gnb.predict(test_length_vectors)
print("NAIVE BAYES f1-micro:", f1_score(ttp_test, gnb_length_predictions, average='micro'))
print("NAIVE BAYES f1-macro:", f1_score(ttp_test, gnb_length_predictions, average='macro'))
print("NAIVE BAYES accuracy:", accuracy_score(ttp_test, gnb_length_predictions))
# UNOPTIMIZED SVM CLASSIFIER
svm_unop_length_classifier = svm_unop.fit(training_length_vectors, ttp_train)
svm_unop_length_predictions = svm_unop.predict(test_length_vectors)
print("UNOPTIMIZED SVM f1-micro:", f1_score(ttp_test, svm_unop_length_predictions, average='micro'))
print("UNOPTIMIZED SVM f1-macro:", f1_score(ttp_test, svm_unop_length_predictions, average='macro'))
print("UNOPTIMIZED SVM accuracy:", accuracy_score(ttp_test, svm_unop_length_predictions))
# RANDOM FOREST CLASSIFIER
rf_length_classifier = rf.fit(training_length_vectors, ttp_train)
rf_length_predictions = rf.predict(test_length_vectors)
print("RANDOM FOREST f1-micro:", f1_score(ttp_test, rf_length_predictions, average='micro'))
print("RANDOM FOREST f1-macro:", f1_score(ttp_test, rf_length_predictions, average='macro'))
print("RANDOM FOREST accuracy:", accuracy_score(ttp_test, rf_length_predictions))
# K NEAREST NEIGHBORS CLASSIFIER
grid_params = {
'knn__n_neighbors': [1, 2, 3, 4, 5],
}
folds = 10
pipeline = Pipeline(steps=[('vec', vectorizer), ('knn', knn)])
grid = grid_search_hyperparam_space(grid_params, pipeline, folds, training_length, training_classes)
n_neighbors_ = grid.best_params_['knn__n_neighbors']
knn_length_classifier = knn.fit(training_length_vectors, ttp_train)
knn_length_predictions = knn.predict(test_length_vectors)
print("K NEAREST NEIGHBORS f1-micro:", f1_score(ttp_test, knn_length_predictions, average='micro'))
print("K NEAREST NEIGHBORS f1-macro:", f1_score(ttp_test, knn_length_predictions, average='macro'))
print("K NEAREST NEIGHBORS accuracy:", accuracy_score(ttp_test, knn_length_predictions))
print(n_neighbors_) # optimal n_neighbors = 3
# LOGISTIC REGRESSION CLASSIFIER
lr_length_classifier = lr.fit(training_length_vectors, ttp_train)
lr_length_predictions = lr.predict(test_length_vectors)
print("LOGISTIC REGRESSION f1-micro:", f1_score(ttp_test, lr_length_predictions, average='micro'))
print("LOGISTIC REGRESSION f1-macro:", f1_score(ttp_test, lr_length_predictions, average='macro'))
print("LOGISTIC REGRESSION accuracy:", accuracy_score(ttp_test, lr_length_predictions))
# NEURAL NETWORK
nn_length_classifier = nn.fit(training_length_vectors, ttp_train)
nn_length_predictions = nn.predict(test_length_vectors)
print("NEURAL NETWORK f1-micro:", f1_score(ttp_test, nn_length_predictions, average='micro'))
print("NEURAL NETWORK f1-macro:", f1_score(ttp_test, nn_length_predictions, average='macro'))
print("NEURAL NETWORK accuracy:", accuracy_score(ttp_test, nn_length_predictions))
# VOTING CLASSIFIER
ens = VotingClassifier(estimators=[('rf', rf), ('gnb', gnb), ('svm_unop', svm_unop)], voting='hard')
ens_length_classifier = ens.fit(training_length_vectors, ttp_train)
ens_length_predictions = ens.predict(test_length_vectors)
print("VOTING ENSEMBLE f1-micro:", f1_score(ttp_test, ens_length_predictions, average='micro'))
print("VOTING ENSEMBLE f1-macro:", f1_score(ttp_test, ens_length_predictions, average='macro'))
print("VOTING ENSEMBLE accuracy:", accuracy_score(ttp_test, ens_length_predictions))
print("-------AGE-------")
# FEATURE: AGE
skf.get_n_splits(training_age_preprocessed, training_classes)
for train_index, test_index in skf.split(training_age_preprocessed, training_classes):
training_age_train = map(training_age_preprocessed.__getitem__, train_index)
training_age_dev = map(training_age_preprocessed.__getitem__, test_index)
ttp_train, ttp_test = training_classes[train_index], training_classes[test_index]
# VECTORIZER
vectorizer = CountVectorizer(ngram_range=(1, 1), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000, token_pattern=r"(?u)\b\w+\b")
training_age_vectors = vectorizer.fit_transform(training_age_train).toarray()
test_age_vectors = vectorizer.transform(training_age_dev).toarray()
print(".......")
# NAIVE BAYES CLASSIFIER
gnb_age_classifier = gnb.fit(training_age_vectors, ttp_train)
gnb_age_predictions = gnb.predict(test_age_vectors)
print("NAIVE BAYES f1-micro:", f1_score(ttp_test, gnb_age_predictions, average='micro'))
print("NAIVE BAYES f1-macro:", f1_score(ttp_test, gnb_age_predictions, average='macro'))
print("NAIVE BAYES accuracy:", accuracy_score(ttp_test, gnb_age_predictions))
# UNOPTIMIZED SVM CLASSIFIER
svm_unop_age_classifier = svm_unop.fit(training_age_vectors, ttp_train)
svm_unop_age_predictions = svm_unop.predict(test_age_vectors)
print("UNOPTIMIZED SVM f1-micro:", f1_score(ttp_test, svm_unop_age_predictions, average='micro'))
print("UNOPTIMIZED SVM f1-macro:", f1_score(ttp_test, svm_unop_age_predictions, average='macro'))
print("UNOPTIMIZED SVM accuracy:", accuracy_score(ttp_test, svm_unop_age_predictions))
# RANDOM FOREST CLASSIFIER
rf_age_classifier = rf.fit(training_age_vectors, ttp_train)
rf_age_predictions = rf.predict(test_age_vectors)
print("RANDOM FOREST f1-micro:", f1_score(ttp_test, rf_age_predictions, average='micro'))
print("RANDOM FOREST f1-macro:", f1_score(ttp_test, rf_age_predictions, average='macro'))
print("RANDOM FOREST accuracy:", accuracy_score(ttp_test, rf_age_predictions))
# K NEAREST NEIGHBORS CLASSIFIER
grid_params = {
'knn__n_neighbors': [1, 2, 3, 4, 5],
}
folds = 10
pipeline = Pipeline(steps=[('vec', vectorizer), ('knn', knn)])
grid = grid_search_hyperparam_space(grid_params, pipeline, folds, training_age_preprocessed, training_classes)
n_neighbors_ = grid.best_params_['knn__n_neighbors']
knn_age_classifier = knn.fit(training_age_vectors, ttp_train)
knn_age_predictions = knn.predict(test_age_vectors)
print("K NEAREST NEIGHBORS f1-micro:", f1_score(ttp_test, knn_age_predictions, average='micro'))
print("K NEAREST NEIGHBORS f1-macro:", f1_score(ttp_test, knn_age_predictions, average='macro'))
print("K NEAREST NEIGHBORS accuracy:", accuracy_score(ttp_test, knn_age_predictions))
print(n_neighbors_) # optimal n_neighbors = 3
# LOGISTIC REGRESSION CLASSIFIER
lr_age_classifier = lr.fit(training_age_vectors, ttp_train)
lr_age_predictions = lr.predict(test_age_vectors)
print("LOGISTIC REGRESSION f1-micro:", f1_score(ttp_test, lr_age_predictions, average='micro'))
print("LOGISTIC REGRESSION f1-macro:", f1_score(ttp_test, lr_age_predictions, average='macro'))
print("LOGISTIC REGRESSION accuracy:", accuracy_score(ttp_test, lr_age_predictions))
# NEURAL NETWORK
nn_age_classifier = nn.fit(training_age_vectors, ttp_train)
nn_age_predictions = nn.predict(test_age_vectors)
print("NEURAL NETWORK f1-micro:", f1_score(ttp_test, nn_age_predictions, average='micro'))
print("NEURAL NETWORK f1-macro:", f1_score(ttp_test, nn_age_predictions, average='macro'))
print("NEURAL NETWORK accuracy:", accuracy_score(ttp_test, nn_age_predictions))
# VOTING CLASSIFIER
ens = VotingClassifier(estimators=[('rf', rf), ('gnb', gnb), ('svm_unop', svm_unop)], voting='hard')
ens_age_classifier = ens.fit(training_age_vectors, ttp_train)
ens_age_predictions = ens.predict(test_age_vectors)
print("VOTING ENSEMBLE f1-micro:", f1_score(ttp_test, ens_age_predictions, average='micro'))
print("VOTING ENSEMBLE f1-macro:", f1_score(ttp_test, ens_age_predictions, average='macro'))
print("VOTING ENSEMBLE accuracy:", accuracy_score(ttp_test, ens_age_predictions))
# APPENDED FEATURES
skf.get_n_splits(training_texts_preprocessed, training_classes)
for train_index, test_index in skf.split(training_texts_preprocessed, training_classes):
training_texts_preprocessed_train = map(training_texts_preprocessed.__getitem__, train_index)
training_texts_preprocessed_dev = map(training_texts_preprocessed.__getitem__, test_index)
ttp_train, ttp_test = training_classes[train_index], training_classes[test_index]
# VECTORIZER
vectorizer = CountVectorizer(ngram_range=(1, 3), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000)
training_data_vectors = vectorizer.fit_transform(training_texts_preprocessed_train).toarray()
test_data_vectors = vectorizer.transform(training_texts_preprocessed_dev).toarray()
training_gender_train = map(training_gender.__getitem__, train_index)
training_gender_dev = map(training_gender.__getitem__, test_index)
gender_vectorizer = CountVectorizer(ngram_range=(1, 1), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000, stop_words=None, token_pattern=r"(?u)\b\w+\b")
training_gender_vectors = gender_vectorizer.fit_transform(training_gender_train).toarray()
test_gender_vectors = gender_vectorizer.transform(training_gender_dev).toarray()
training_data_vectors = np.concatenate((training_data_vectors, training_gender_vectors), axis=1)
test_data_vectors = np.concatenate((test_data_vectors, test_gender_vectors), axis=1)
training_cluster_train = map(training_clusters.__getitem__, train_index)
training_cluster_dev = map(training_clusters.__getitem__, test_index)
clustervectorizer = CountVectorizer(ngram_range=(1, 1), max_features=10000, stop_words=None,
token_pattern=r"(?u)\b\w+\b")
training_cluster_vectors = clustervectorizer.fit_transform(training_cluster_train).toarray()
test_cluster_vectors = clustervectorizer.transform(training_cluster_dev).toarray()
training_data_vectors = np.concatenate((training_data_vectors, training_cluster_vectors), axis=1)
test_data_vectors = np.concatenate((test_data_vectors, test_cluster_vectors), axis=1)
training_length_train = map(training_length.__getitem__, train_index)
training_length_dev = map(training_length.__getitem__, test_index)
length_vectorizer = CountVectorizer(ngram_range=(1, 1), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000, token_pattern=r"(?u)\b\w+\b")
training_length_vectors = length_vectorizer.fit_transform(training_length_train).toarray()
test_length_vectors = length_vectorizer.transform(training_length_dev).toarray()
training_data_vectors = np.concatenate((training_data_vectors, training_length_vectors), axis=1)
test_data_vectors = np.concatenate((test_data_vectors, test_length_vectors), axis=1)
training_age_train = map(training_age_preprocessed.__getitem__, train_index)
training_age_dev = map(training_age_preprocessed.__getitem__, test_index)
age_vectorizer = CountVectorizer(ngram_range=(1, 1), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000, token_pattern=r"(?u)\b\w+\b")
training_age_vectors = age_vectorizer.fit_transform(training_age_train).toarray()
test_age_vectors = age_vectorizer.transform(training_age_dev).toarray()
training_data_vectors = np.concatenate((training_data_vectors, training_age_vectors), axis=1)
test_data_vectors = np.concatenate((test_data_vectors, test_age_vectors), axis=1)
print(".......")
# NAIVE BAYES CLASSIFIER
gnb = GaussianNB()
gnb_classifier = gnb.fit(training_data_vectors, ttp_train)
gnb_predictions = gnb.predict(test_data_vectors)
print("NAIVE BAYES f1-micro:", f1_score(ttp_test, gnb_predictions, average='micro'))
print("NAIVE BAYES f1-macro:", f1_score(ttp_test, gnb_predictions, average='macro'))
print("NAIVE BAYES accuracy:", accuracy_score(ttp_test, gnb_predictions))
# UNOPTIMIZED SVM CLASSIFIER
svm_unop = svm.SVC(C=1, cache_size=200,
coef0=0.0, degree=3, gamma='auto', kernel='linear', max_iter=-1, probability=True,
random_state=None, shrinking=True, tol=0.001, verbose=False)
svm_unop_classifier = svm_unop.fit(training_data_vectors, ttp_train)
svm_unop_predictions = svm_unop.predict(test_data_vectors)
print("UNOPTIMIZED SVM f1-micro:", f1_score(ttp_test, svm_unop_predictions, average='micro'))
print("UNOPTIMIZED SVM f1-macro:", f1_score(ttp_test, svm_unop_predictions, average='macro'))
print("UNOPTIMIZED SVM accuracy:", accuracy_score(ttp_test, svm_unop_predictions))
# RANDOM FOREST CLASSIFIER
rf = RandomForestClassifier(n_estimators=20, random_state=1)
rf_classifier = rf.fit(training_data_vectors, ttp_train)
rf_predictions = rf.predict(test_data_vectors)
print("RANDOM FOREST f1-micro:", f1_score(ttp_test, rf_predictions, average='micro'))
print("RANDOM FOREST f1-macro:", f1_score(ttp_test, rf_predictions, average='macro'))
print("RANDOM FOREST accuracy:", accuracy_score(ttp_test, rf_predictions))
# K NEAREST NEIGHBORS CLASSIFIER
grid_params = {
'knn__n_neighbors': [1, 2, 3, 4, 5],
}
knn = KNeighborsClassifier()
folds = 10
pipeline = Pipeline(steps=[('vec', vectorizer), ('knn', knn)])
grid = grid_search_hyperparam_space(grid_params, pipeline, folds, training_texts_preprocessed, training_classes)
n_neighbors_ = grid.best_params_['knn__n_neighbors']
knn_classifier = knn.fit(training_data_vectors, ttp_train)
knn_predictions = knn.predict(test_data_vectors)
print("K NEAREST NEIGHBORS f1-micro:", f1_score(ttp_test, knn_predictions, average='micro'))
print("K NEAREST NEIGHBORS f1-macro:", f1_score(ttp_test, knn_predictions, average='macro'))
print("K NEAREST NEIGHBORS accuracy:", accuracy_score(ttp_test, knn_predictions))
# LOGISTIC REGRESSION CLASSIFIER
lr = LogisticRegression(random_state=0)
lr_classifier = lr.fit(training_data_vectors, ttp_train)
lr_predictions = lr.predict(test_data_vectors)
print("LOGISTIC REGRESSION f1-micro:", f1_score(ttp_test, lr_predictions, average='micro'))
print("LOGISTIC REGRESSION f1-macro:", f1_score(ttp_test, lr_predictions, average='macro'))
print("LOGISTIC REGRESSION accuracy:", accuracy_score(ttp_test, lr_predictions))
# NEURAL NETWORK
nn = MLPClassifier(random_state=1, max_iter=300)
nn_classifier = nn.fit(training_data_vectors, ttp_train)
nn_predictions = nn.predict(test_data_vectors)
print("NEURAL NETWORK f1-micro:", f1_score(ttp_test, nn_predictions, average='micro'))
print("NEURAL NETWORK f1-macro:", f1_score(ttp_test, nn_predictions, average='macro'))
print("NEURAL NETWORK accuracy:", accuracy_score(ttp_test, nn_predictions))
# VOTING CLASSIFIER
ens = VotingClassifier(estimators=[('rf', rf), ('gnb', gnb), ('svm_unop', svm_unop)], voting='hard')
ens_classifier = ens.fit(training_data_vectors, ttp_train)
ens_predictions = ens.predict(test_data_vectors)
print("VOTING ENSEMBLE f1-micro:", f1_score(ttp_test, ens_predictions, average='micro'))
print("VOTING ENSEMBLE f1-macro:", f1_score(ttp_test, ens_predictions, average='macro'))
print("VOTING ENSEMBLE accuracy:", accuracy_score(ttp_test, ens_predictions))
# CONCATENATED FEATURES
vectorizer = CountVectorizer(ngram_range=(1, 3), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000)
training_data_vectors = vectorizer.fit_transform(training_texts_preprocessed).toarray()
test_data_vectors = vectorizer.transform(test_texts_preprocessed).toarray()
gender_vectorizer = CountVectorizer(ngram_range=(1, 1), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000, stop_words=None, token_pattern=r"(?u)\b\w+\b")
training_gender_vectors = gender_vectorizer.fit_transform(training_gender).toarray()
test_gender_vectors = gender_vectorizer.transform(test_gender).toarray()
training_data_vectors = np.concatenate((training_data_vectors, training_gender_vectors), axis=1)
test_data_vectors = np.concatenate((test_data_vectors, test_gender_vectors), axis=1)
clustervectorizer = CountVectorizer(ngram_range=(1, 1), max_features=10000, stop_words=None,
token_pattern=r"(?u)\b\w+\b")
training_cluster_vectors = clustervectorizer.fit_transform(training_clusters).toarray()
test_cluster_vectors = clustervectorizer.transform(test_clusters).toarray()
training_data_vectors = np.concatenate((training_data_vectors, training_cluster_vectors), axis=1)
test_data_vectors = np.concatenate((test_data_vectors, test_cluster_vectors), axis=1)
length_vectorizer = CountVectorizer(ngram_range=(1, 1), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000, token_pattern=r"(?u)\b\w+\b")
training_length_vectors = length_vectorizer.fit_transform(training_length).toarray()
test_length_vectors = length_vectorizer.transform(test_length).toarray()
'''
training_data_vectors = np.concatenate((training_data_vectors, training_length_vectors), axis=1)
test_data_vectors = np.concatenate((test_data_vectors, test_length_vectors), axis=1)
'''
age_vectorizer = CountVectorizer(ngram_range=(1, 1), analyzer="word", tokenizer=None, preprocessor=None,
max_features=10000, token_pattern=r"(?u)\b\w+\b")
training_age_vectors = age_vectorizer.fit_transform(training_age_preprocessed).toarray()
test_age_vectors = age_vectorizer.transform(test_age_preprocessed).toarray()
training_data_vectors = np.concatenate((training_data_vectors, training_age_vectors), axis=1)
test_data_vectors = np.concatenate((test_data_vectors, test_age_vectors), axis=1)
print(".......")
# NAIVE BAYES CLASSIFIER
gnb = GaussianNB()
gnb_classifier = gnb.fit(training_data_vectors, training_classes)
gnb_predictions = gnb.predict(test_data_vectors)
print("NAIVE BAYES f1-micro:", f1_score(test_classes, gnb_predictions, average='micro'))
print("NAIVE BAYES f1-macro:", f1_score(test_classes, gnb_predictions, average='macro'))
print("NAIVE BAYES accuracy:", accuracy_score(test_classes, gnb_predictions))
# UNOPTIMIZED SVM CLASSIFIER
svm_unop = svm.SVC(C=1, cache_size=200,
coef0=0.0, degree=3, gamma='auto', kernel='linear', max_iter=-1, probability=True,
random_state=None, shrinking=True, tol=0.001, verbose=False)
svm_unop_classifier = svm_unop.fit(training_data_vectors, training_classes)
svm_unop_predictions = svm_unop.predict(test_data_vectors)
print("UNOPTIMIZED SVM f1-micro:", f1_score(test_classes, svm_unop_predictions, average='micro'))
print("UNOPTIMIZED SVM f1-macro:", f1_score(test_classes, svm_unop_predictions, average='macro'))
print("UNOPTIMIZED SVM accuracy:", accuracy_score(test_classes, svm_unop_predictions))
# RANDOM FOREST CLASSIFIER
rf = RandomForestClassifier(n_estimators=20, random_state=1)
rf_classifier = rf.fit(training_data_vectors, training_classes)
rf_predictions = rf.predict(test_data_vectors)
print("RANDOM FOREST f1-micro:", f1_score(test_classes, rf_predictions, average='micro'))
print("RANDOM FOREST f1-macro:", f1_score(test_classes, rf_predictions, average='macro'))
print("RANDOM FOREST accuracy:", accuracy_score(test_classes, rf_predictions))
# K NEAREST NEIGHBORS CLASSIFIER
grid_params = {
'knn__n_neighbors': [1, 2, 3, 4, 5],
}
knn = KNeighborsClassifier()
folds = 10
pipeline = Pipeline(steps=[('vec', vectorizer), ('knn', knn)])
grid = grid_search_hyperparam_space(grid_params, pipeline, folds, training_texts_preprocessed, training_classes)
n_neighbors_ = grid.best_params_['knn__n_neighbors']
knn_classifier = knn.fit(training_data_vectors, training_classes)
knn_predictions = knn.predict(test_data_vectors)
print("K NEAREST NEIGHBORS f1-micro:", f1_score(test_classes, knn_predictions, average='micro'))
print("K NEAREST NEIGHBORS f1-macro:", f1_score(test_classes, knn_predictions, average='macro'))
print("K NEAREST NEIGHBORS accuracy:", accuracy_score(test_classes, knn_predictions))
# LOGISTIC REGRESSION CLASSIFIER
lr = LogisticRegression(random_state=0)
lr_classifier = lr.fit(training_data_vectors, training_classes)
lr_predictions = lr.predict(test_data_vectors)
print("LOGISTIC REGRESSION f1-micro:", f1_score(test_classes, lr_predictions, average='micro'))
print("LOGISTIC REGRESSION f1-macro:", f1_score(test_classes, lr_predictions, average='macro'))
print("LOGISTIC REGRESSION accuracy:", accuracy_score(test_classes, lr_predictions))
# NEURAL NETWORK
nn = MLPClassifier(random_state=1, max_iter=300)
nn_classifier = nn.fit(training_data_vectors, training_classes)
nn_predictions = nn.predict(test_data_vectors)
print("NEURAL NETWORK f1-micro:", f1_score(test_classes, nn_predictions, average='micro'))
print("NEURAL NETWORK f1-macro:", f1_score(test_classes, nn_predictions, average='macro'))
print("NEURAL NETWORK accuracy:", accuracy_score(test_classes, nn_predictions))
# VOTING CLASSIFIER
ens = VotingClassifier(estimators=[('rf', rf), ('gnb', gnb), ('svm_unop', svm_unop)], voting='hard')
ens_classifier = ens.fit(training_data_vectors, training_classes)
ens_predictions = ens.predict(test_data_vectors)
print("VOTING ENSEMBLE f1-micro:", f1_score(test_classes, ens_predictions, average='micro'))
print("VOTING ENSEMBLE f1-macro:", f1_score(test_classes, ens_predictions, average='macro'))
print("VOTING ENSEMBLE accuracy:", accuracy_score(test_classes, ens_predictions))