-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (68 loc) · 2.67 KB
/
main.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
#!/usr/bin/env python3
# coding: utf-8
"""
title: main.py
date: 2019-11-23
author: jskrable
description: Term project for CS777. Sarcasm detection in headlines.
"""
import sys
import train as tr
import prep as pp
import evaluation as ev
from timeit import default_timer as timer
from pyspark import SparkContext
from pyspark.sql import SQLContext, Row
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: main.py <size> <basedir> <output> ", file=sys.stderr)
exit(-1)
basedir = sys.argv[1]
size = int(sys.argv[2])
output = sys.argv[3]
sc = SparkContext(appName="Term Project: Sarcasm Detection")
sc.setLogLevel("ERROR")
sql = SQLContext(sc)
print('\nPRE-PROCESSING-----------------------------------------------------------')
start = timer()
s_prep = timer()
print('Reading dataset...')
# data = pp.get_source_data('./data')[:25000]
# data = pp.get_source_data(basedir)[:size]
data = pp.get_source_data(basedir)
print('Total observations: {}'.format(len(data)))
print('Sending to pre-processing...')
train_df, test_df = pp.preprocessing(sql, data)
e_prep = timer()
print('\nTRAINING-----------------------------------------------------------------')
s_train = timer()
models = ['naive_bayes','random_forest','linear_SVC','gradient_boosted_tree']
results = {m: tr.train_model(train_df, m) for m in models}
e_train = timer()
print('\nTESTING------------------------------------------------------------------')
s_test = timer()
print('Making predictions...')
for m in models:
preds = results[m]['model'].transform(test_df)
score = ev.binary_eval(preds)
metrics = ev.binary_metrics(
preds.select(['prediction','indexedLabel']).rdd.map(lambda x: (x[0],x[1]))
)
results[m].update({'score': score, 'metrics': metrics})
[print(m + ' model accuracy : {:2.6f}'.format(results[m]['score'])) for m in models]
[print(m + ' train time : {:2.6f}'.format(results[m]['train_time'])) for m in models]
e_test = timer()
ev.plot_results(results)
end = timer()
print('\nTIMING-------------------------------------------------------------------')
print('Input : {} seconds'.format(s_prep-start))
print('Prep : {} seconds'.format(e_prep-s_prep))
print('Training : {} seconds'.format(e_train-s_train))
print('Testing : {} seconds'.format(e_test-s_test))
print('----------------------------------------------')
print('Total : {} seconds'.format(end-start))
# print('\nTry it out!')
# take_input(nb_model)
# print('\nSaving model to ')
# rf_model.save(sys.argv[1])
sc.stop()