-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
152 lines (139 loc) · 6.51 KB
/
train.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
import math
from sklearn.model_selection import train_test_split
import xgboost as xgb
import optuna
import data
dataset = data.load_dir('data')
maxtime = max(dataset.one_hot['epochseconds'])
mintime = min(dataset.one_hot['epochseconds'])
maxdays = math.ceil((maxtime - mintime)/86400)
best_loss = 1e12
best_loss_error = 1.0
best_error = 1.0
def update(error, loss):
global best_loss, best_loss_error, best_error
if loss < best_loss:
best_loss = loss
best_loss_error = error
if error < best_error:
best_error = error
print(f"best loss {best_loss} best loss error {best_loss_error} best error {best_error}")
def get_columns(dataset, additional_drop=None):
always_dropped = ['outcome_victory', 'epochseconds', 'uptime']
if additional_drop is not None:
always_dropped += additional_drop
def incolumn(column):
for drop in always_dropped:
if drop in column:
return True
return False
return [column for column in dataset.one_hot.columns if not incolumn(column)]
def select_features(df, features):
dropped_features = [column for column in df.columns if (column not in features) or features[column] == 0]
assert 'epochseconds' in dropped_features and 'outcome_victory' not in df.columns
pruned = df.drop(dropped_features, axis='columns')
return pruned
def testcv(params):
print(f"loaded {len(dataset.dataframe)}")
dfWindow = dataset.one_hot[dataset.one_hot['epochseconds'] > maxtime - (params['limit_days']*86400)]
print(f"window {len(dfWindow)}")
if not len(dfWindow):
return 1e12
dfX = select_features(dfWindow.drop('outcome_victory', axis=1), params)
dfY = dfWindow['outcome_victory']
assert len(dfX) == len(dfY)
d = xgb.DMatrix(dfX, label=dfY)
print(dfX.columns)
result = xgb.cv(params, d, num_boost_round=params['num_boost_round'], nfold=10, shuffle=True, metrics=['error', 'logloss'])
error = result['test-error-mean'][params['num_boost_round']-1]
loss = result['test-logloss-mean'][params['num_boost_round']-1]
print(error, loss)
update(error, loss)
return loss
def testtv(params):
days = 5
print(f"loaded {len(dataset.dataframe)}")
dfWindow = dataset.one_hot[dataset.one_hot['epochseconds'] > maxtime - (params['limit_days']*86400)]
print(f"window {len(dfWindow)}")
dfTrain = dfWindow[dfWindow['epochseconds'] < maxtime - (days*86400)]
dfTest = dfWindow[dfWindow['epochseconds'] >= maxtime - (days*86400)]
if not (len(dfTrain) and len(dfTest)):
return 1e12
assert len(dfTrain) + len(dfTest) == len(dfWindow)
dfXTrain = select_features(dfTrain.drop('outcome_victory', axis=1), params)
dfYTrain = dfTrain['outcome_victory']
assert len(dfXTrain) == len(dfYTrain)
dfXTest = select_features(dfTest.drop('outcome_victory', axis=1), params)
dfYTest = dfTest['outcome_victory']
assert len(dfXTest) == len(dfYTest)
dTrain = xgb.DMatrix(dfXTrain, label=dfYTrain)
dTest = xgb.DMatrix(dfXTest, label=dfYTest)
evals_result = dict()
print(dfXTrain.columns)
result = xgb.train(params, dTrain, num_boost_round=params['num_boost_round'], evals=[(dTest, 'test')], evals_result=evals_result, verbose_eval=False)
error = evals_result['test']['error'][-1]
loss = evals_result['test']['logloss'][-1]
print(error, loss)
update(error, loss)
return loss
def train_final(params):
dfTrain = dataset.one_hot
if 'limit_days' in params.keys() and params['limit_days'] is not None:
maxtime = max(dfTrain['epochseconds'])
dfTrain = dfTrain[dfTrain['epochseconds'] > maxtime - (params['limit_days']*86400)]
dfXTrain = select_features(dfTrain.drop('outcome_victory', axis=1), params)
dfYTrain = dfTrain['outcome_victory']
assert len(dfXTrain) == len(dfYTrain)
print(f"final train with {len(dfTrain)}")
print("final params", params)
dTrain = xgb.DMatrix(dfXTrain, label=dfYTrain)
model = xgb.train(params, dTrain, num_boost_round=params['num_boost_round'])
return model
def inference_final(inp, history, modelandparams):
inf_dataframe = data.inference_dataframe(inp, history, dataset.dataframe)
inf_inp = inf_dataframe.iloc[-1:].drop('outcome_victory', axis=1)
print(inf_inp.T)
for basename, model, params in modelandparams:
dTest = xgb.DMatrix(select_features(inf_inp, params), label=None)
res = model.predict(dTest)[0]
if res >= 0.5:
outcome = 'victory'
else:
outcome = 'defeat'
print(f"{basename}: {outcome} ({res:.3f},{1-res:.3f})")
def getobjective(cv=True, additional_drop=None, select_features=False, no_session_features=False):
print("getting objective with maxdays:", maxdays)
def objective(trial):
columns = get_columns(dataset, additional_drop)
params = {'eta': trial.suggest_float('eta', 0.01, 100, log=True),
'max_depth': trial.suggest_int('max_depth', 1, 20),
'alpha': trial.suggest_float('alpha', 0.001, 100, log=True),
'lambda': trial.suggest_float('lambda', 0.001, 100, log=True),
'gamma': trial.suggest_float('gamma', 0.001, 100, log=True),
'min_child_weight': trial.suggest_float('min_child_weight', 0.001, 100, log=True),
'max_delta_step': trial.suggest_float('max_delta_step', 0, 10, step=0.25),
'subsample': trial.suggest_float('subsample', 0, 1, step=0.05),
'objective': 'binary:logistic',
'eval_metric': ['error', 'logloss'],
'verbosity': 0,
'num_boost_round': trial.suggest_int('num_boost_round', 10, 1000),
'limit_days': trial.suggest_int('limit_days', 14, maxdays),
}
for column in columns:
if select_features:
params[column] = trial.suggest_int(column, 0, 1)
else:
if no_session_features and 'feature' in column:
params[column] = trial.suggest_int(column, 0, 0)
else:
params[column] = trial.suggest_int(column, 1, 1)
if cv:
return testcv(params)
return testtv(params)
return objective
if __name__ == '__main__':
params = {'booster': 'gbtree', 'eval_metric': ['error', 'logloss'], 'eta':0.3, 'max_depth':2, 'lambda':10}
#print(get_columns())
study = optuna.create_study(direction='minimize')
#study.optimize(getobjective(True), n_trials=10000)
study.optimize(getobjective(False), n_trials=1000)