BraydenMoore commited on
Commit
74a75f8
1 Parent(s): 67c7c86

Add dynamic updating record

Browse files
Source/Build/__pycache__/build.cpython-310.pyc ADDED
Binary file (9.16 kB). View file
 
Source/Build/__pycache__/build.cpython-311.pyc CHANGED
Binary files a/Source/Build/__pycache__/build.cpython-311.pyc and b/Source/Build/__pycache__/build.cpython-311.pyc differ
 
Source/Build/__pycache__/build.cpython-39.pyc ADDED
Binary file (9.41 kB). View file
 
Source/Build/build.py CHANGED
@@ -18,10 +18,10 @@ current_season = year if month in [8,9,10,11,12] else year-1
18
  def get_pbp_data(get_seasons=[]):
19
  """
20
  Pull data from nflFastR's Github repo.
21
- If you choose to overwrite, it will replace the existing pbp data with the data you pull.
22
 
23
  """
24
  pbp = nfl.import_pbp_data(get_seasons)
 
25
  pbp['TOP_seconds'] = pbp['drive_time_of_possession'].apply(lambda x: int(x.split(':')[0]) * 60 + int(x.split(':')[1]) if pd.notnull(x) else 0)
26
 
27
  return pbp
@@ -110,10 +110,9 @@ def build_gbg_data(get_seasons=[]):
110
  'PointDiff':'mean'
111
  }
112
 
113
- game = team.groupby('game_id').agg(features).reset_index()
114
  game[['W','L']] = game[['W','L']].expanding().sum()
115
  game[game.columns[4:]] = game[game.columns[4:]].expanding().mean()
116
-
117
  if season != current_season:
118
  game[game.columns[1:]] = game[game.columns[1:]].shift()
119
  game['TEAM'] = team_name
@@ -161,9 +160,9 @@ def add_odds_data():
161
  odds['Home Team'] = odds['Home Team'].str.replace('Washington Redskins','Washington Commanders').str.replace('Washington Football Team','Washington Commanders')
162
  odds['Away Team'] = odds['Away Team'].str.replace('Washington Redskins','Washington Commanders').str.replace('Washington Football Team','Washington Commanders')
163
  odds['Season'] = [i.year if i.month in [8,9,10,11,12] else i.year-1 for i in odds['Date']]
164
- odds['Home Team Abbrev'] = odds['Home Team'].map(team_abbreviation_dict)
165
- odds['Away Team Abbrev'] = odds['Away Team'].map(team_abbreviation_dict)
166
- odds = odds[['Date','Home Score','Away Score','Home Team Abbrev','Away Team Abbrev','Home Odds Close','Away Odds Close','Total Score Close']]
167
  odds['Key'] = odds['Date'].astype(str) + odds['Home Team Abbrev'] + odds['Away Team Abbrev']
168
  odds = odds.drop(columns=['Date','Home Team Abbrev','Away Team Abbrev']).dropna()
169
  odds['Home Odds'] = [round((i-1)*100) if i>= 2 else round(-100/(i-1)) for i in odds['Home Odds Close']]
@@ -183,6 +182,7 @@ def add_odds_data():
183
  i = dataframes[idx]
184
  i['Key'] = i['game_date'].astype(str) + i['home_team'] + i['away_team']
185
  gbg_and_odds = i.merge(odds, left_on='Key', right_on='Key')
 
186
  gbg_and_odds['Home-Team-Win'] = (gbg_and_odds['Home Score']>gbg_and_odds['Away Score']).astype(int)
187
  gbg_and_odds['Over'] = ((gbg_and_odds['Home Score'] + gbg_and_odds['Away Score'])>gbg_and_odds['Total Score Close']).astype(int)
188
 
@@ -191,7 +191,7 @@ def add_odds_data():
191
  else:
192
  file_path = os.path.join(data_directory, 'gbg_and_odds_this_year.csv')
193
 
194
- gbg_and_odds.to_csv(file_path, index=False)
195
 
196
 
197
 
 
18
  def get_pbp_data(get_seasons=[]):
19
  """
20
  Pull data from nflFastR's Github repo.
 
21
 
22
  """
23
  pbp = nfl.import_pbp_data(get_seasons)
24
+ #pbp = pd.read_csv(r"C:\Users\brayd\Downloads\play_by_play_2023.csv")
25
  pbp['TOP_seconds'] = pbp['drive_time_of_possession'].apply(lambda x: int(x.split(':')[0]) * 60 + int(x.split(':')[1]) if pd.notnull(x) else 0)
26
 
27
  return pbp
 
110
  'PointDiff':'mean'
111
  }
112
 
113
+ game = team.groupby('game_id').agg(features).reset_index().sort_values('GP')
114
  game[['W','L']] = game[['W','L']].expanding().sum()
