Spaces:
Running
Running
File size: 6,618 Bytes
3231b63 74a75f8 1a89c67 3231b63 1ab47cd 3231b63 7727a49 3231b63 fc2b782 3231b63 e0391c1 1a89c67 3231b63 ee27188 3231b63 e0391c1 3231b63 7727a49 7257aaa fa670d6 7727a49 3231b63 7257aaa fa670d6 7727a49 3231b63 7257aaa 7727a49 3231b63 74a75f8 3231b63 fc2b782 7727a49 3231b63 fc2b782 1a89c67 fc2b782 3231b63 c1a1d81 fc2b782 3231b63 fc2b782 d16c351 ad2e206 fc2b782 3231b63 2b36ba0 fc2b782 3231b63 fc2b782 3231b63 fc2b782 3231b63 7727a49 |
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 |
import xgboost as xgb
import numpy as np
import pandas as pd
import pickle as pkl
import os
import requests
from bs4 import BeautifulSoup
import warnings
warnings.filterwarnings("ignore")
from datetime import datetime
# set dirs for other files
current_directory = os.path.dirname(os.path.abspath(__file__))
parent_directory = os.path.dirname(current_directory)
data_directory = os.path.join(parent_directory, 'Data')
model_directory = os.path.join(parent_directory, 'Models')
pickle_directory = os.path.join(parent_directory, 'Pickles')
file_path = os.path.join(data_directory, 'gbg_this_year.csv')
gbg = pd.read_csv(file_path, low_memory=False)
file_path = os.path.join(data_directory, 'results.csv')
results = pd.read_csv(file_path, low_memory=False)
# get team abbreviations
file_path = os.path.join(pickle_directory, 'team_name_to_abbreviation.pkl')
with open(file_path, 'rb') as f:
team_name_to_abbreviation = pkl.load(f)
file_path = os.path.join(pickle_directory, 'team_abbreviation_to_name.pkl')
with open(file_path, 'rb') as f:
team_abbreviation_to_name = pkl.load(f)
# get schedule
file_path = os.path.join(pickle_directory, 'schedule.pkl')
with open(file_path, 'rb') as f:
schedule = pkl.load(f)
# load models
# moneyline
model = 'xgboost_ML_no_odds_71.4%'
file_path = os.path.join(model_directory, f'{model}.json')
xgb_ml = xgb.Booster()
xgb_ml.load_model(file_path)
# over/under
model = 'xgboost_OU_no_odds_59.8%'
file_path = os.path.join(model_directory, f'{model}.json')
xgb_ou = xgb.Booster()
xgb_ou.load_model(file_path)
def get_week():
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.9',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Dnt': '1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
}
url = 'https://www.nfl.com/schedules/'
resp = requests.get(url,headers=headers)
soup = BeautifulSoup(resp.text, 'html.parser')
h2_tags = soup.find_all('h2')
year = h2_tags[0].getText().split(' ')[0]
week = h2_tags[0].getText().split(' ')[-1]
return int(week), int(year)
def get_games(week):
# pull from NBC
#url = 'https://www.nbcsports.com/nfl/schedule'
#df = pd.read_html(url)[week-1]
df = schedule[week-1]
df['Away Team'] = [' '.join(i.split('\xa0')[1:]) for i in df['Away TeamAway Team']]
df['Home Team'] = [' '.join(i.split('\xa0')[1:]) for i in df['Home TeamHome Team']]
df['Date'] = pd.to_datetime(df['Game TimeGame Time'])
df['Date'] = df['Date'].dt.strftime('%A %d/%m %I:%M %p')
df['Date'] = df['Date'].apply(lambda x: f"{x.split()[0]} {int(x.split()[1].split('/')[1])}/{int(x.split()[1].split('/')[0])} {x.split()[2]}".capitalize())
return df[['Away Team','Home Team','Date']]
def get_one_week(home,away,season,week):
try:
max_GP_home = gbg.loc[((gbg['home_team'] == home) | (gbg['away_team'] == home)) & (gbg['GP'] < week)]['GP'].max()
max_GP_away = gbg.loc[((gbg['home_team'] == away) | (gbg['away_team'] == away)) & (gbg['GP'] < week)]['GP'].max()
home_df = gbg.loc[((gbg['away_team']==home) | (gbg['home_team']==home)) & (gbg['Season']==season) & (gbg['GP']==max_GP_home)]
gbg_home_team = home_df['home_team'].item()
home_df.drop(columns=['game_id','home_team','away_team','Season','game_date'], inplace=True)
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]]
home_df.columns = [i.replace('.Away','') for i in home_df.columns]
away_df = gbg.loc[((gbg['away_team']==away) | (gbg['home_team']==away)) & (gbg['Season']==season) & (gbg['GP']==max_GP_away)]
gbg_home_team = away_df['home_team'].item()
away_df.drop(columns=['game_id','home_team','away_team','Season','game_date'], inplace=True)
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]]
away_df.columns = [i.replace('.Away','') + '.Away' for i in away_df.columns]
df = home_df.reset_index(drop=True).merge(away_df.reset_index(drop=True), left_index=True, right_index=True)
return df
except ValueError:
return pd.DataFrame()
def predict(home,away,season,week,total):
global results
# finish preparing data
if len(home)>4:
home_abbrev = team_name_to_abbreviation[home]
else:
home_abbrev = home
if len(away)>4:
away_abbrev = team_name_to_abbreviation[away]
else:
away_abbrev = away
data = get_one_week(home_abbrev,away_abbrev,season,week)
data['Total Score Close'] = total
matrix = xgb.DMatrix(data.astype(float).values)
# create game id
game_id = str(season) + '_0' + str(int(week)) + '_' + away_abbrev + '_' + home_abbrev
try:
moneyline_result = results.loc[results['game_id']==game_id, 'winner'].item()
except:
moneyline_result = 'N/A'
try:
ml_predicted_proba = xgb_ml.predict(matrix)[0][1]
winner_proba = max([ml_predicted_proba, 1-ml_predicted_proba]).item()
moneyline = {'Winner': [home if ml_predicted_proba>0.5 else away if ml_predicted_proba<0.5 else 'Toss-Up'],
'Probabilities':[winner_proba],
'Result': moneyline_result}
except:
moneyline = {'Winner': 'NA',
'Probabilities':['N/A'],
'Result': moneyline_result}
try:
result = results.loc[results['game_id']==game_id, 'total'].item()
over_under_result = 'Over' if float(result)>float(total) else 'Push' if float(result)==float(total) else 'Under'
print(float(result), float(total))
except:
over_under_result = 'N/A'
try:
ou_predicted_proba = xgb_ou.predict(matrix)[0][1]
ou_proba = max([ou_predicted_proba, 1-ou_predicted_proba]).item()
over_under = {'Over/Under': ['Over' if ou_predicted_proba>0.5 else 'Under'],
'Probability': [ou_proba],
'Result': over_under_result}
except:
over_under = {'Over/Under': 'N/A',
'Probability': ['N/A'],
'Result': over_under_result}
return game_id, moneyline, over_under
|