{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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 os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom functools import reduce\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\ninput_dir = '/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/'\nstage1_dir = input_dir + 'MDataFiles_Stage1/'\n\nteams = pd.read_csv(stage1_dir + \"MTeams.csv\")\nseasons = pd.read_csv(stage1_dir + \"MSeasons.csv\")\nseeds = pd.read_csv(stage1_dir + \"MNCAATourneySeeds.csv\")\nregular_res = pd.read_csv(stage1_dir + \"MRegularSeasonCompactResults.csv\")\nbracket_res = pd.read_csv(stage1_dir + \"MNCAATourneyCompactResults.csv\")\nsample_sub = pd.read_csv(input_dir + \"MSampleSubmissionStage1_2020.csv\")\nregular_detail = pd.read_csv(stage1_dir + \"MRegularSeasonDetailedResults.csv\")\nbracket_detail = pd.read_csv(stage1_dir + \"MNCAATourneyDetailedResults.csv\")\nordinals = pd.read_csv(stage1_dir + \"MMasseyOrdinals.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"regular_detail.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"regular_detail['WFGP'] = regular_detail['WFGM'] / regular_detail['WFGA']\nregular_detail['LFGP'] = regular_detail['LFGM'] / regular_detail['LFGA']\nregular_detail['WFTP'] = regular_detail['WFTM'] / regular_detail['WFTA']\nregular_detail['LFTP'] = regular_detail['LFTM'] / regular_detail['LFTA']\nregular_detail['WFGP3'] = regular_detail['WFGM3'] / regular_detail['WFGA3']\nregular_detail['LFGP3'] = regular_detail['LFGM3'] / regular_detail['LFGA3']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"all_games = regular_res.merge(regular_detail, how='inner', on=['Season', 'DayNum', 'WTeamID', 'LTeamID'], suffixes=('', '_y'))\nall_games.drop(columns=['LScore_y', 'WScore_y', 'NumOT_y', 'WLoc_y'], inplace=True)\nall_games.head()\n\nstats = ['Score', 'FTM', 'FGM3', 'FGM', 'TO', 'OR', 'DR', 'Stl', 'Ast', 'Blk', 'FGP', 'FTP', 'FGP3']\n_stat_abbrevs_overrides = {'Score': 'PPG'}\nstat_abbrevs1 = {stat: stat for stat in stats}\nstat_abbrevs2 = {'Opp'+stat: 'O'+stat for stat in stats}\nstat_abbrevs = {**stat_abbrevs1, **stat_abbrevs2}\nstat_abbrevs_overrides1 = {key: _stat_abbrevs_overrides[key] for key in _stat_abbrevs_overrides}\nstat_abbrevs_overrides2 = {'Opp'+key: 'O'+_stat_abbrevs_overrides[key] for key in _stat_abbrevs_overrides}\nstat_abbrevs_overrides = {**stat_abbrevs_overrides1, **stat_abbrevs_overrides2}\nstat_abbrevs = {**stat_abbrevs, **stat_abbrevs_overrides}\n\nfinal_stats_cols = reduce(lambda a,b: a+b, [[stat, 'Opp'+stat] for stat in stats])\nstats_cols = reduce(lambda a,b: a+b, [['W'+stat, 'L'+stat] for stat in stats])\nstats_win_map = {'W'+stat: stat for stat in stats}\nstats_win_map_opp = {'L'+stat: 'Opp'+stat for stat in stats}\nstats_loss_map = {'L'+stat: stat for stat in stats}\nstats_loss_map_opp = {'W'+stat: 'Opp'+stat for stat in stats}\n\nstat_rename_win = {s: stat_abbrevs[s]+'1' for s in stat_abbrevs}\nstat_rename_loss = {s: stat_abbrevs[s]+'2' for s in stat_abbrevs}\n\nwins = all_games[['Season', 'WTeamID'] + stats_cols]\nwins.rename(columns={'WTeamID': 'TeamID', **stats_win_map, **stats_win_map_opp}, inplace=True)\n\nlosses = all_games[['Season', 'LTeamID'] + stats_cols]\nlosses.rename(columns={'LTeamID': 'TeamID', **stats_loss_map, **stats_loss_map_opp}, inplace=True)\nreg_games = pd.concat((wins, losses), sort=True).reset_index(drop=True)\nteams_group = reg_games.groupby(['Season', 'TeamID'])\n\n# TODO support other aggregation methods\nseason_stats = teams_group[final_stats_cols].median().reset_index()\nseason_stats","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tourney_res = bracket_res.drop(['DayNum', 'WScore', 'LScore', 'WLoc', 'NumOT'], axis=1)\ntourney_res = tourney_res.merge(seeds, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')\ntourney_res = tourney_res.rename(columns={'Seed': 'WSeed'})\ntourney_res = tourney_res.drop('TeamID', axis=1)\ntourney_res = tourney_res.merge(seeds, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')\ntourney_res = tourney_res.rename(columns={'Seed': 'LSeed'})\ntourney_res = tourney_res.drop('TeamID', axis=1)\n\ntourney_res['WSeed'] = tourney_res['WSeed'].map(lambda x: int(x[1:3]))\ntourney_res['LSeed'] = tourney_res['LSeed'].map(lambda x: int(x[1:3]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tourney_wins_stats = tourney_res.merge(season_stats[['Season', 'TeamID'] + list(stat_rename_win.keys())], left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')\ntourney_wins_stats.rename(columns=stat_rename_win, inplace=True)\ntourney_wins_stats = tourney_wins_stats.drop('TeamID', axis=1)\ntourney_wins_stats = tourney_wins_stats[tourney_wins_stats[list(stat_rename_win.values())[0]].notnull()]\ntourney_loss_stats = tourney_res.merge(season_stats[['Season', 'TeamID'] + list(stat_rename_loss.keys())], left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')\ntourney_loss_stats.rename(columns=stat_rename_loss, inplace=True)\ntourney_loss = tourney_loss_stats.drop('TeamID', axis=1)\ntourney_loss_stats = tourney_loss_stats[tourney_loss_stats[list(stat_rename_loss.values())[0]].notnull()]\ntourney_res = tourney_wins_stats.merge(tourney_loss_stats, on=['Season', 'WTeamID', 'LTeamID', 'WSeed', 'LSeed'])\ntourney_res = tourney_res.drop('TeamID', axis=1)\ntourney_res","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tourney_wins = tourney_res.drop(['Season', 'WTeamID', 'LTeamID'], axis=1)\ntourney_wins.rename(columns={'WSeed': 'Seed1', 'LSeed': 'Seed2'}, inplace=True)\n\ntourney_loss = tourney_wins.copy()\ntourney_loss['Seed1'] = tourney_wins['Seed2']\ntourney_loss['Seed2'] = tourney_wins['Seed1']\nfor stat in stat_abbrevs.values():\n    tourney_loss[stat+'1'] = tourney_wins[stat+'2']\n    tourney_loss[stat+'2'] = tourney_wins[stat+'1']\n\ntourney_wins['outcome'] = 1\ntourney_loss['outcome'] = 0\ntrain = pd.concat((tourney_wins, tourney_loss), sort=True).reset_index(drop=True)\ntrain","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = sample_sub.copy()\ntest_df['Season'] = sample_sub['ID'].map(lambda x: int(x[:4]))\ntest_df['WTeamID'] = sample_sub['ID'].map(lambda x: int(x[5:9]))\ntest_df['LTeamID'] = sample_sub['ID'].map(lambda x: int(x[10:14]))\n\ntest_df = test_df.merge(seeds, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')\ntest_df = test_df.rename(columns={'Seed': 'Seed1'})\ntest_df = test_df.drop('TeamID', axis=1)\ntest_df = test_df.merge(seeds, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')\ntest_df = test_df.rename(columns={'Seed': 'Seed2'})\ntest_df = test_df.drop('TeamID', axis=1)\n\ntest_df = test_df.merge(season_stats, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')\ntest_df.rename(columns=stat_rename_win, inplace=True)\ntest_df = test_df.drop('TeamID', axis=1)\ntest_df = test_df.merge(season_stats, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')\ntest_df.rename(columns=stat_rename_loss, inplace=True)\ntest_df = test_df.drop('TeamID', axis=1)\n\ntest_df['Seed1'] = test_df['Seed1'].map(lambda x: int(x[1:3]))\ntest_df['Seed2'] = test_df['Seed2'].map(lambda x: int(x[1:3]))\ntest = test_df.drop(['ID', 'Pred', 'Season', 'WTeamID', 'LTeamID'], axis=1)\ntest","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = train.drop('outcome', axis=1)\ny = train.outcome","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn import preprocessing\ndf = pd.concat([X, test], axis=0, sort=False).reset_index(drop=True)\ndf_log = pd.DataFrame(\n    preprocessing.MinMaxScaler().fit_transform(df),\n    columns=df.columns,\n    index=df.index\n)\ntrain_log, test_log = df_log.iloc[:len(X), :], df_log.iloc[len(X):,:].reset_index(drop=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\n\n# scaled\n#clf = LogisticRegression(penalty='elasticnet', solver='saga', l1_ratio=0.75, random_state=2).fit(train_log, y)\nclf = LogisticRegression(penalty='l1', solver='saga', random_state=2).fit(train_log, y)\nprobs = clf.predict_proba(test_log)\n\nteam1_probs = [x[1] for x in probs]\n\nres_df_orig = pd.DataFrame({'Season': test_df['Season'], 'Team1ID': test_df['WTeamID'], 'Team2ID': test_df['LTeamID'], 'Pred': team1_probs})\nres_df_orig['ID'] = res_df_orig.apply(lambda row: str(int(row['Season'])) + '_' + str(int(row['Team1ID'])) + '_' + str(int(row['Team2ID'])), axis=1)\nres_df = res_df_orig[['ID', 'Pred']]\nres_df\nres_df.to_csv('submission.csv', index=False)\nres_df['Pred'].hist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import log_loss\n\ncheck_df = bracket_res[bracket_res['Season'] >= 2015].merge(res_df_orig, right_on=['Season', 'Team1ID', 'Team2ID'], left_on=['Season', 'WTeamID', 'LTeamID'], how='left')\ncheck_df2 = bracket_res[bracket_res['Season'] >= 2015].merge(res_df_orig, right_on=['Season', 'Team1ID', 'Team2ID'], left_on=['Season', 'LTeamID', 'WTeamID'], how='left')\n\ncheck_df = check_df[check_df['Team1ID'].notnull()]\ncheck_df['outcome'] = 1\ncheck_df2 = check_df2[check_df2['Team1ID'].notnull()]\ncheck_df2['outcome'] = 0\nchecker = pd.concat((check_df, check_df2), sort=True)\nchecker.drop(['DayNum', 'LScore', 'NumOT', 'WLoc', 'WScore'], axis=1, inplace=True)\nlog_loss(checker['outcome'], checker['Pred'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"i = 0\nfor col in train_log.columns:\n    print(col, ': ', clf.coef_[0][i], '%')\n    i += 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}