115
  game[game.columns[4:]] = game[game.columns[4:]].expanding().mean()
 
116
  if season != current_season:
117
  game[game.columns[1:]] = game[game.columns[1:]].shift()
118
  game['TEAM'] = team_name
 
160
  odds['Home Team'] = odds['Home Team'].str.replace('Washington Redskins','Washington Commanders').str.replace('Washington Football Team','Washington Commanders')
161
  odds['Away Team'] = odds['Away Team'].str.replace('Washington Redskins','Washington Commanders').str.replace('Washington Football Team','Washington Commanders')
162
  odds['Season'] = [i.year if i.month in [8,9,10,11,12] else i.year-1 for i in odds['Date']]
163
+ odds['Home Team Abbrev'] = odds['Home Team'].map(team_abbreviation_dict).str.replace('LAR','LA')
164
+ odds['Away Team Abbrev'] = odds['Away Team'].map(team_abbreviation_dict).str.replace('LAR','LA')
165
+ odds = odds[['Date','Home Score','Away Score','Home Team Abbrev','Away Team Abbrev','Home Odds Close','Away Odds Close','Total Score Close','Home Line Close']]
166
  odds['Key'] = odds['Date'].astype(str) + odds['Home Team Abbrev'] + odds['Away Team Abbrev']
167
  odds = odds.drop(columns=['Date','Home Team Abbrev','Away Team Abbrev']).dropna()
168
  odds['Home Odds'] = [round((i-1)*100) if i>= 2 else round(-100/(i-1)) for i in odds['Home Odds Close']]
 
182
  i = dataframes[idx]
183
  i['Key'] = i['game_date'].astype(str) + i['home_team'] + i['away_team']
184
  gbg_and_odds = i.merge(odds, left_on='Key', right_on='Key')
185
+ gbg_and_odds['Home-Team-Cover'] = [1 if (h-a)>-l else 0 if (h-a)<-l else 2 for h,a,l in gbg_and_odds[['Home Score','Away Score','Home Line Close']].values]
186
  gbg_and_odds['Home-Team-Win'] = (gbg_and_odds['Home Score']>gbg_and_odds['Away Score']).astype(int)
187
  gbg_and_odds['Over'] = ((gbg_and_odds['Home Score'] + gbg_and_odds['Away Score'])>gbg_and_odds['Total Score Close']).astype(int)
188
 
 
191
  else:
192
  file_path = os.path.join(data_directory, 'gbg_and_odds_this_year.csv')
193
 
194
+ gbg_and_odds.drop_duplicates(subset='game_id').to_csv(file_path, index=False)
195
 
196
 
197
 
Source/Build/update.py CHANGED
@@ -1,4 +1,3 @@
1
- from operator import index
2
  import nfl_data_py.nfl_data_py as nfl
3
  import build
4
  import datetime as dt
@@ -29,10 +28,12 @@ current_season = year if month in [8,9,10,11,12] else year-1
29
 
30
  # update current season
31
  build.build_gbg_data(get_seasons=[current_season])
 
32
  build.add_odds_data()
33
 
34
  # get winners
35
- pbp = build.get_pbp_data([2023])
 
36
  pbp = pbp.drop_duplicates(subset='game_id')
37
  pbp[['season','week','away','home']] = pbp['game_id'].str.split('_', expand=True)
38
  games = pbp[['game_id','away_score','home_score','season','week','away','home']]
@@ -48,3 +49,4 @@ file_path = os.path.join(data_directory, 'results.csv')
48
  games[['game_id','total','winner']].to_csv(file_path, index=False)
49
 
50
 
 
 
 
1
  import nfl_data_py.nfl_data_py as nfl
2
  import build
3
  import datetime as dt
 
28
 
29
  # update current season
30
  build.build_gbg_data(get_seasons=[current_season])
31
+ #build.build_gbg_data(get_seasons=range(2014,2024))
32
  build.add_odds_data()
33
 
34
  # get winners
35
+ pbp = build.get_pbp_data([current_season])
36
+
37
  pbp = pbp.drop_duplicates(subset='game_id')
38
  pbp[['season','week','away','home']] = pbp['game_id'].str.split('_', expand=True)
39
  games = pbp[['game_id','away_score','home_score','season','week','away','home']]
 
49
  games[['game_id','total','winner']].to_csv(file_path, index=False)
50
 
51
 
52
+
Source/Data/gbg.csv CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:87ac3980c207a257b4a0d006502ce2a79ea40e72107b8ab76f9f6114209bc6ff
3
  size 1613287
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4b03612517c6505adabfc11be02fbd51be8ab8c104361f645401c58c128fd2d7
3
  size 1613287
