-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
369 lines (324 loc) · 9.08 KB
/
utils.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
from config import *
import numpy as np
import pandas as pd
from scipy import stats
def split_test(data, gameweek):
# print(data["GW"].value_counts())
data_gw = data[data["GW"] == gameweek]
data_other_gw = data[~(data["GW"] == gameweek)]
return data_gw, data_other_gw
def check_win(df):
list_win = []
for index in df.index:
result = (df["team_a_score"] - df["team_h_score"]).loc[index]
is_home = df["was_home"].loc[index]
if result == 0:
list_win.append(1)
elif result > 0 and is_home == True:
list_win.append(0)
elif result < 0 and is_home == True:
list_win.append(3)
elif result > 0 and is_home == False:
list_win.append(3)
elif result < 0 and is_home == False:
list_win.append(0)
return list_win
def get_2020_21_season_pos(club):
"""get the position of the club in the 2020-21 season"""
if club == "Man City":
return 1
elif club == "Man Utd":
return 2
elif club == "Liverpool":
return 3
elif club == "Chelsea":
return 4
elif club == "Leicester":
return 5
elif club == "West Ham":
return 6
elif club == "Spurs":
return 7
elif club == "Arsenal":
return 8
elif club == "Leeds":
return 9
elif club == "Everton":
return 10
elif club == "Aston Villa":
return 11
elif club == "Newcastle":
return 12
elif club == "Wolves":
return 13
elif club == "Crystal Palace":
return 14
elif club == "Southampton":
return 15
elif club == "Brighton":
return 16
elif club == "Burnley":
return 17
else:
return 20
def get_2019_20_season_pos(club):
"""get the position of the club in the 2019-20 season"""
if club == "Liverpool":
return 1
elif club == "Man City":
return 2
elif club == "Man Utd":
return 3
elif club == "Chelsea":
return 4
elif club == "Leicester":
return 5
elif club == "Spurs":
return 6
elif club == "Wolves":
return 7
elif club == "Arsenal":
return 8
elif club == "Sheffield Utd":
return 9
elif club == "Burnley":
return 10
elif club == "Southampton":
return 11
elif club == "Everton":
return 12
elif club == "Newcastle":
return 13
elif club == "Crystal Palace":
return 14
elif club == "Brighton":
return 15
elif club == "West Ham":
return 16
elif club == "Aston Villa":
return 17
else:
return 20
def get_2021_22_season_pos(club):
if club == "Man City":
return 1
elif club == "Liverpool":
return 2
elif club == "Chelsea":
return 3
elif club == "Spurs":
return 4
elif club == "Arsenal":
return 5
elif club == "Man Utd":
return 6
elif club == "West Ham":
return 7
elif club == "Leicester":
return 8
elif club == "Brighton":
return 9
elif club == "Wolves":
return 10
elif club == "Newcastle":
return 11
elif club == "Crystal Palace":
return 12
elif club == "Brentford":
return 13
elif club == "Aston Villa":
return 14
elif club == "Southampton":
return 15
elif club == "Everton":
return 16
elif club == "Leeds":
return 17
else:
return 20
def get_2022_23_season_pos(club):
if club == "Man City":
return 1
elif club == "Arsenal":
return 2
elif club == "Man Utd":
return 3
elif club == "Newcastle":
return 4
elif club == "Liverpool":
return 5
elif club == "Brighton":
return 6
elif club == "Aston Villa":
return 7
elif club == "Spurs":
return 8
elif club == "Brentford":
return 9
elif club == "Fulham":
return 10
elif club == "Crystal Palace":
return 11
elif club == "Chelsea":
return 12
elif club == "Wolves":
return 13
elif club == "West Ham":
return 14
elif club == "Bournemouth":
return 15
elif club == "Nott'm Forest":
return 16
elif club == "Everton":
return 17
else:
return 20
def get_last_season_pos(year):
"""get the function to get the last season position of a team at any year"""
if year == "2020-21":
return get_2019_20_season_pos
elif year == "2021-22":
return get_2020_21_season_pos
elif year == "2022-23":
return get_2021_22_season_pos
elif year == "2023-24":
return get_2022_23_season_pos
def remove_neg(val):
if val > 0:
return val
else:
return -val
def deque_and_queue(stats, value):
# if -1 in stats:
# return stats
# deque
stats = stats[1:]
stats.append(value)
return stats
def get_last_stats(data, stat, name, no_last_stats=3):
name_df = data[data["name"] == name]
seasons = ["2020-21", "2021-22", "2022-23"]
name_df_dict = {}
for season in seasons:
name_df_season = name_df[name_df["season"] == season]
list_stats = []
stats_x = []
for value in name_df_season[stat]:
if len(stats_x) < no_last_stats:
list_stats.append(np.array(stats_x))
stats_x.append(value)
else:
list_stats.append(np.array(stats_x))
stats_x = deque_and_queue(stats_x, value)
name_df_season[f"last {no_last_stats} {stat}"] = list_stats
name_df_dict[season] = name_df_season
return pd.concat(name_df_dict.values())
def get_all_players_last_stats(data, stat, no_last_stats=3):
players_df = []
for player in data["name"].unique():
data_player = get_last_stats(data, stat, player, no_last_stats)
players_df.append(data_player)
# print(pd.concat(players_df))
return pd.concat(players_df)
def get_last_stats_test(data, stat, name, no_last_stats=3):
name_df = data[data["name"] == name]
list_stats = []
stats_x = []
for value in name_df[stat]:
if len(stats_x) < no_last_stats:
list_stats.append(np.array(stats_x))
stats_x.append(value)
else:
list_stats.append(np.array(stats_x))
stats_x = deque_and_queue(stats_x, value)
name_df[f"last {no_last_stats} {stat}"] = list_stats
return name_df
def get_players_last_stats_test(data, stat, no_last_stats=short_term_stats):
players_df = []
for player in data["name"].unique():
data_player = get_last_stats_test(data, stat, player, no_last_stats)
players_df.append(data_player)
# print(pd.concat(players_df))
return pd.concat(players_df)
def convert_minutes(val):
"""CONVERTS MINUTES TO A CATEGORICAL OUTPUT"""
if val > 10:
return 1
else:
return 0
def find_mode(vals):
"""find the mode of vals"""
try:
if -1 in vals:
return -1
return stats.mode(vals)[0][0]
except IndexError:
return np.nan
def find_mean(vals):
"""find the mean of vals"""
try:
if -1 in vals:
return -1
return np.mean(vals)
except:
return np.nan
def find_max(vals):
"""find the maximum of vals"""
try:
if -1 in vals:
return -1
return np.max(vals)
except:
return np.nan
def find_std(vals):
"""find the standard deviation of vals"""
try:
if -1 in vals:
return -1
return np.std(vals)
except:
return np.nan
def find_value_count(vals, to_count):
"""find the number of times to_count appears in vals"""
try:
if -1 in vals:
return -1
values, count = np.unique(vals, return_counts=True)
index = np.where(values == to_count)[0][0]
return count[index]
except:
return -2
def get_opp_team(data):
"""get the opponent team for each player"""
opp_teams = []
home_teams = data["home_team"]
away_teams = data["away_team"]
my_teams = data["team"]
for id in data.index:
my_team = my_teams.iloc[id]
home_team = home_teams.iloc[id]
away_team = away_teams.iloc[id]
if my_team == home_team:
opp_teams.append(away_team)
else:
opp_teams.append(home_team)
return opp_teams
def clean_opponent(opponent):
opponent = opponent.replace("Name: home_team, dtype: object", "").replace("\n", "")[
4:
]
while opponent[0] == " ":
opponent = opponent[1:]
return opponent
def create_features(data, mean_features, std_features):
for mean_feature in mean_features:
print(mean_feature)
data[f"mean {mean_feature} {no_last_stats}"] = [
find_mean(values) for values in data[f"last {no_last_stats} {mean_feature}"]
]
for std_feature in std_features:
print(std_feature)
data[f"std {std_feature} {no_last_stats}"] = [
find_std(values) for values in data[f"last {no_last_stats} {std_feature}"]
]
return data