-
Notifications
You must be signed in to change notification settings - Fork 3
/
KaggleDataReader.py
199 lines (168 loc) · 11.6 KB
/
KaggleDataReader.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
from datetime import datetime
import pandas as pd
import sqlalchemy
import sys
from sqlalchemy import create_engine # database connection
import numpy as np
# TODO: More features: points gained in 5 last home games for home team / away games for away team,
# TODO: points gained in all last games, number of goals scored and conceded, stadium size,
# TODO: points gained in matches against same team (e.g. history of Club - Anderlecht)
def print_full(x):
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
print(x)
pd.reset_option('display.max_rows')
pd.reset_option('display.max_columns')
engine = create_engine("sqlite:///database.sqlite")
leagues = pd.read_sql_query('SELECT * FROM League;', engine)
teams = pd.read_sql_query('SELECT * FROM Team;', engine)
player_stats = pd.read_sql_query('SELECT * FROM Player_Stats;', engine)
players = pd.read_sql_query('SELECT * FROM Player;', engine)
matches = pd.read_sql_query('SELECT * FROM Match;', engine)
belgium_matches = pd.read_sql_query('SELECT * FROM Match WHERE league_id=1;', engine)
england_matches = pd.read_sql_query('SELECT * FROM Match WHERE league_id=1729;', engine).sort_index()
# england_matches = england_matches.sort_values(by='date').head(200)
result_entries = []
feature_entries = []
# player_stats_feature_cols = 'overall_rating', 'potential', 'preferred_foot', 'attacking_work_rate', \
# 'defensive_work_rate', 'crossing', 'finishing', 'heading_accuracy', 'short_passing', \
# 'volleys', 'dribbling', 'curve', 'free_kick_accuracy', 'long_passing', 'ball_control', \
# 'acceleration', 'sprint_speed', 'agility', 'reactions', 'balance', 'shot_power', 'jumping', \
# 'stamina', 'strength', 'long_shots', 'aggression', 'interceptions', 'positioning', 'vision', \
# 'penalties', 'marking', 'standing_tackle', 'sliding_tackle', 'gk_diving', 'gk_handling', \
# 'gk_kicking', 'gk_positioning', 'gk_reflexes'
player_stats_feature_cols = 'overall_rating', 'potential'
# Creating a result dataframe
for i in range(len(england_matches)):
record = england_matches.iloc[i, :]
print('RESULTS', str(i) + '/' + str(len(england_matches) - 1))
home_goals = int(record['home_team_goal'])
away_goals = int(record['away_team_goal'])
if home_goals > away_goals:
result = 'HOME'
elif away_goals > home_goals:
result = 'AWAY'
else:
result = 'DRAW'
home_team = teams[teams.team_api_id == record['home_team_api_id']]['team_long_name'].values[0]
away_team = teams[teams.team_api_id == record['away_team_api_id']]['team_long_name'].values[0]
result_entry = {'match_id': record['match_api_id'], 'date': datetime.strptime(record['date'], '%Y-%m-%d %H:%M:%S'),
'home_team_goal': record['home_team_goal'], 'away_team_goal': record['away_team_goal'],
'B365H': record['B365H'], 'B365D': record['B365D'], 'B365A': record['B365A'], 'result': result,
'home_team': home_team, 'away_team': away_team}
result_entries.append(result_entry)
result_df = pd.DataFrame(result_entries)
def get_historical_data(_home_team, _away_team, date, _result_df, n_games=5):
# Get goals scored/conceded in n_games latest (home/away/all) games
# Get points obtained in n_games latest (home/away/all) games
# Get the points obtained by the home and away team in the n_games latest mutual games
home_home_games = _result_df[(_result_df.home_team == _home_team) & (_result_df.date < date)] \
.sort_values(by='date').tail(n_games) # Sort_values is default ascending
home_home_goals_scored = sum(home_home_games['home_team_goal'])
home_home_goals_conceded = sum(home_home_games['away_team_goal'])
home_home_points = sum(home_home_games['result'] == 'HOME') * 3 + sum(home_home_games['result'] == 'DRAW')
home_away_games = _result_df[(_result_df.away_team == _home_team) & (_result_df.date < date)] \
.sort_values(by='date').tail(n_games)
home_all_goals_scored = sum(home_away_games['away_team_goal']) + home_home_goals_scored
home_all_goals_conceded = sum(home_away_games['home_team_goal']) + home_home_goals_conceded
home_all_points = sum(home_away_games['result'] == 'AWAY') * 3 + sum(home_away_games['result'] == 'DRAW') + \
home_home_points
away_away_games = _result_df[(_result_df.away_team == _away_team) & (_result_df.date < date)] \
.sort_values(by='date').tail(n_games)
away_away_goals_scored = sum(away_away_games['away_team_goal'])
away_away_goals_conceded = sum(away_away_games['home_team_goal'])
away_away_points = sum(away_away_games['result'] == 'AWAY') * 3 + sum(away_away_games['result'] == 'DRAW')
away_home_games = _result_df[(_result_df.home_team == _away_team) & (_result_df.date < date)] \
.sort_values(by='date').tail(n_games)
away_all_goals_scored = sum(away_home_games['home_team_goal']) + away_away_goals_scored
away_all_goals_conceded = sum(away_home_games['away_team_goal']) + away_away_goals_conceded
away_all_points = sum(away_home_games['result'] == 'HOME') * 3 + sum(away_home_games['result'] == 'DRAW') + \
away_away_points
same_mutual_games = _result_df[(_result_df.home_team == _home_team) & (_result_df.away_team == _away_team) &
(_result_df.date < date)].sort_values(by='date').tail(n_games)
home_same_mutual_points = sum(same_mutual_games['result'] == 'HOME') * 3 + sum(same_mutual_games['result'] == 'DRAW')
home_same_mutual_goals_scored = sum(same_mutual_games['home_team_goal'])
home_same_mutual_goals_conceded = sum(same_mutual_games['away_team_goal'])
away_same_mutual_points = sum(same_mutual_games['result'] == 'AWAY') * 3 + sum(same_mutual_games['result'] == 'DRAW')
different_mutual_games = _result_df[(_result_df.home_team == _away_team) & (_result_df.away_team == _home_team) &
(_result_df.date < date)].sort_values(by='date').tail(n_games)
home_mutual_points = sum(different_mutual_games['result'] == 'AWAY') * 3 + sum(different_mutual_games['result'] == 'DRAW') \
+ home_same_mutual_points
home_mutual_goals_scored = sum(different_mutual_games['away_team_goal']) + home_same_mutual_goals_scored
home_mutual_goals_conceded = sum(different_mutual_games['home_team_goal']) + home_same_mutual_goals_conceded
away_mutual_points = sum(different_mutual_games['result'] == 'HOME') * 3 + sum(different_mutual_games['result'] == 'DRAW') \
+ away_same_mutual_points
away_mutual_goals_scored = home_mutual_goals_conceded
away_mutual_goals_conceded = home_mutual_goals_scored
return home_home_goals_scored, home_home_goals_conceded, home_all_goals_scored, home_all_goals_conceded, \
home_home_points, home_all_points, away_away_goals_scored, away_away_goals_conceded, \
away_all_goals_scored, away_all_goals_conceded, away_away_points, away_all_points, home_mutual_points, \
home_mutual_goals_scored, home_mutual_goals_conceded, away_mutual_points, away_mutual_goals_scored, \
away_mutual_goals_conceded
# Creating the features dataframe
for i in range(len(england_matches)):
record = england_matches.iloc[i, :]
print('FEATURES', str(i) + '/' + str(len(england_matches) - 1))
feature_entry = {}
feature_entry['match_id'] = record['match_api_id']
feature_entry['date'] = datetime.strptime(record['date'], '%Y-%m-%d %H:%M:%S')
feature_entry['home_team'] = teams[teams.team_api_id == record['home_team_api_id']]['team_long_name'].values[0]
feature_entry['away_team'] = teams[teams.team_api_id == record['away_team_api_id']]['team_long_name'].values[0]
feature_entry['B365H'] = record['B365H']
feature_entry['B365D'] = record['B365D']
feature_entry['B365A'] = record['B365A']
# Get some historical data
home_home_goals_scored, home_home_goals_conceded, home_all_goals_scored, home_all_goals_conceded, \
home_home_points, home_all_points, away_away_goals_scored, away_away_goals_conceded, \
away_all_goals_scored, away_all_goals_conceded, away_away_points, away_all_points, home_mutual_points, \
home_mutual_goals_scored, home_mutual_goals_conceded, away_mutual_points, away_mutual_goals_scored, \
away_mutual_goals_conceded = get_historical_data(feature_entry['home_team'], feature_entry['away_team'],
feature_entry['date'], result_df.copy())
feature_entry['home_home_goals_scored'] = home_home_goals_scored
feature_entry['home_home_goals_conceded'] = home_home_goals_conceded
feature_entry['home_all_goals_scored'] = home_all_goals_scored
feature_entry['home_all_goals_conceded'] = home_all_goals_conceded
feature_entry['home_home_points'] = home_home_points
feature_entry['home_all_points'] = home_all_points
feature_entry['away_away_goals_scored'] = away_away_goals_scored
feature_entry['away_away_goals_conceded'] = away_away_goals_conceded
feature_entry['away_all_goals_scored'] = away_all_goals_scored
feature_entry['away_all_goals_conceded'] = away_all_goals_conceded
feature_entry['away_away_points'] = away_away_points
feature_entry['away_all_points'] = away_all_points
feature_entry['home_mutual_points'] = home_mutual_points
feature_entry['home_mutual_goals_scored'] = home_mutual_goals_scored
feature_entry['home_mutual_goals_conceded'] = home_mutual_goals_conceded
feature_entry['away_mutual_points'] = away_mutual_points
feature_entry['away_mutual_goals_scored'] = away_mutual_goals_scored
feature_entry['away_mutual_goals_conceded'] = away_mutual_goals_conceded
# Get some player stats
for k in range(1, 12):
feature_entry['home_player_X' + str(k)] = record['home_player_X' + str(k)]
feature_entry['home_player_Y' + str(k)] = record['home_player_Y' + str(k)]
feature_entry['away_player_X' + str(k)] = record['away_player_X' + str(k)]
feature_entry['away_player_Y' + str(k)] = record['away_player_Y' + str(k)]
player_all_stats_home = player_stats[player_stats.player_api_id == record['home_player_' + str(k)]]
min = sys.maxsize
closest_stat_record = None
for j in range(len(player_all_stats_home)):
stat_record = player_all_stats_home.iloc[j, :]
if abs((datetime.strptime(record['date'], '%Y-%m-%d %H:%M:%S') -
datetime.strptime(stat_record['date_stat'], '%Y-%m-%d %H:%M:%S')).total_seconds()) < min:
closest_stat_record = stat_record
for stat_feature_col in player_stats_feature_cols:
feature_entry[stat_feature_col + '_home_' + str(k)] = stat_record[stat_feature_col]
player_all_stats_away = player_stats[player_stats.player_api_id == record['away_player_' + str(k)]]
min = sys.maxsize
closest_stat_record = None
for j in range(len(player_all_stats_away)):
stat_record = player_all_stats_away.iloc[j, :]
if abs((datetime.strptime(record['date'], '%Y-%m-%d %H:%M:%S') -
datetime.strptime(stat_record['date_stat'], '%Y-%m-%d %H:%M:%S')).total_seconds()) < min:
closest_stat_record = stat_record
for stat_feature_col in player_stats_feature_cols:
feature_entry[stat_feature_col + '_away_' + str(k)] = stat_record[stat_feature_col]
feature_entries.append(feature_entry)
feature_df = pd.DataFrame(feature_entries)
feature_df.to_csv('kaggle_features_v3.csv')
result_df.to_csv('kaggle_labels_v3.csv')