Source/Data/gbg_and_odds.csv CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:eb55eaa75e12c6cffdb65601f2a48b5c09850dd5b42daa0415a2ce9896417eb6
3
- size 1558685
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d6f1a3ea061ed26a13c2398938efe0a60267d07abfde4baaff82db22ec35e79f
3
+ size 1663814
Source/Data/predictions_this_year.pkl DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:926248e52d1fa532c317e37da24ed652ae64110f8219cb5e061668bd3091f048
3
- size 5
 
 
 
 
Source/Data/record.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"winners_correct": "5", "winners_incorrect": "11", "over_unders_correct": "7", "over_unders_incorrect": "9"}
Source/Predict/__pycache__/predict.cpython-310.pyc ADDED
Binary file (5.54 kB). View file
 
Source/Predict/__pycache__/predict.cpython-311.pyc CHANGED
Binary files a/Source/Predict/__pycache__/predict.cpython-311.pyc and b/Source/Predict/__pycache__/predict.cpython-311.pyc differ
 
Source/Predict/__pycache__/predict.cpython-39.pyc ADDED
Binary file (5.65 kB). View file
 
Source/Predict/predict.py CHANGED
@@ -5,6 +5,8 @@ import pickle as pkl
5
  import os
6
  import requests
7
  from bs4 import BeautifulSoup
 
 
8
 
9
  # set dirs for other files
10
  current_directory = os.path.dirname(os.path.abspath(__file__))
@@ -64,27 +66,26 @@ def get_games(week):
64
  def get_one_week(home,away,season,week):
65
  try:
66
  home_df = gbg.loc[((gbg['away_team']==home) | (gbg['home_team']==home)) & (gbg['Season']==season) & (gbg['GP']==week-1)]
67
- print(home_df)
68
  gbg_home_team = home_df['home_team'].item()
69
  home_df.drop(columns=['game_id','home_team','away_team','Season','game_date'], inplace=True)
70
  home_df = home_df[[i for i in home_df.columns if '.Away' not in i] if gbg_home_team==home else [i for i in home_df.columns if '.Away' in i]]
71
  home_df.columns = [i.replace('.Away','') for i in home_df.columns]
72
 
73
  away_df = gbg.loc[((gbg['away_team']==away) | (gbg['home_team']==away)) & (gbg['Season']==season) & (gbg['GP']==week-1)]
74
- print(away_df)
75
  gbg_home_team = away_df['home_team'].item()
76
  away_df.drop(columns=['game_id','home_team','away_team','Season','game_date'], inplace=True)
77
  away_df = away_df[[i for i in away_df.columns if '.Away' not in i] if gbg_home_team==away else [i for i in away_df.columns if '.Away' in i]]
78
  away_df.columns = [i.replace('.Away','') + '.Away' for i in away_df.columns]
79
 
80
  df = home_df.merge(away_df, left_on='GP', right_on='GP.Away')
81
- print(df.columns)
82
  return df
83
  except ValueError:
84
  return pd.DataFrame()
85
 
86
 
87
  def predict(home,away,season,week,total):
 
 
88
  # finish preparing data
89
  if len(home)>4:
90
  home_abbrev = team_name_to_abbreviation[home]
@@ -149,5 +150,4 @@ def predict(home,away,season,week,total):
149
  'Probability': ['N/A'],
150
  'Result': over_under_result}
151
 
152
- print(moneyline)
153
  return game_id, moneyline, over_under
 
5
  import os
6
  import requests
7
  from bs4 import BeautifulSoup
8
+ import warnings
9
+ warnings.filterwarnings("ignore")
10
 
11
  # set dirs for other files
12
  current_directory = os.path.dirname(os.path.abspath(__file__))
 
66
  def get_one_week(home,away,season,week):
67
  try:
68
  home_df = gbg.loc[((gbg['away_team']==home) | (gbg['home_team']==home)) & (gbg['Season']==season) & (gbg['GP']==week-1)]
 
69
  gbg_home_team = home_df['home_team'].item()
70
  home_df.drop(columns=['game_id','home_team','away_team','Season','game_date'], inplace=True)
71
  home_df = home_df[[i for i in home_df.columns if '.Away' not in i] if gbg_home_team==home else [i for i in home_df.columns if '.Away' in i]]
72
  home_df.columns = [i.replace('.Away','') for i in home_df.columns]
73
 
74
  away_df = gbg.loc[((gbg['away_team']==away) | (gbg['home_team']==away)) & (gbg['Season']==season) & (gbg['GP']==week-1)]
 
75
  gbg_home_team = away_df['home_team'].item()
76
  away_df.drop(columns=['game_id','home_team','away_team','Season','game_date'], inplace=True)
77
  away_df = away_df[[i for i in away_df.columns if '.Away' not in i] if gbg_home_team==away else [i for i in away_df.columns if '.Away' in i]]
78
  away_df.columns = [i.replace('.Away','') + '.Away' for i in away_df.columns]
