# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# %matplotlib inline
import math

# +
sixteen_df = pd.read_csv('./uscho_mens_games_2016.csv',header=None)
sixteen_df['season'] = pd.Series(2016, index=sixteen_df.index)

seventeen_df = pd.read_csv('./uscho_mens_games_2017.csv',header=None)
seventeen_df['season'] = pd.Series(2017, index=seventeen_df.index)

eighteen_df = pd.read_csv('./uscho_mens_games_2018.csv',header=None)
eighteen_df['season'] = pd.Series(2018, index=eighteen_df.index)
#drop games not yet played
eighteen_df = eighteen_df.dropna(subset=[9])

# put all the seasons together
df = pd.concat([sixteen_df,seventeen_df,eighteen_df],ignore_index=True)
# -

df.head()

df = df[[8,9,14,15,'season']]

df.head()

df = df.rename(columns={8: 'team', 9: 'teamscore', 14: 'opponent', 15: 'oppscore'})

df.head()

df['weights'] = (df['season']-2012)
df['weights'] = df['weights'].apply(lambda x: math.factorial(x))

df.head()

df['location'] = 'V'

df.head()

import re

df.head()

# +
df['team'] = df['team'].apply(lambda x: re.sub("[\(\[].*?[\)\]]", "", x))
df['team'] = df['team'].apply(lambda x: x.lstrip())

df['opponent'] = df['opponent'].apply(lambda x: re.sub("[\(\[].*?[\)\]]", "", x))
df['opponent'] = df['opponent'].apply(lambda x: x.lstrip())
# -

df.head()

df['scorediff'] = (df['teamscore']-df['oppscore'])

df['location'] = df['location'].replace('V',-1)
df['location'] = df['location'].replace('N',0)
df['location'] = df['location'].replace('H',1)

df.head()

df1 = df.copy()
df1.head()

df1 = df1.rename(columns={'team': 'opponent', 'teamscore': 'oppscore', 'opponent': 'team', 'oppscore':'teamscore'})

df1.head()

df1['scorediff'] = df1['scorediff'].apply(lambda x: x*-1)
df1['location'] = df1['location'].apply(lambda x: x*-1)

df1.head()

df.shape

df1.shape

df = pd.concat([df,df1],ignore_index=True)

df.head()

df = df[~df.team.str.contains("/")]
df = df[~df.opponent.str.contains("/")]

# +
# create dummy variables, need to do this in python b/c does not handle automatically like R
team_dummies = pd.get_dummies(df.team, prefix='team')
opponent_dummies = pd.get_dummies(df.opponent, prefix='opponent')

df = pd.concat([df, team_dummies, opponent_dummies], axis=1)
# -

df.head()

df = df.dropna(subset=['scorediff','weights'])

# make the training data
X = df.drop(['team','opponent','teamscore','oppscore','season','weights','scorediff'], axis=1)
y = df['scorediff']
weights = df['weights']

X.head()

from sklearn.linear_model import Ridge
ridge_reg = Ridge()
ridge_reg.fit(X, y, sample_weight=weights)

# get the R^2 value
r_squared = ridge_reg.score(X, y, sample_weight=weights)
print('R^2 on the training data:')
print(r_squared)

# get the coefficients for each feature
coef_data = list(zip(X.columns,ridge_reg.coef_))
coef_df = pd.DataFrame(coef_data,columns=['feature','feature_coef'])
coef_df.head()

# +
# first get rid of opponent_ variables
team_df = coef_df[~coef_df['feature'].str.contains("opponent")]

# get rid of the location variable
team_df = team_df.iloc[1:]
# -

# rank them by coef, not alphabetical order
ranked_team_df = team_df.sort_values(['feature_coef'],ascending=False)
# reset the indices at 0
ranked_team_df = ranked_team_df.reset_index(drop=True);
ranked_team_df.rename(columns={'feature':'team', 'feature_coef':'YUSAG_coef'}, inplace=True)
ranked_team_df['team'] = ranked_team_df['team'].str.replace('team_', '')

ranked_team_df

ivy_team_names = ['Yale','Harvard','Princeton','Cornell','Brown','Columbia','Dartmouth','Penn']
ivy_ranked_team_df = ranked_team_df[ranked_team_df['team'].isin(ivy_team_names)]

ivy_ranked_team_df

ECAC_team_names = ['Yale','Harvard','Princeton','Cornell','Brown','Columbia','Dartmouth','Penn',
                  'Clarkson','Colgate','Union','<NAME>','Quinnipiac','Rensselaer']
ECAC_ranked_team_df = ranked_team_df[ranked_team_df['team'].isin(ECAC_team_names)]

ECAC_ranked_team_df

ECAC_ranked_team_df.to_csv("ECAC_ranked_team_df")

# I am now going to train a logistic regression model, so that we can do win probabilities.
#
# I am going to use multinomial linear regression to get probabilities for the three classes (0=loss, 1=tie, 2=win)

# Using the model to predict win probabilities
# First, let's figure out how the predicted score differentials translate into percentages.

df['predicted_scorediff'] = ridge_reg.predict(X)

df.head()

df['predicted_scorediff'] = df['predicted_scorediff'].apply(lambda x: round(x,2))

df.head()

# I'm only going to train the point spread to percentages model on the most recent data b/c that's what the YUSAG prediction model is optimized for. I.e. the predictions are not great when you go back a couple of years since we weights more recent results more heavily.

# ignore warnings from pandas (sorry my code probably isn't idiomatic)
import warnings
warnings.filterwarnings('ignore')

last_year_df = df[df['season']==2018]

last_year_df.loc[last_year_df.scorediff > 0, 'result'] = 2 # win
last_year_df.loc[last_year_df.scorediff == 0, 'result'] = 1 # tie
last_year_df.loc[last_year_df.scorediff < 0, 'result'] = 0 # tie

last_year_df[['scorediff','predicted_scorediff','result']].head(20)

# Now use a logistic regression model to predict the result based on the predicted_scorediff

from sklearn.linear_model import LogisticRegression

# make the training data
X = last_year_df['predicted_scorediff']
X = X.values.reshape(-1,1)
y = last_year_df['result']

log_reg = LogisticRegression()
# we create an instance of Neighbours Classifier and fit the data.
log_reg.fit(X, y)

log_reg.score(X, y)

log_reg.predict_proba(X)

# +
last_year_df['loss_prob'] = 0
last_year_df['loss_prob'] = log_reg.predict_proba(X)[:,0]

last_year_df['tie_prob'] = 0
last_year_df['tie_prob'] = log_reg.predict_proba(X)[:,1]

last_year_df['win_prob'] = 0
last_year_df['win_prob'] = log_reg.predict_proba(X)[:,2]
# -

last_year_df[['scorediff','predicted_scorediff','loss_prob','tie_prob','win_prob']]



StarcoderdataJupyterScriptsDedupFiltered