def __init__(self): conn = establish_db_connection('sqlalchemy').connect() self.url = "https://kenpom.com/team.php" self.url_team = lambda x, y: '%s?team=%s&y=%s' % (self.url, str(x), str(y)) self.df = pd.DataFrame() teams = pd.read_sql( 'SELECT DISTINCT Team FROM kenpom_season_data WHERE Year = 2019 AND Rank <= 60', con=conn) teams = teams['Team'].tolist() skip_teams = pd.read_sql( 'SELECT DISTINCT Team FROM kenpom_madness_table', con=conn) skip_teams = skip_teams['Team'].tolist() for team in skip_teams: teams.remove(team) for team in teams: self._start_session(team) print "Fetching game results for %s" % team self.team = team self._fetch_team_results() print "Uploading to db..." self._upload_data() print "" print self.df self.driver.close()
def four_factors_algo(team_a, team_b): conn = establish_db_connection('sqlalchemy').connect() team_a = str(team_a) team_b = str(team_b) ## fetch data cols = { 'Team': 'team', 'eFG Perc': 'efg_perc', 'Turnover Perc': 'tov_perc', 'Off Rebound Perc': 'orb_perc', 'FTRate': 'ft_rate', 'Opp eFG Perc': 'opp_efg_perc', 'Opp Turnover Perc': 'opp_tov_perc', 'Opp Off Rebound Perc': 'opp_orb_perc', 'Opp FTRate': 'opp_ft_rate' } sql_str = 'SELECT `{}` FROM kenpom_four_factors_data WHERE Year = 2019'.format( '`, `'.join(cols.keys())) df = pd.read_sql(sql_str, con=conn) df.rename(index=str, columns=cols, inplace=True) ## clean data for col in [col for col in df.columns if col != 'team']: df[col] = df[col] / 100 df = df[df['team'].isin([team_a, team_b])].copy() ## run algo weights_dict = {'efg': 0.40, 'tov': 0.25, 'orb': 0.20, 'ft_rate': 0.15} temp = {} for index, row in df.iterrows(): efg = (row['efg_perc'] - row['opp_efg_perc']) * 100 tov = (row['tov_perc'] - row['opp_tov_perc']) * 100 orb = (row['tov_perc'] - row['opp_tov_perc']) * 100 ft_rate = (row['ft_rate'] - row['opp_ft_rate']) * 100 temp[row['team']] = { 'efg': efg, 'tov': tov, 'orb': orb, 'ft_rate': ft_rate } algo_dict = {} print team_a for s, w in weights_dict.iteritems(): algo_dict[s] = (temp[team_a].get(s) - temp[team_b].get(s)) * w spread = round((sum(algo_dict.values()) * 2), 3) print spread return spread
def __init__(self, start, end): self.conn = establish_db_connection('sqlalchemy').connect() self.dates = range_all_dates(start, end) self.clean_cols = [ 'game_date', 'game_id', 'away_id', 'away_team', 'home_id', 'home_team', 'pts_away', 'pts_home', 'pt_diff_away', 'pts_total', 'win_side', 'win_id' ] self.clean_scores = pd.DataFrame(columns=self.clean_cols) self._fetch_raw_scores()
def __init__(self): self.conn = establish_db_connection('sqlalchemy').connect() self.games = ['', '', '', '', '', ''] self.stats = [ 'EFG_PCT', 'FTA_RATE', 'TM_TOV_PCT', 'OREB_PCT', 'OPP_EFG_PCT', 'OPP_FTA_RATE', 'OPP_TOV_PCT', 'OPP_OREB_PCT' ] self._fetch_stats() self._weight_player_stats() print self._team[self.stats[:] + ['TEAM_ID']] print self._weighted
def __init__(self): self.conn = establish_db_connection('sqlalchemy').connect() self.today = datetime.now().strftime('%Y-%m-%d') self.prev_day = (datetime.strptime(self.today, '%Y-%m-%d').date() - timedelta(days=1)).strftime('%Y-%m-%d') self.games_list = get_games_list(self.prev_day) self._fetch_game_stats() self._fetch_final_scores() self._fetch_spreads() # self._calculate_results() self._update_db()
def linear_algo(team_a, team_b): conn = establish_db_connection('sqlalchemy').connect() ## fetch data cols = { 'Team': 'team', 'eFG Perc': 'efg_perc', 'Turnover Perc': 'tov_perc', 'Off Rebound Perc': 'orb_perc', 'FTRate': 'ft_rate', 'Opp eFG Perc': 'opp_efg_perc', 'Opp Turnover Perc': 'opp_tov_perc', 'Opp Off Rebound Perc': 'opp_orb_perc', 'Opp FTRate': 'opp_ft_rate' } sql_str = 'SELECT `{}` FROM kenpom_four_factors_data WHERE Year = 2019'.format( '`, `'.join(cols.keys())) df = pd.read_sql(sql_str, con=conn) df.rename(index=str, columns=cols, inplace=True) ## adjust percentages for algorithm for col in [ x for x in df.columns if x not in ['team', 'opp_ft_rate', 'ft_rate'] ]: df[col] = df[col] / 100 ## run algo weights_dict = { 'efg_perc': 101.54, 'tov_perc': -0.829, 'orb_perc': 0.063, 'ft_rate': 7.57, 'opp_efg_perc': -92.41, 'opp_tov_perc': 1.0428, 'opp_orb_perc': -0.0123, 'opp_ft_rate': -13.095 } spreads = {} for team in [team_a, team_b]: data = df[df['team'] == team].copy() spread = 0 for s, w in weights_dict.iteritems(): spread += data[s].max() * w spreads[team] = spread spread = spreads[team_a] - spreads[team_b] print spread return None
def __init__(self, gamedate=None): self.conn = establish_db_connection('sqlalchemy').connect() if gamedate is not None: self.gamedate = gamedate else: self.gamedate = datetime.now().date().strftime('%Y-%m-%d') self.games_df = assets.games_daily(self.gamedate) self._fetch_agg_stats() self._determine_b2b() self._build_predictions() self._merge_with_spreads() self._clean_merged_data() self.merged_df.to_sql('daily_picks', con=self.conn, if_exists='replace', index=False) self.merged_df.to_sql('historical_picks', con=self.conn, if_exists='append', index=False)
def __init__(self, stats): self.conn = establish_db_connection('sqlalchemy').connect() self.stats = stats
def _upload_data(self): conn = establish_db_connection('sqlalchemy').connect() self.df.to_sql('kenpom_madness_table', con=conn, if_exists='append', index=None)
def __init__(self): self.conn = establish_db_connection('sqlalchemy').connect()
import dash import dash_core_components as dcc import dash_html_components as html import plotly.graph_objs as go from dash.dependencies import Input, Output, State, Event import dash_table import styling import pandas as pd import madness_helper as helper from nba_utilities.db_connection_manager import establish_db_connection app = dash.Dash() app.title = "Madness" app.css.append_css({'external_url':"https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"}) conn = establish_db_connection('sqlalchemy').connect() teams = pd.read_sql('SELECT DISTINCT team FROM kenpom_four_factors_data', con=conn) teams = [ row['team'] for index, row in teams.iterrows() ] teams = [ {'label': team, 'value': team} for team in sorted(teams) ] def serve_layout(): return html.Div(children = [ html.Div(className="app-header", children = [ html.H1('Alge-bracket', className="app-header--title", style=styling.portal_header_txt)], style = styling.portal_header_bgrd), dcc.Tabs(id="tabs", children=[ #### Head 2 Head Predictor Tab #### dcc.Tab(label='H2H Predictor', children=[ html.H1(children='Matchup Predictor', style=styling.tab_header), html.Div(style={'padding':10}), html.Div(className='row', children=[