-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
65 lines (48 loc) · 2.18 KB
/
run.py
File metadata and controls
65 lines (48 loc) · 2.18 KB
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
import csv
import glob
import numpy as np
from src.init import*
from src.utils import*
from src.train_or_evaluate import preprocess_dataset
def major_vote():
"""
Performs majority voting on prediction files + creates a final submission file
"""
pred_f = glob.glob('models/neural_netwroks/predictions/*.csv')
preds = np.zeros((10000, 2))
for file in pred_f:
with open(file, 'r') as f:
lines = f.readlines()[1:]
currents = np.array([int(l.split(',')[1]) for l in lines])
currents[currents < 0] = 0
preds[range(10000), currents] += 1
preds = np.argmax(preds, axis=1)
preds[preds < 1 ] = -1
path = os.path.join('models/neural_networks/predictions', f'pred_cnn.csv')
create_submission(preds, path)
return 0
def create_submission(y_pred, path):
ids=[i for i in range(1, len(y_pred)+1)]
with open(path, 'w', newline='') as csvfile:
fileds = ['Id', 'Prediction']
w = csv.DictWriter(csvfile, delimiter=",", fieldnames=fileds)
w.writeheader()
for r1, r2 in zip(ids, y_pred):
w.writerow({'Id':int(r1),'Prediction':int(r2)})
if __name__ == '__main__':
cnn = load_best("cnn", 'normal')
# lstm = load_best("lstm", 'normal')
# bi_lstm = load_best("bi_lstm", 'normal')
# gru = load_best("gru", 'normal')
to_pred = preprocess_tweets_to_predict()
_, _, _, _, _, tokenizer, _, _, _ = preprocess_dataset('normal', 200, 100)
pred_cnn = compute_model_predictions(to_pred, cnn, tokenizer, maxlen=100)
# pred_lstm = compute_model_predictions(to_pred, lstm, tokenizer, maxlen=50)
# pred_bi_lstm = compute_model_predictions(to_pred, bi_lstm, tokenizer, maxlen=100)
# pred_gru = compute_model_predictions(to_pred, gru, tokenizer, maxlen=100)
path = os.path.join('models/neural_networks/predictions', f'pred_cnn.csv')
create_submission(pred_cnn, path)
# create_submission(pred_lstm, "'models/neural_netwroks/predictions/pred_lstm.csv")
# create_submission(pred_bi_lstm, "'models/neural_netwroks/predictions/pred_bi_lstm.csv")
# create_submission(pred_gru, "'models/neural_netwroks/predictions/pred_gru.csv")
major_vote()