-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_data.py
295 lines (242 loc) · 12.8 KB
/
process_data.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
import sqlite3
import pandas as pd
import numpy as np
from xml_events_parsing import get_foulcommit_events, get_goal_events, get_shoton_events, get_shotoff_events, get_cards
def getData():
database = 'database.sqlite'
conn = sqlite3.connect(database)
player_data = pd.read_sql("SELECT * FROM Player;", conn)
player_stats_data = pd.read_sql("SELECT * FROM Player_Attributes;", conn)
team_data = pd.read_sql("SELECT * FROM Team;", conn)
match_data = pd.read_sql("SELECT * FROM Match where goal is not null ;", conn)
match_data['date'] = pd.to_datetime(match_data['date'], format='%Y-%m-%d 00:00:00')
return player_data, player_stats_data, team_data, match_data
def add_cards(match_data):
# Initialize empty lists for each new column
home_yellow_before_half = []
home_yellow_after_half = []
away_yellow_before_half = []
away_yellow_after_half = []
total_yellow_before_half = []
total_yellow_after_half = []
home_red_before_half = []
home_red_after_half = []
away_red_before_half = []
away_red_after_half = []
total_red_before_half = []
total_red_after_half = []
home_card_score = []
away_card_score = []
card_score = []
# Iterate over each row in the DataFrame
for index, row in match_data.iterrows():
# Extract the necessary values from the row
xml_card_data = row['card']
home_team_id = row['home_team_api_id']
away_team_id = row['away_team_api_id']
# Get the card data
card_data = get_cards(xml_card_data, home_team_id, away_team_id)
# Add the card data to the respective lists
home_yellow_before_half.append(card_data[0])
home_yellow_after_half.append(card_data[1])
away_yellow_before_half.append(card_data[2])
away_yellow_after_half.append(card_data[3])
total_yellow_before_half.append(card_data[4])
total_yellow_after_half.append(card_data[5])
home_red_before_half.append(card_data[6])
home_red_after_half.append(card_data[7])
away_red_before_half.append(card_data[8])
away_red_after_half.append(card_data[9])
total_red_before_half.append(card_data[10])
total_red_after_half.append(card_data[11])
home_card_score.append(card_data[12])
away_card_score.append(card_data[13])
card_score.append(card_data[14])
# Add the new columns to the DataFrame
match_data['home_yellow_before_half'] = home_yellow_before_half
match_data['home_yellow_after_half'] = home_yellow_after_half
match_data['away_yellow_before_half'] = away_yellow_before_half
match_data['away_yellow_after_half'] = away_yellow_after_half
match_data['total_yellow_before_half'] = total_yellow_before_half
match_data['total_yellow_after_half'] = total_yellow_after_half
match_data['home_red_before_half'] = home_red_before_half
match_data['home_red_after_half'] = home_red_after_half
match_data['away_red_before_half'] = away_red_before_half
match_data['away_red_after_half'] = away_red_after_half
match_data['total_red_before_half'] = total_red_before_half
match_data['total_red_after_half'] = total_red_after_half
match_data['home_card_score'] = home_card_score
match_data['away_card_score'] = away_card_score
match_data['card_score'] = card_score
return match_data
def add_fouls(match_data):
# Iterate over each row in the DataFrame
for index, row in match_data.iterrows():
# Initialize empty lists for each new column
home_fouls_before_half = []
home_fouls_after_half = []
away_fouls_before_half = []
away_fouls_after_half = []
total_fouls_before_half = []
total_fouls_after_half = []
# Extract the necessary values from the row
xml_foulcommit_data = row['foulcommit']
home_team_id = row['home_team_api_id']
away_team_id = row['away_team_api_id']
# Get the fouls data
fouls_data = get_foulcommit_events(xml_foulcommit_data, home_team_id, away_team_id)
# Add the fouls data to the respective lists
home_fouls_before_half.append(fouls_data[0])
home_fouls_after_half.append(fouls_data[1])
away_fouls_before_half.append(fouls_data[2])
away_fouls_after_half.append(fouls_data[3])
total_fouls_before_half.append(fouls_data[4])
total_fouls_after_half.append(fouls_data[5])
# Add the new columns to the DataFrame
match_data['home_fouls_before_half'] = home_fouls_before_half
match_data['home_fouls_after_half'] = home_fouls_after_half
match_data['away_fouls_before_half'] = away_fouls_before_half
match_data['away_fouls_after_half'] = away_fouls_after_half
match_data['total_fouls_before_half'] = total_fouls_before_half
match_data['total_fouls_after_half'] = total_fouls_after_half
return match_data
def add_shotoff(match_data):
# Initialize empty lists for each new column
home_shotoff_before_half = []
home_shotoff_after_half = []
away_shotoff_before_half = []
away_shotoff_after_half = []
total_shotoff_before_half = []
total_shotoff_after_half = []
# Iterate over each row in the DataFrame
for index, row in match_data.iterrows():
# Extract the necessary values from the row
xml_foulcommit_data = row['shotoff']
home_team_id = row['home_team_api_id']
away_team_id = row['away_team_api_id']
# Get the shotoff data
shotoff_data = get_shotoff_events(xml_foulcommit_data, home_team_id, away_team_id)
# Add the shotoff data to the respective lists
home_shotoff_before_half.append(shotoff_data[0])
home_shotoff_after_half.append(shotoff_data[1])
away_shotoff_before_half.append(shotoff_data[2])
away_shotoff_after_half.append(shotoff_data[3])
total_shotoff_before_half.append(shotoff_data[4])
total_shotoff_after_half.append(shotoff_data[5])
# Add the new columns to the DataFrame
match_data['home_shotoff_before_half'] = home_shotoff_before_half
match_data['home_shotoff_after_half'] = home_shotoff_after_half
match_data['away_shotoff_before_half'] = away_shotoff_before_half
match_data['away_shotoff_after_half'] = away_shotoff_after_half
match_data['total_shotoff_before_half'] = total_shotoff_before_half
match_data['total_shotoff_after_half'] = total_shotoff_after_half
return match_data
def add_overall_rating(match_data, player_stats_data):
home_players = ["home_player_" + str(x) for x in range(1, 12)]
away_players = ["away_player_" + str(x) for x in range(1, 12)]
for player in home_players:
match_data = pd.merge(match_data, player_stats_data[["id", "overall_rating"]], left_on=[player],
right_on=["id"],
suffixes=["", "_" + player])
for player in away_players:
match_data = pd.merge(match_data, player_stats_data[["id", "overall_rating"]], left_on=[player],
right_on=["id"],
suffixes=["", "_" + player])
match_data = match_data.rename(columns={"overall_rating": "overall_rating_home_player_1"})
match_data = match_data[match_data[['overall_rating_' + p for p in home_players]].isnull().sum(axis=1) <= 0]
match_data = match_data[match_data[['overall_rating_' + p for p in away_players]].isnull().sum(axis=1) <= 0]
match_data['overall_rating_home'] = match_data[['overall_rating_' + p for p in home_players]].sum(axis=1)
match_data['overall_rating_away'] = match_data[['overall_rating_' + p for p in away_players]].sum(axis=1)
match_data['overall_rating_difference'] = match_data['overall_rating_home'] - match_data['overall_rating_away']
match_data['min_overall_rating_home'] = match_data[['overall_rating_' + p for p in home_players]].min(axis=1)
match_data['min_overall_rating_away'] = match_data[['overall_rating_' + p for p in away_players]].min(axis=1)
match_data['max_overall_rating_home'] = match_data[['overall_rating_' + p for p in home_players]].max(axis=1)
match_data['max_overall_rating_away'] = match_data[['overall_rating_' + p for p in away_players]].max(axis=1)
match_data['mean_overall_rating_home'] = match_data[['overall_rating_' + p for p in home_players]].mean(axis=1)
match_data['mean_overall_rating_away'] = match_data[['overall_rating_' + p for p in away_players]].mean(axis=1)
match_data['std_overall_rating_home'] = match_data[['overall_rating_' + p for p in home_players]].std(axis=1)
match_data['std_overall_rating_away'] = match_data[['overall_rating_' + p for p in away_players]].std(axis=1)
return match_data
def add_shoton(match_data):
# Iterate over each row in the DataFrame
# Initialize the lists for this row
home_shoton_before_half = []
home_shoton_after_half = []
away_shoton_before_half = []
away_shoton_after_half = []
total_shoton_before_half = []
total_shoton_after_half = []
for index, row in match_data.iterrows():
# Extract the necessary values from the row
xml_shoton_data = row['shoton']
home_team_id = row['home_team_api_id']
away_team_id = row['away_team_api_id']
# Get the shoton on goal data
shoton_data = get_shoton_events(xml_shoton_data, home_team_id, away_team_id)
# Add the shoton on goal data to the respective lists
home_shoton_before_half.append(shoton_data[0])
home_shoton_after_half.append(shoton_data[1])
away_shoton_before_half.append(shoton_data[2])
away_shoton_after_half.append(shoton_data[3])
total_shoton_before_half.append(shoton_data[4])
total_shoton_after_half.append(shoton_data[5])
# Add the new columns to the DataFrame
match_data['home_shoton_before_half'] = home_shoton_before_half
match_data['home_shoton_after_half'] = home_shoton_after_half
match_data['away_shoton_before_half'] = away_shoton_before_half
match_data['away_shoton_after_half'] = away_shoton_after_half
match_data['total_shoton_before_half'] = total_shoton_before_half
match_data['total_shoton_after_half'] = total_shoton_after_half
return match_data
def add_goals(match_data):
# Iterate over each row in the DataFrame
# Initialize empty lists for each new column
home_goals_before_half = []
home_goals_after_half = []
away_goals_before_half = []
away_goals_after_half = []
total_goals_before_half = []
total_goals_after_half = []
for index, row in match_data.iterrows():
# Extract the necessary values from the row
xml_goal_data = row['goal']
home_team_id = row['home_team_api_id']
away_team_id = row['away_team_api_id']
if xml_goal_data is not None:
goals_data = get_goal_events(xml_goal_data, home_team_id, away_team_id)
# Add the goals data to the respective lists
home_goals_before_half.append(goals_data[0])
home_goals_after_half.append(goals_data[1])
away_goals_before_half.append(goals_data[2])
away_goals_after_half.append(goals_data[3])
total_goals_before_half.append(goals_data[4])
total_goals_after_half.append(goals_data[5])
else:
home_goals_before_half.append(None)
home_goals_after_half.append(None)
away_goals_before_half.append(None)
away_goals_after_half.append(None)
total_goals_before_half.append(None)
total_goals_after_half.append(None)
# Add the new columns to the DataFrame
match_data['home_goals_before_half'] = home_goals_before_half
match_data['home_goals_after_half'] = home_goals_after_half
match_data['away_goals_before_half'] = away_goals_before_half
match_data['away_goals_after_half'] = away_goals_after_half
match_data['total_goals_before_half'] = total_goals_before_half
match_data['total_goals_after_half'] = total_goals_after_half
return match_data
def add_shots(match_data):
match_data = add_shoton(match_data)
match_data = add_shotoff(match_data)
return match_data
def add_outcome(match_data):
match_data['goal_difference'] = match_data['home_team_goal'] - match_data['away_team_goal']
match_data['home_status'] = 'D'
match_data['home_status'] = np.where(match_data['goal_difference'] > 0, 'W', match_data['home_status'])
match_data['home_status'] = np.where(match_data['goal_difference'] < 0, 'L', match_data['home_status'])
# print percentage of home wins and losses and draws
print(f"Percentage of home wins: {len(match_data[match_data['home_status'] == 'W']) / len(match_data) * 100}")
print(f"Percentage of home losses: {len(match_data[match_data['home_status'] == 'L']) / len(match_data) * 100}")
print(f"Percentage of home draws: {len(match_data[match_data['home_status'] == 'D']) / len(match_data) * 100}")
return match_data