forked from andreiapostoae/dota2-predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretrain.py
92 lines (66 loc) · 2.27 KB
/
pretrain.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
""" Pretrains models in MIN_MMR, MAX_MMR range in iterations of 100 """
import csv
import sys
import logging
from preprocessing.prepare_data import DataPreprocess
from training.logistic_regression import LogReg
from training.evaluate import evaluate_model
FOLDER = "pretrained"
MIN_MMR = 2000
MAX_MMR = 4300
def process_row(row):
""" Rounds accuracy, AUC, F1-score to 3 decimals
row -- list of elements in a csv row
"""
row[:] = [round(x, 3) for x in row]
def filter_and_train(mmr, offset):
""" Filters games within (mmr - offset, mmr + offset), trains the model
and writes the results in FOLDER/results.csv
mmr -- target mmr
offset -- gives the range of search
"""
logging.basicConfig(level=logging.INFO, format='%(name)-30s %(levelname)-8s %(message)s')
logger = logging.getLogger(__name__)
try:
in_file = open(sys.argv[1], "rt")
except IOError:
sys.exit("Usage: %s input_file offset" % sys.argv[0])
csv_reader = csv.reader(in_file, delimiter=",")
full_list = list(csv_reader)
logger.info("%d - %d range", mmr - offset, mmr + offset)
data_preprocess = DataPreprocess(full_list, mmr, offset=offset)
filtered_list = data_preprocess.run()
model_name = FOLDER + "/" + str(mmr)
logreg = LogReg(filtered_list, mmr, offset, output_model=model_name)
[model, data_list] = logreg.run()
results = evaluate_model(model, data_list)
process_row(results)
results.insert(0, "%d - %d" % (mmr - offset, mmr + offset))
in_file.close()
return results
def main():
""" Main function """
logging.basicConfig(level=logging.INFO, format='%(name)-30s %(levelname)-8s %(message)s')
logger = logging.getLogger(__name__)
try:
offset = int(sys.argv[2])
except ValueError:
logger.critical("Usage: %s input_file offset", sys.argv[0])
sys.exit(1)
output_path = FOLDER + "/results.csv"
try:
output_file = open(output_path, "wt")
except IOError:
logger.critical("Could not open %s", output_path)
sys.exit(1)
csv_writer = csv.writer(output_file, delimiter=",")
csv_writer.writerow(['MMR', 'Data set size', 'Accuracy', 'AUC score', 'F1-score'])
mmrs = []
for i in range((MAX_MMR - MIN_MMR) / 100):
mmrs.append(MIN_MMR + i * 100)
for mmr in mmrs:
results = filter_and_train(mmr, offset)
csv_writer.writerow(results)
output_file.close()
if __name__ == "__main__":
main()