{"cells":[{"metadata":{},"cell_type":"markdown","source":"Analyzing Women"},{"metadata":{},"cell_type":"markdown","source":"Firstly, let's import the basic modules:\n"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport csv\nimport math\nimport os\nimport pandas as pd\nimport random\nimport sklearn\nfrom sklearn.model_selection import KFold\nfrom sklearn import linear_model\n#from sklearn import cross_validation, linear_model\nfrom sklearn.model_selection import cross_validate\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.model_selection import train_test_split\nimport datetime, time\nimport sys\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport re\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.\ndf_fdr =\"/kaggle/input/march-madness-analytics-2020/2020DataFiles/2020DataFiles/2020-Womens-Data/WDataFiles_Stage1/\"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Credits to Original code by @spencerthayer (2017) open source published on Github. This is an improved version.**** The First Part tackkles the men Stage1 & 2 data. While the second part of this algorithm handles the Women Stage1 and Stage2 data respectively."},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"if len(sys.argv) == 1:\n    print(\"- - - - - - - - - - - - - - - - - - - - - - - - -\")\n    print(\"NO DEFINED YEAR! QUITTING!\")\n    print(str(time.time()))\n    print(\"Use 'python3 NCAA.py [YEAR]' to define the year.\")\n    print(\"- - - - - - - - - - - - - - - - - - - - - - - - -\")\n    quit()\nelse:\n    theYear = sys.argv[1]\n\nprint(\"Generating results for \" + theYear +\".\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 2. Seasons Data Analysis\n\nSpecific year that the tournament has been dand event. Of cos the most recent being 2020."},{"metadata":{"trusted":true},"cell_type":"code","source":"seasondata=pd.read_csv(df_fdr +'WSeasons.csv')\nseasondata.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Total held seasons including the current\nseasondata['Season'].count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mteams=pd.read_csv(df_fdr +'WTeams.csv')\nmteams.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"** This part is for identifying the seeds for all teams in each NCAA® tournament, for all seasons of historical data. Geeting to know the seeds for all teams in the NCAA tournament from historic perspective**"},{"metadata":{"trusted":true},"cell_type":"code","source":"mseeds=pd.read_csv(df_fdr +'WNCAATourneySeeds.csv')\nmseeds.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mseeds = pd.merge(mseeds, mteams,on='TeamID')\nmseeds.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Spliting regions from the Seeds\n\nmseeds['Region'] = mseeds['Seed'].apply(lambda x: x[0][:1])\nmseeds['Seed'] = mseeds['Seed'].apply(lambda x: int(x[1:3]))\nprint(mseeds.head())\nprint(mseeds.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Teams with maximum top seeds\nfig = plt.gcf()\nfig.set_size_inches(10, 6)\ncolors = ['dodgerblue', 'plum', '#F0A30A','#8c564b','orange','green','yellow'] \n\nmseeds[mseeds['Seed'] ==1]['TeamName'].value_counts()[:10].plot(kind='bar',color=colors,linewidth=2,edgecolor='black')\nplt.xlabel('Number of times in Top seeded positions')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mcompactre = pd.read_csv(df_fdr +'WRegularSeasonCompactResults.csv')\nmcompactre.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#  Score Avergae for Win vs Loss over a certain past\nx = mcompactre.groupby('Season')[['WScore','LScore']].mean()\n\nfig = plt.gcf()\nfig.set_size_inches(28, 12)\nplt.plot(x.index,x['WScore'],marker='o', markerfacecolor='green', markersize=12, color='green', linewidth=4)\nplt.plot(x.index,x['LScore'],marker=7, markerfacecolor='red', markersize=12, color='red', linewidth=4)\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#df_fdr = '/kaggle/working/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"files = os.listdir(df_fdr)\nnames = [x.split('.')[0] for x in files]\npath_list = [f'{df_fdr}{x}' for x in files]\ndf_dict = {}\nfor num, name in enumerate(names):\n    try:\n        df_dict[name] = pd.read_csv(path_list[num])\n    except:\n        print(f'{name} did not load')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Summary of files:\\n___________________________')\nfor name, df in df_dict.items():\n    print(f'{name}: {df.shape}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > DATA\npath_datasets = df_fdr\n\ndf_rs_c_res = pd.read_csv(path_datasets + 'WRegularSeasonCompactResults.csv')\ndf_rs_d_res = pd.read_csv(path_datasets + 'WRegularSeasonDetailedResults.csv')\ndf_teams = pd.read_csv(path_datasets + 'WTeams.csv')\ndf_seeds = pd.read_csv(path_datasets + 'WNCAATourneySeeds.csv')\ncoaches = pd.read_csv(path_datasets + 'WTeams.csv')\ndf_tourn = pd.read_csv(path_datasets + 'WNCAATourneyCompactResults.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > DATA CLEANING\n# clean team information\n\ndf_teams_cl = df_teams.iloc[:,:2]\n\n## > DATA CLEANING\n# clean seed information\n\ndf_seeds_cl = df_seeds.loc[:, ['TeamID', 'Season', 'Seed']]\n\ndef clean_seed(seed):\n    s_int = int(seed[1:3])\n    return s_int\n\ndef extract_seed_region(seed):\n    s_reg = seed[0:1]\n    return s_reg\n\ndf_seeds_cl['seed_int'] = df_seeds_cl['Seed'].apply(lambda x: clean_seed(x))\ndf_seeds_cl['seed_region'] = df_seeds_cl['Seed'].apply(lambda x: extract_seed_region(x))\ndf_seeds_cl['top_seeded_teams'] = np.where(df_seeds_cl['Seed'].isnull(), 0, 1)\n\ndf_seeds_cl.drop(labels=['Seed'], inplace=True, axis=1) # This is the string label\ndf_seeds_cl.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > DATA CLEANING\n# create games dataframe WINNERS\n\ndef new_name_w_1(old_name):\n    match = re.match(r'^L', old_name)\n    if match:\n        out = re.sub('^L','', old_name)\n        return out + '_opp'\n    return old_name\n\ndef new_name_w_2(old_name):\n    match = re.match(r'^W', old_name)\n    if match:\n        out = re.sub('^W','', old_name)\n        return out\n    return old_name\n\ndef prepare_stats_extended_winners(df_in, df_seed_in, df_teams_in):\n    df_in['poss'] = df_in['WFGA'] + 0.475*df_in['WFTA'] - df_in['WOR'] + df_in['WTO']\n    df_in['opp_poss'] = df_in['LFGA'] + 0.475*df_in['LFTA'] - df_in['LOR'] + df_in['LTO']\n    df_in['off_rating'] = 100*(df_in['WScore'] / df_in['poss'])\n    df_in['def_rating'] = 100*(df_in['LScore'] / df_in['opp_poss'])\n    df_in['net_rating'] = df_in['off_rating'] - df_in['def_rating']\n    df_in['pace'] = 48*((df_in['poss']+df_in['opp_poss'])/(2*(240/5)))\n    \n    df_in = df_in.rename(columns={'WTeamID':'TeamID', \n                                  'WLoc':'_Loc',\n                                  'LTeamID':'TeamID_opp',\n                                  'WScore':'Score_left', \n                                  'LScore':'Score_right'})\n    \n    df_seeds_opp = df_seed_in.rename(columns={'TeamID':'TeamID_opp',\n                                              'seed_int':'seed_int_opp',\n                                              'seed_region':'seed_region_opp',\n                                              'top_seeded_teams':'top_seeded_teams_opp'})\n    \n    df_out = pd.merge(left=df_in, right=df_seeds_cl, how='left', on=['Season', 'TeamID'])\n    df_out = pd.merge(left=df_out, right=df_seeds_opp, how='left', on=['Season', 'TeamID_opp'])\n    df_out = pd.merge(left=df_out, right=df_teams_in, how='left', on=['TeamID'])\n    \n    df_out['DayNum'] = pd.to_numeric(df_out['DayNum'])\n    df_out['win_dummy'] = 1\n    \n    df_out['seed_int'] = np.where(df_out['seed_int'].isnull(), 20, df_out['seed_int'])\n    df_out['seed_region'] = np.where(df_out['seed_region'].isnull(), 'NoTour', df_out['seed_region'])\n    df_out['top_seeded_teams'] = np.where(df_out['top_seeded_teams'].isnull(), 0, df_out['top_seeded_teams'])\n    \n    df_out['seed_int_opp'] = np.where(df_out['seed_int_opp'].isnull(), 20, df_out['seed_int_opp'])\n    df_out['seed_region_opp'] = np.where(df_out['seed_region_opp'].isnull(), 'NoTour', df_out['seed_region_opp'])\n    df_out['top_seeded_teams_opp'] = np.where(df_out['top_seeded_teams_opp'].isnull(), 0, df_out['top_seeded_teams_opp'])\n    \n    df_out = df_out.rename(columns=new_name_w_1)\n    df_out = df_out.rename(columns=new_name_w_2)\n    \n    return df_out\n\ndf_games_w = prepare_stats_extended_winners(df_rs_d_res, df_seeds_cl, df_teams_cl)\n\ndf_games_w.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > DATA CLEANING\n# create games dataframe LOSERS\n\ndef new_name_l_1(old_name):\n    match = re.match(r'^W', old_name)\n    if match:\n        out = re.sub('^W','', old_name)\n        return out + '_opp'\n    return old_name\n\ndef new_name_l_2(old_name):\n    match = re.match(r'^L', old_name)\n    if match:\n        out = re.sub('^L','', old_name)\n        return out\n    return old_name\n\ndef prepare_stats_extended_losers(df_in, df_seed_in, df_teams_in):\n    df_in['poss'] = df_in['LFGA'] + (0.475*df_in['LFTA']) - df_in['LOR'] + df_in['LTO']\n    df_in['opp_poss'] = df_in['WFGA'] + (0.475*df_in['WFTA']) - df_in['WOR'] + df_in['WTO']\n    df_in['off_rating'] = 100*(df_in['LScore'] / df_in['poss'])\n    df_in['def_rating'] = 100*(df_in['WScore'] / df_in['opp_poss'])\n    df_in['net_rating'] = df_in['off_rating'] - df_in['def_rating']\n    df_in['pace'] = 48*((df_in['poss']+df_in['opp_poss'])/(2*(240/5)))\n    \n    df_in = df_in.rename(columns={'LTeamID':'TeamID', \n                                  'LLoc':'_Loc',\n                                  'WTeamID':'TeamID_opp',\n                                  'LScore':'Score_left', \n                                  'WScore':'Score_right'})\n    \n    df_seeds_opp = df_seed_in.rename(columns={'TeamID':'TeamID_opp',\n                                              'seed_int':'seed_int_opp',\n                                              'seed_region':'seed_region_opp',\n                                              'top_seeded_teams':'top_seeded_teams_opp'})\n    \n    df_out = pd.merge(left=df_in, right=df_seeds_cl, how='left', on=['Season', 'TeamID'])\n    df_out = pd.merge(left=df_out, right=df_seeds_opp, how='left', on=['Season', 'TeamID_opp'])\n    df_out = pd.merge(left=df_out, right=df_teams_in, how='left', on=['TeamID'])\n    \n    df_out['DayNum'] = pd.to_numeric(df_out['DayNum'])\n    df_out['win_dummy'] = 0\n    \n    df_out['seed_int'] = np.where(df_out['seed_int'].isnull(), 20, df_out['seed_int'])\n    df_out['seed_region'] = np.where(df_out['seed_region'].isnull(), 'NoTour', df_out['seed_region'])\n    df_out['top_seeded_teams'] = np.where(df_out['top_seeded_teams'].isnull(), 0, df_out['top_seeded_teams'])\n    \n    df_out['seed_int_opp'] = np.where(df_out['seed_int_opp'].isnull(), 20, df_out['seed_int_opp'])\n    df_out['seed_region_opp'] = np.where(df_out['seed_region_opp'].isnull(), 'NoTour', df_out['seed_region_opp'])\n    df_out['top_seeded_teams_opp'] = np.where(df_out['top_seeded_teams_opp'].isnull(), 0, df_out['top_seeded_teams_opp'])\n\n    df_out = df_out.rename(columns=new_name_l_1)\n    df_out = df_out.rename(columns=new_name_l_2)\n    \n    return df_out\n\ndf_games_l = prepare_stats_extended_losers(df_rs_d_res, df_seeds_cl, df_teams_cl)\n\ndf_games_l.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > MERGE\n\ndf_games_t = pd.concat([df_games_w,df_games_l], sort=True)\n\n## > AGGREGATED STATS BY TEAM AND SEASON\n\ndef aggr_stats(df):\n    d = {}\n    d['G'] = df['win_dummy'].count()\n    d['W'] = df['win_dummy'].sum()\n    d['L'] = np.sum(df['win_dummy'] == 0)\n    d['G_vs_topseeds'] = np.sum(df['top_seeded_teams_opp'] == 1)\n    d['W_vs_topseeds'] = np.sum((df['win_dummy'] == 1) & (df['top_seeded_teams_opp'] == 1))\n    d['L_vs_topseeds'] = np.sum((df['win_dummy'] == 0) & (df['top_seeded_teams_opp'] == 1))\n    d['G_last30D'] = np.sum((df['DayNum'] > 100))\n    d['W_last30D'] = np.sum((df['win_dummy'] == 1) & (df['DayNum'] > 100))\n    d['L_last30D'] = np.sum((df['win_dummy'] == 0) & (df['DayNum'] > 100))\n    d['G_H'] = np.sum((df['_Loc'] == 'H'))\n    d['W_H'] = np.sum((df['win_dummy'] == 1) & (df['_Loc'] == 'H'))\n    d['L_H'] = np.sum((df['win_dummy'] == 0) & (df['_Loc'] == 'H'))\n    d['G_A'] = np.sum((df['_Loc'] == 'A'))\n    d['W_A'] = np.sum((df['win_dummy'] == 1) & (df['_Loc'] == 'A'))\n    d['L_A'] = np.sum((df['win_dummy'] == 0) & (df['_Loc'] == 'A'))\n    d['G_N'] = np.sum((df['_Loc'] == 'N'))\n    d['W_N'] = np.sum((df['win_dummy'] == 1) & (df['_Loc'] == 'N'))\n    d['L_N'] = np.sum((df['win_dummy'] == 0) & (df['_Loc'] == 'N'))\n    \n    d['PS'] = np.mean(df['Score_left'])\n    d['PS_H'] = np.mean(df['Score_left'][df['_Loc'] == 'H'])\n    d['PS_A'] = np.mean(df['Score_left'][df['_Loc'] == 'A'])\n    d['PS_N'] = np.mean(df['Score_left'][df['_Loc'] == 'N'])\n    d['PS_last30D'] = np.mean(df['Score_left'][df['DayNum'] > 100])\n    \n    d['PA'] = np.mean(df['Score_right'])\n    d['PA_H'] = np.mean(df['Score_right'][df['_Loc'] == 'H'])\n    d['PA_A'] = np.mean(df['Score_right'][df['_Loc'] == 'A'])\n    d['PA_N'] = np.mean(df['Score_right'][df['_Loc'] == 'N'])\n    d['PA_last30D'] = np.mean(df['Score_right'][df['DayNum'] > 100])\n    \n    d['poss_m'] = np.mean(df['poss'])\n    d['opp_poss_m'] = np.mean(df['opp_poss'])\n    d['off_rating_m'] = np.mean(df['off_rating'])\n    d['def_rating_m'] = np.mean(df['def_rating'])\n    d['net_rating_m'] = np.mean(df['net_rating'])\n    d['pace_m'] = np.mean(df['pace'])\n    \n    d['off_rating_m_last30D'] = np.mean(df['off_rating'][df['DayNum'] > 100])\n    d['def_rating_m_last30D'] = np.mean(df['def_rating'][df['DayNum'] > 100])\n    d['net_rating_m_last30D'] = np.mean(df['net_rating'][df['DayNum'] > 100])\n    \n    d['off_rating_m_vs_topseeds'] = np.mean(df['off_rating'][df['top_seeded_teams_opp'] == 1])\n    d['def_rating_m_vs_topseeds'] = np.mean(df['def_rating'][df['top_seeded_teams_opp'] == 1])\n    d['net_rating_m_vs_topseeds'] = np.mean(df['net_rating'][df['top_seeded_teams_opp'] == 1])\n    \n    return pd.Series(d)\n\n\ndf_agg_stats = df_games_t.\\\n                          groupby([df_games_t['Season'], \n                                   df_games_t['TeamID'],\n                                   df_games_t['TeamName'],\n                                   df_games_t['seed_int'],\n                                   df_games_t['seed_region']], \n                                  as_index=False).\\\n                          apply(aggr_stats).\\\n                          reset_index()\n\n\ndf_agg_stats['w_pct'] = df_agg_stats['W'] / df_agg_stats['G']\ndf_agg_stats['w_pct_last30D'] = df_agg_stats['W_last30D'] / df_agg_stats['G_last30D']\ndf_agg_stats['w_pct_vs_topseeds'] = df_agg_stats['W_vs_topseeds'] / df_agg_stats['G_vs_topseeds']\n\ndf_agg_stats.head(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > DATA CLEANING \n\n# prepare tournament dataset\ndef prepare_tournament_datasets(df_tourn_in, df_agg_stats_in):\n    \n    df_tourn_in['TeamID'] = df_tourn_in[['WTeamID','LTeamID']].min(axis=1)\n    df_tourn_in['TeamID_opp'] = df_tourn_in[['WTeamID','LTeamID']].max(axis=1)\n    df_tourn_in['win_dummy'] = np.where(df_tourn_in['TeamID'] == df_tourn_in['WTeamID'], 1, 0)\n    df_tourn_in['delta'] = np.where(df_tourn_in['win_dummy'] == 1,\n                                    df_tourn_in['WScore'] - df_tourn['LScore'],\n                                    df_tourn_in['LScore'] - df_tourn['WScore'])\n    df_tourn_in['Score_left'] = np.where(df_tourn_in['win_dummy'] == 1,\n                                         df_tourn_in['WScore'],\n                                         df_tourn_in['LScore'])\n    df_tourn_in['Score_right'] = np.where(df_tourn_in['win_dummy'] == 1,\n                                          df_tourn_in['LScore'],\n                                          df_tourn_in['WScore'])\n                                 \n    df_teams_gr_left = df_agg_stats_in.loc[:,['Season', 'TeamID',\n                                              'w_pct', 'seed_int', \n                                              'net_rating_m_last30D',\n                                              'net_rating_m_vs_topseeds',\n                                              'net_rating_m']].\\\n                  rename(columns={'w_pct':'w_pct_left',\n                                  'seed_int':'seed_int_left', \n                                  'net_rating_m_last30D':'net_rating_m_last30D_left', \n                                  'net_rating_m_vs_topseeds':'net_rating_m_vs_topseeds_left', \n                                  'net_rating_m':'net_rating_m_left'})\n    \n    df_teams_gr_right = df_agg_stats_in.loc[:,['Season', 'TeamID',\n                                               'w_pct', 'seed_int',\n                                               'net_rating_m_last30D',\n                                               'net_rating_m_vs_topseeds',\n                                               'net_rating_m']].\\\n                  rename(columns={'TeamID':'TeamID_opp',\n                                  'w_pct':'w_pct_right',\n                                  'seed_int':'seed_int_right', \n                                  'net_rating_m_last30D':'net_rating_m_last30D_right', \n                                  'net_rating_m_vs_topseeds':'net_rating_m_vs_topseeds_right', \n                                  'net_rating_m':'net_rating_m_right'})\n    \n    df_tourn_out = pd.merge(left=df_tourn_in, \n                            right=df_teams_gr_left, \n                            how='left', on=['Season', 'TeamID'])\n    df_tourn_out = pd.merge(left=df_tourn_out, \n                            right=df_teams_gr_right, \n                            how='left', on=['Season', 'TeamID_opp'])\n\n    df_tourn_out['delta_w_pct'] = df_tourn_out['w_pct_left'] - \\\n                                         df_tourn_out['w_pct_right']\n\n\n    df_tourn_out['delta_seed_int'] = df_tourn_out['seed_int_left'] - \\\n                                          df_tourn_out['seed_int_right']\n\n\n    df_tourn_out['delta_net_rating_m'] = df_tourn_out['net_rating_m_left'] - df_tourn_out['net_rating_m_right']\n    \n    df_tourn_out['delta_net_rating_m_last30D'] = df_tourn_out['net_rating_m_last30D_left'] - df_tourn_out['net_rating_m_last30D_right']\n    \n    df_tourn_out['delta_net_rating_m_vs_topseeds'] = df_tourn_out['net_rating_m_vs_topseeds_left'] - df_tourn_out['net_rating_m_vs_topseeds_right']\n    \n    df_out = df_tourn_out.loc[:, ['Season', 'DayNum',\n                                  'TeamID', 'TeamID_opp',\n                                  'Score_left', 'Score_right',\n                                  'win_dummy', \n                                  'delta', 'NumOT', 'delta_w_pct', \n                                  'delta_net_rating_m_last30D',\n                                  'delta_net_rating_m_vs_topseeds',\n                                  'delta_net_rating_m', 'delta_seed_int']]\n                                    \n    return df_out\n\n                                    \ndf_tourn_cl = prepare_tournament_datasets(df_tourn, df_agg_stats)                                    \ndf_tourn_cl[(df_tourn_cl['Season'].isin([2015, 2016, 2017, 2018]))].head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > DUKE RS\ndf_agg_stats[(df_agg_stats['TeamName'] == 'Duke') & (df_agg_stats['Season'] == 2019)].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > DUKE TOURNAMENT\ndf_tourn_cl[((df_tourn_cl['TeamID'] == 1181) | (df_tourn_cl['TeamID_opp'] == 1181)) & \\\n            (df_tourn_cl['Season'] == 2019)].head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > DATA VIZ RS\nsns.set(style=\"ticks\", color_codes=True)\n\ndf_teams_gr = df_agg_stats.loc[:,['w_pct',\n                                  'net_rating_m', 'net_rating_m_last30D', \n                                  'net_rating_m_vs_topseeds', 'pace_m']]\n\ndf_teams_gr = df_teams_gr.fillna(0)\n\n#df_teams_gr.describe()\nsns.pairplot(df_teams_gr, palette=\"Set1\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > DATA VIZ TOURNEY\nsns.set(style=\"ticks\", color_codes=True)\n\ndf_tourn_cl_gr = df_tourn_cl[(df_tourn_cl['Season'].isin([2015, 2016, 2017, 2018]))].reindex()\n\ndf_tourn_cl_gr = df_tourn_cl_gr.loc[:,['win_dummy',\n                                       'delta_net_rating_m_last30D',\n                                       'delta_net_rating_m_vs_topseeds',\n                                       'delta_net_rating_m',  \n                                       'delta_seed_int']]\n\nfig, ax = plt.subplots(figsize=(11, 7))\nsns.boxplot(x=\"variable\", y=\"value\", hue = 'win_dummy', ax=ax, \n            data=pd.melt(df_tourn_cl_gr, id_vars='win_dummy'), palette=\"Set2\")\nplt.xticks(rotation=45)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > DATA VIZ TOURNEY\ndf_tourn_cl_gr = df_tourn_cl[(df_tourn_cl['Season'].isin([2015, 2016, 2017, 2018]))].reindex()\n\ndf_tourn_cl_gr = df_tourn_cl_gr.loc[:,['win_dummy',\n                                       'delta_w_pct']]\n\nfig, ax = plt.subplots(figsize=(9, 7))\nsns.boxplot(x=\"variable\", y=\"value\", hue = 'win_dummy', ax=ax, \n            data=pd.melt(df_tourn_cl_gr, id_vars='win_dummy'), palette=\"Set2\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > Correlation\n# Compute the correlation matrix\ndf_tourn_cl_gr = df_tourn_cl[(df_tourn_cl['Season'].isin([2015, 2016, 2017, 2018]))].reindex()\n\ndf_tourn_cl_gr = df_tourn_cl_gr.loc[:,['win_dummy',\n                                       'delta_net_rating_m_last30D',\n                                       'delta_net_rating_m_vs_topseeds',                                       \n                                       'delta_net_rating_m',  \n                                       'delta_w_pct',\n                                       'delta_seed_int']].fillna(0)\n\ncorr = df_tourn_cl_gr.corr()\nfig, ax = plt.subplots(figsize=(11, 7))\nsns.heatmap(corr, cmap=\"YlGnBu\", ax = ax)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## > AR\ndef somers2_py(x, y):\n    \n    from sklearn.metrics import roc_auc_score\n    \n    C = roc_auc_score(y, x)\n    Dxy = (2 * roc_auc_score(y, x))  - 1\n    \n    return Dxy, C\n\ndef apply_somers(df):\n    \n    d = {}\n    \n    dxy, cxy = somers2_py(df['value'],\n                          df['win_dummy'])\n    \n    d['Dxy'] = dxy\n    d['C'] = cxy\n    \n    \n    return pd.Series(d)\n\ndf_tourn_cl_gr = df_tourn_cl[(df_tourn_cl['Season'].isin([2015, 2016, 2017, 2018,2019]))].reindex()\n\ndf_tourn_cl_gr = df_tourn_cl_gr.loc[:,['win_dummy',\n                                       'delta_net_rating_m_last30D',\n                                       'delta_net_rating_m_vs_topseeds',                                       \n                                       'delta_net_rating_m',  \n                                       'delta_w_pct',\n                                       'delta_seed_int']].fillna(0)\n\ndf_ar = pd.melt(df_tourn_cl_gr, id_vars='win_dummy')\n\ndf_ar.groupby(['variable']).\\\n                          apply(apply_somers).\\\n                          reset_index().\\\n                          sort_values(by=['Dxy'], ascending=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**References:**\n\n1. \tboyuan-li: Kaggle-GCP-NCAA-ML-2019 \n\thttps://github.com/boyuan-li/Kaggle-GCP-NCAA-ML-2019:\n\n2.        wdg3: Deep neural network that attempts to predict the NCAA \tMarch Madness tournament: https://github.com/wdg3/march-madness\n\n3. \tDecoding March Madness Kaggle Notebook:@Parul Pandey Data Science Evangelist at \tH2O.ai"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.6"}},"nbformat":4,"nbformat_minor":4}