79
 
80
  df = home_df.merge(away_df, left_on='GP', right_on='GP.Away')
 
81
  return df
82
  except ValueError:
83
  return pd.DataFrame()
84
 
85
 
86
  def predict(home,away,season,week,total):
87
+ global results
88
+
89
  # finish preparing data
90
  if len(home)>4:
91
  home_abbrev = team_name_to_abbreviation[home]
 
150
  'Probability': ['N/A'],
151
  'Result': over_under_result}
152
 
 
153
  return game_id, moneyline, over_under
get_record.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime as dt
2
+ import numpy as np
3
+ import pandas as pd
4
+ pd.set_option('chained_assignment',None)
5
+ pd.set_option('display.max_columns',None)
6
+ import os
7
+ import pickle as pkl
8
+ from Source.Predict.predict import predict
9
+
10
+ # get team abbreviations
11
+ with open('Source/Pickles/team_abbreviation_to_name.pkl', 'rb') as f:
12
+ team_abbreviation_to_name = pkl.load(f)
13
+
14
+ # get this year's odds and results
15
+ gbg_and_odds_this_year = pd.read_csv('Source/Data/gbg_and_odds_this_year.csv')
16
+ results = pd.read_csv('Source/Data/results.csv')
17
+
18
+ # make predictions
19
+ from tqdm import tqdm
20
+ print("Predicting games and getting record")
21
+ predictions = {}
22
+ for game_id,home,away,season,week,total in tqdm(gbg_and_odds_this_year[['game_id','home_team','away_team','Season','GP','Total Score Close']].values):
23
+ if week!=1:
24
+ predictions[game_id] = predict(home,away,season,week,total)
25
+
26
+ # get record and save it
27
+ predictions_df = pd.DataFrame(predictions).T
28
+ predictions_df['predicted_winner'] = [i['Winner'][0] if type(i['Winner'])==list else None for i in predictions_df[1]]
29
+ predictions_df['predicted_winner'] = predictions_df['predicted_winner'].map(team_abbreviation_to_name)
30
+ predictions_df['predicted_over_under'] = [i['Over/Under'][0] if type(i['Over/Under'])==list else None for i in predictions_df[2]]
31
+ predictions_df = predictions_df.merge(results, left_index=True, right_on='game_id').merge(gbg_and_odds_this_year[['game_id','Total Score Close']]).dropna(subset=['predicted_winner'])
32
+ predictions_df['over_under'] = ['Over' if t>tsc else 'Under' if t<tsc else 'Push' for t,tsc in predictions_df[['total','Total Score Close']].values]
33
+
34
+ predictions_df['winner_correct'] = (predictions_df['predicted_winner']==predictions_df['winner']).astype(int)
35
+ predictions_df['winner_incorrect'] = (predictions_df['predicted_winner']!=predictions_df['winner']).astype(int)
36
+ predictions_df['over_under_correct'] = (predictions_df['predicted_over_under']==predictions_df['over_under']).astype(int)
37
+ predictions_df['over_under_incorrect'] = (predictions_df['predicted_over_under']!=predictions_df['over_under']).astype(int)
38
+
39
+ winners_correct = predictions_df['winner_correct'].sum()
40
+ winners_incorrect = predictions_df['winner_incorrect'].sum()
41
+ over_unders_correct = predictions_df['over_under_correct'].sum()
42
+ over_unders_incorrect = predictions_df['over_under_incorrect'].sum()
43
+
44
+ record = {"winners_correct":str(winners_correct),
45
+ "winners_incorrect":str(winners_incorrect),
46
+ "over_unders_correct":str(over_unders_correct),
47
+ "over_unders_incorrect":str(over_unders_incorrect)}
48
+
49
+ import json
50
+ with open('Source/Data/record.json', 'w') as f:
51
+ json.dump(record,f)
52
+
main.py CHANGED
@@ -9,6 +9,8 @@ pd.set_option('display.expand_frame_repr', False)
9
 
10
  import os
11
  import json
 
 
12
 
13
  # input the over/under lines here for quicker predictions!
14
  lines = [44.5,
@@ -38,7 +40,7 @@ games = predict.get_games(week)[['Date','Away Team','Home Team']]
38
 
39
  @app.route('/')
40
  def index():
41
- return render_template('index.html')
42
 
43
  @app.route('/get_lines')
44
  def get_lines():
 
9
 
10
  import os
11
  import json
12
+ with open('Source/Data/record.json','r') as f:
13
+ record = json.load(f)
14
 
15
  # input the over/under lines here for quicker predictions!
16
  lines = [44.5,
 
40
 
41
  @app.route('/')
42
  def index():
43
+ return render_template('index.html', **record)
44
 
45
  @app.route('/get_lines')
46
  def get_lines():