-
Notifications
You must be signed in to change notification settings - Fork 0
/
baselineV3.py
347 lines (290 loc) · 13.4 KB
/
baselineV3.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
#%%
import xgboost as xgb
import sklearn.metrics
import numpy as np
from torch.utils.data import Dataset, DataLoader, random_split, Subset
from tqdm import tqdm
import polars as pl
import os
import json
from transformer import TransformerModel
import random
import torch
from torch.nn.utils.rnn import pad_sequence
import plotly.graph_objects as go
import plotly.express as px
BATCH_SIZE = 1500000
ITERATIONS = 1
model = None
DEVICE = 'cpu' #"torch.device('cuda' if torch.cuda.is_available() else 'cpu')
DATA_FOLDER = 'filtered_data/filtered_data'
GRAPHS_FOLDER = 'training_graphs'
CHECKPOINTS_FOLDER = 'checkpoints'
SEED = 42
if not os.path.exists(GRAPHS_FOLDER):
os.makedirs(GRAPHS_FOLDER)
if not os.path.exists(CHECKPOINTS_FOLDER):
os.makedirs(CHECKPOINTS_FOLDER)
random.seed(SEED)
# class LoLDatasetCache(Dataset):
# def __init__(self, max_len, n_games):
# self.max_len = max_len
# self.n_games = n_games
# self.cached_data = None
# self.cached_targets = None
# self.cached_file_number = -1
# self.cache_size = -1
# def __len__(self):
# return self.n_games
# def __getitem__(self, idx):
# file_number = int(idx // 1000)
# if self.cached_file_number != file_number:
# file_name = f'timeline_{file_number}.parquet'
# df = pl.read_parquet(os.path.join(DATA_FOLDER,file_name))
# grouped = df.group_by(['matchId'])
# games = []
# for _, group in grouped:
# group = group.drop('matchId')
# games.append(torch.from_numpy(group.to_numpy()))
# games = pad_sequence(games, batch_first=True).to(torch.float)
# if games.shape[1] != self.max_len:
# padding = torch.zeros((games.shape[0], self.max_len - games.shape[1], games.shape[2]))
# games = torch.cat((games, padding), 1)
# games[:, :, -1] = games[:, 0, -1].unsqueeze(-1).to(DEVICE)
# X = games[:, :, :-1]
# y = (games[:, :, -1] / 100.0 - 1).unsqueeze(-1)
# self.cached_data = X
# self.cached_targets = y
# self.cached_file_number = file_number
# self.cache_size = games.shape[0]
# return self.cached_data[idx % self.cache_size], self.cached_targets[idx % self.cache_size]
class LoLDatasetCache(Dataset):
def __init__(self, max_len, n_games, k_samples):
self.max_len = max_len
self.n_games = n_games
self.k_samples = k_samples
self.cached_data = None
self.cached_targets = None
self.cached_file_number = -1
self.cache_size = -1
self.cached_timestamps = None
def __len__(self):
return self.n_games
def __getitem__(self, idx):
file_number = int(idx // 1000)
if self.cached_file_number != file_number:
file_name = f'timeline_{file_number}.parquet'
df = pl.read_parquet(os.path.join(DATA_FOLDER,file_name))
grouped = df.group_by(['matchId'])
games = []
timestamps_per_game = []
for _, group in grouped:
group = group.drop('matchId')
timestamps = group['timestamp'].to_numpy()
tensor_group = torch.from_numpy(group.to_numpy())
max_time = timestamps[-1]
timestamps = (timestamps / max_time).astype(np.float32)
# multiply by 100 to get the percentage then int
timestamps = (timestamps * 100).astype(np.int32)
if tensor_group.shape[0] > self.k_samples:
sample_indices = random.sample(range(tensor_group.shape[0]), self.k_samples)
# take first 10 samples
#sample_indices = [0]
tensor_group = tensor_group[sample_indices]
timestamps = timestamps[sample_indices]
if tensor_group.shape[0] < self.k_samples:
continue
timestamps_per_game.append(torch.from_numpy(timestamps))
games.append(tensor_group)
games = torch.stack(games).to(torch.float)
timestamps_per_game = torch.stack(timestamps_per_game).to(DEVICE)
games[:, :, -1] = games[:, 0, -1].unsqueeze(-1).to(DEVICE)
X = games[:, :, :-1]
y = (games[:, :, -1] / 100.0 - 1).unsqueeze(-1)
self.cached_data = X
self.cached_targets = y
self.cached_file_number = file_number
self.cache_size = games.shape[0]
self.cached_timestamps = timestamps_per_game
return self.cached_data[idx % self.cache_size], self.cached_targets[idx % self.cache_size], self.cached_timestamps[idx % self.cache_size]
def index_split(n_games):
indices = np.arange(n_games)
random.shuffle(indices)
split_index = int(n_games // 1.1111111)
return sorted(indices[:split_index]),sorted(indices[split_index:])
with open('data_stats.json', 'r') as file:
data_stats = json.load(file)
dataset = LoLDatasetCache(data_stats['max_len'], data_stats['n_games'],80)
train_indices, test_indices = index_split(data_stats['n_games'])
train_dataset = Subset(dataset, train_indices)
test_dataset = Subset(dataset, test_indices)
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=False)
test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False)
k =0
for i in range(ITERATIONS):
it = 0
for X, y,t in tqdm(train_loader):
k+=1
X = X.numpy()
y = y.numpy()
t = t.numpy()
# flatten 0,1 dimensions
X = X.reshape(X.shape[0] * X.shape[1], X.shape[2])
y = y.reshape(y.shape[0] * y.shape[1])
t = t.reshape(t.shape[0] * t.shape[1])
# find all rows in X where all values are 0
mask = np.all(X == 0, axis=1)
mask_y = (y == -1.0)
mask = mask | mask_y
X = X[~mask]
y = y[~mask]
t = t[~mask]
print("SHAPES: ",X.shape, y.shape)
param = {'objective': 'binary:logistic', # Objective is binary classification
'booster':'dart',
'max_depth': 15, # Maximum depth of a tree. Increasing this value will make the model more complex and more likely to overfit.
'learning_rate': 0.1, # Step size shrinkage used in update to prevents overfitting. After each boosting step, we can directly get the weights of new features.
'subsample': 0.2, # Subsample ratio of the training instances. Setting it to 0.5 means that XGBoost would randomly sample half of the training data prior to growing trees. and this will prevent overfitting.
'colsample_bytree': 0.7, # Subsample ratio of columns when constructing each tree.
'n_estimators': 100, # Number of trees to fit.
'reg_alpha': 0.01, # L1 regularization term on weights. Increasing this value will make model more conservative.
'reg_lambda': 1, # L2 regularization term on weights. Increasing this value will make model more conservative.
'scale_pos_weight': 1, # Control the balance of positive and negative weights, useful for unbalanced classes.
'random_state': 42, # Random number seed. It can be used for producing reproducible results and also for parameter tuning.
'eval_metric': 'logloss', # Evaluation metrics for validation data, a default metric will be assigned according to objective (rmse for regression, and error for classification, mean average precision for ranking)
"device": 'cpu',
'process_type': 'default' if model is None else 'default',
'refresh_leaf': True,
}
# Train the model
model = xgb.train(param, dtrain=xgb.DMatrix(X, label=y), xgb_model=model)
#
# prededict on train set
print('Predicting on train set')
y_pr = model.predict(xgb.DMatrix(X))
accuracies_per_percent = np.zeros(101)
sample_count = np.zeros(101)
for l in range(len(y_pr)):
accuracies_per_percent[t[l]] += (y_pr[l] > 0.5).astype(np.float32) == y[l]
sample_count[t[l]] += 1
sample_count[sample_count == 0] = 1
accuracies_per_percent = accuracies_per_percent / sample_count
#use plotly to plot the accuracies
fig = go.Figure()
fig.add_trace(go.Scatter(x=np.arange(101), y=accuracies_per_percent))
fig.update_layout(title='Accuracy per time percentage TRAIN SET')
#fig.write_image(os.path.join(GRAPHS_FOLDER, f'accuracy_{i}.png'))
fig.show()
print('Train set MSE itr@{}: {}'.format(k, sklearn.metrics.mean_squared_error(y, y_pr)))
print('Train set Accuracy at the end: {}'.format(((y_pr>0.5).astype(np.float32) == y).mean()))
if k%10 == 1:
y_te = []
y_pr = []
t_combined = []
it2 = 0
for X, y,t in test_loader:
it2+=1
if it2 > 1:
break
X = X.numpy()
y = y.numpy()
t = t.numpy()
X = X.reshape(X.shape[0] * X.shape[1], X.shape[2])
y = y.reshape(y.shape[0] * y.shape[1])
t = t.reshape(t.shape[0] * t.shape[1])
mask = np.all(X == 0, axis=1)
X = X[~mask]
y = y[~mask]
t = t[~mask]
t_combined.append(t)
y_te.append(y)
y_pr.append(model.predict(xgb.DMatrix(X)))
t_combined = np.concatenate(t_combined)
y_te = np.concatenate(y_te)
y_pr = np.concatenate(y_pr)
accuracies_per_percent = np.zeros(101)
sample_count = np.zeros(101)
for l in range(len(y_pr)):
accuracies_per_percent[t_combined[l]] += (y_pr[l] > 0.5).astype(np.float32) == y_te[l]
sample_count[t_combined[l]] += 1
sample_count[sample_count == 0] = 1
accuracies_per_percent = accuracies_per_percent / sample_count
#use plotly to plot the accuracies
fig = go.Figure()
fig.add_trace(go.Scatter(x=np.arange(101), y=accuracies_per_percent))
fig.update_layout(title='VAL SET Accuracy per time percentage ')
#fig.write_image(os.path.join(GRAPHS_FOLDER, f'accuracy_{i}.png'))
fig.show()
print('VAL SET MSE itr@{}: {}'.format(k, sklearn.metrics.mean_squared_error(y_te, y_pr)))
print('VAL SET Accuracy itr@{}: {}'.format(k, ((y_pr>0.5).astype(np.float32) == y_te).mean()))
# save model
y_te = []
y_pr = []
t_combined = []
for X, y,t in test_loader:
X = X.numpy()
y = y.numpy()
t = t.numpy()
X = X.reshape(X.shape[0] * X.shape[1], X.shape[2])
y = y.reshape(y.shape[0] * y.shape[1])
t = t.reshape(t.shape[0] * t.shape[1])
mask = np.all(X == 0, axis=1)
X = X[~mask]
y = y[~mask]
t = t[~mask]
t_combined.append(t)
y_te.append(y)
y_pr.append(model.predict(xgb.DMatrix(X)))
t_combined = np.concatenate(t_combined)
y_te = np.concatenate(y_te)
y_pr = np.concatenate(y_pr)
accuracies_per_percent = np.zeros(101)
sample_count = np.zeros(101)
for l in range(len(y_pr)):
accuracies_per_percent[t_combined[l]] += (y_pr[l] > 0.5).astype(np.float32) == y_te[l]
sample_count[t_combined[l]] += 1
sample_count[sample_count == 0] = 1
accuracies_per_percent = accuracies_per_percent / sample_count
#use plotly to plot the accuracies
fig = go.Figure()
fig.add_trace(go.Scatter(x=np.arange(101), y=accuracies_per_percent))
fig.update_layout(title='VAL SET Accuracy per time percentage ')
#fig.write_image(os.path.join(GRAPHS_FOLDER, f'accuracy_{i}.png'))
fig.show()
print('VAL SET MSE itr@{}: {}'.format(k, sklearn.metrics.mean_squared_error(y_te, y_pr)))
print('VAL SET Accuracy itr@{}: {}'.format(k, ((y_pr > 0.5).astype(np.float32) == y_te).mean()))
# save model
# save model
model.save_model(os.path.join(CHECKPOINTS_FOLDER, f'model_final.json'))
# y_pr = model.predict(xgb.DMatrix(x_te))
# print('MSE at the end: {}'.format(sklearn.metrics.mean_squared_error(y_te, y_pr)))
# print('Accuracy at the end: {}'.format(((y_pr>0.5).astype(np.float32) == y_te).mean()))
# show feature importance
xgb.plot_importance(model)
# print number of features
print('Number of features: {}'.format(len(model.get_score(importance_type='weight'))))
#%%
import xgboost as xgb
# load model
model = xgb.Booster()
CHECKPOINTS_FOLDER = 'checkpoints'
model.load_model(os.path.join(CHECKPOINTS_FOLDER, f'model_final.json'))
xgb.plot_importance(model)
# Print number of features
print('Number of features: {}'.format(len(model.get_score(importance_type='weight'))))
# Get feature importance scores
importance_scores = model.get_score(importance_type='weight')
# Sort features by importance
sorted_features = sorted(importance_scores.items(), key=lambda item: item[1], reverse=True)
# Print top 10 features
print("Top 10 most important features:")
for feature, score in sorted_features[:10]:
print(f"{feature}: {score:.2f}")
# create a csv file with the feature importance scores
import csv
with open('feature_importance.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Feature', 'Importance'])
for feature, score in sorted_features:
writer.writerow([feature, score])
#%%