{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"tourney_results=pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyDetailedResults.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"regular_results=pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyDetailedResults.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"regular_results.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def prepare_data(df):\n    dfswap = df[['Season', 'DayNum', 'LTeamID', 'LScore', 'WTeamID', 'WScore', 'WLoc', 'NumOT', \n    'LFGM', 'LFGA', 'LFGM3', 'LFGA3', 'LFTM', 'LFTA', 'LOR', 'LDR', 'LAst', 'LTO', 'LStl', 'LBlk', 'LPF', \n    'WFGM', 'WFGA', 'WFGM3', 'WFGA3', 'WFTM', 'WFTA', 'WOR', 'WDR', 'WAst', 'WTO', 'WStl', 'WBlk', 'WPF']]\n\n    dfswap.loc[df['WLoc'] == 'H', 'WLoc'] = 'A'\n    dfswap.loc[df['WLoc'] == 'A', 'WLoc'] = 'H'\n    df.columns.values[6] = 'location'\n    dfswap.columns.values[6] = 'location'    \n      \n    df.columns = [x.replace('W','T1_').replace('L','T2_') for x in list(df.columns)]\n    dfswap.columns = [x.replace('L','T1_').replace('W','T2_') for x in list(dfswap.columns)]\n\n    output = pd.concat([df, dfswap]).reset_index(drop=True)\n    output.loc[output.location=='N','location'] = '0'\n    output.loc[output.location=='H','location'] = '1'\n    output.loc[output.location=='A','location'] = '-1'\n    output.location = output.location.astype(int)\n    \n    output['PointDiff'] = output['T1_Score'] - output['T2_Score']\n    \n    return output","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"regular_data=prepare_data(regular_results)\ntourney_data=prepare_data(tourney_results)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tourney_data.loc[0:1115,'Result']=1\ntourney_data.loc[1115:2230,'Result']=0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"stat_cols=[col for col in regular_data.columns if col not in ['Season', 'DayNum', 'T1_TeamID', 'T1_Score', 'T2_TeamID', 'T2_Score',\n       'location', 'NumOT']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"stat_seasons=regular_data.groupby(['Season','T1_TeamID'])[stat_cols].agg([np.mean]).reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"stat_seasons","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"stat_seasons.columns.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"stat_seasons.columns=[''.join(col).strip() for col in stat_seasons.columns.values]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"stat_seasons.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"stat_seasons_T1=stat_seasons.copy()\nstat_seasons_T2=stat_seasons.copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"stat_seasons_T1.columns=['T1'+ x.replace('T1','').replace('T2','oppponent') for x in stat_seasons_T1.columns]\nstat_seasons_T2.columns=['T2'+ x.replace('T1','').replace('T2','oppponent') for x in stat_seasons_T2.columns]\nstat_seasons_T1.columns.values[0]=\"Season\"\nstat_seasons_T2.columns.values[0]=\"Season\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"stat_seasons_T1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tourney_data = tourney_data[['Season', 'DayNum', 'T1_TeamID', 'T1_Score', 'T2_TeamID' ,'T2_Score','Result']]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tourney_data=pd.merge(tourney_data,stat_seasons_T1,on=['Season','T1_TeamID'],how='left')\ntourney_data=pd.merge(tourney_data,stat_seasons_T2,on=['Season','T2_TeamID'],how='left')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"seeds_data=pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneySeeds.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"seeds_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def extra(x):\n    if len(x)==3:\n        return -1\n    elif x[3]=='a':\n        return 1\n    else:\n        return 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"seeds_data['seed']=seeds_data['Seed'].apply(lambda x : int(x[1:3]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"seeds_data['seed_2']=seeds_data['Seed'].apply(extra)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"seeds_T1 = seeds_data[['Season','TeamID','seed','seed_2']].copy()\nseeds_T2 = seeds_data[['Season','TeamID','seed','seed_2']].copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"seeds_T1.columns = ['Season','T1_TeamID','T1_seed','T1_seed_2']\nseeds_T2.columns = ['Season','T2_TeamID','T2_seed','T2_seed_2']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"seeds_T2.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tourney_data = pd.merge(tourney_data, seeds_T1, on = ['Season', 'T1_TeamID'], how = 'left')\ntourney_data = pd.merge(tourney_data, seeds_T2, on = ['Season', 'T2_TeamID'], how = 'left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tourney_data[\"Seed_diff\"] = tourney_data[\"T1_seed\"].astype(int) - tourney_data[\"T2_seed\"].astype(int)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tourney_data.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tourney_data.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"features=[col for col in tourney_data.columns.values if col not in ['Season', 'DayNum', 'T1_TeamID', 'T2_TeamID','Result','T1_Score','T2_Score']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(features)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = tourney_data['Result']\nX = tourney_data[features].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import xgboost","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dtrain=xgboost.DMatrix(X,label=y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def cauchyobj(preds, dtrain):\n    labels = dtrain.get_label()\n    c = 5000 \n    x =  preds-labels    \n    grad = x / (x**2/c**2+1)\n    hess = -c**2*(x**2-c**2)/(x**2+c**2)**2\n    return grad, hess","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"param = {} \n#param['objective'] = 'reg:linear'\nparam['eval_metric'] =  'mae'\nparam['booster'] = 'gbtree'\nparam['eta'] = 0.05 #change to ~0.02 for final run\nparam['subsample'] = 0.35\nparam['colsample_bytree'] = 0.7\nparam['num_parallel_tree'] = 3 #recommend 10\nparam['min_child_weight'] = 40\nparam['gamma'] = 10\nparam['max_depth'] =  3\nparam['silent'] = 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import KFold","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xgb_cv = []\nrepeat_cv = 10 # recommend 10\n\nfor i in range(repeat_cv): \n    print(f\"Fold repeater {i}\")\n    xgb_cv.append(\n        xgboost.cv(\n          params = param,\n          dtrain = dtrain,\n          obj = cauchyobj,\n          num_boost_round = 3000,\n          folds = KFold(n_splits = 5, shuffle = True, random_state = i),\n          early_stopping_rounds = 25,\n          verbose_eval = 50\n        )\n    )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"iteration_counts = [np.argmin(x['test-mae-mean'].values) for x in xgb_cv]\nval_mae = [np.min(x['test-mae-mean'].values) for x in xgb_cv]\niteration_counts, val_mae","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"oof_preds = []\nfor i in range(10):\n    print(f\"Fold repeater {i}\")\n    preds = y.copy()\n    kfold = KFold(n_splits = 5, shuffle = True, random_state = i+100)    \n    for train_index, val_index in kfold.split(X,y):\n \n        dtrain_i = xgboost.DMatrix(X[train_index], label = y[train_index])\n        dval_i = xgboost.DMatrix(X[val_index], label = y[val_index])  \n        model = xgboost.train(\n              params = param,\n              dtrain = dtrain_i,\n              num_boost_round = iteration_counts[i],\n              verbose_eval = 50\n        )\n        preds[val_index] = model.predict(dval_i)\n    oof_preds.append(preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"oof_preds[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub=pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub['Season'] = sub['ID'].map(lambda x: int(x[:4]))\nsub[\"T1_TeamID\"] = sub[\"ID\"].apply(lambda x: x[5:9]).astype(int)\nsub[\"T2_TeamID\"] = sub[\"ID\"].apply(lambda x: x[10:14]).astype(int)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nsub = pd.merge(sub, stat_seasons_T1, on = ['Season', 'T1_TeamID'])\nsub = pd.merge(sub, stat_seasons_T2, on = ['Season', 'T2_TeamID'])\nsub = pd.merge(sub, seeds_T1, on = ['Season', 'T1_TeamID'])\nsub = pd.merge(sub, seeds_T2, on = ['Season', 'T2_TeamID'])\nsub[\"Seed_diff\"] = sub[\"T1_seed\"] - sub[\"T2_seed\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Xsub=sub[features].values\ndtest = xgboost.DMatrix(Xsub)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dtest = xgboost.DMatrix(Xsub)\nsub_models = []\nfor i in range(repeat_cv):\n    print(f\"Fold repeater {i}\")\n    sub_models.append(\n        xgboost.train(\n          params = param,\n          dtrain = dtrain,\n          num_boost_round = int(iteration_counts[i] * 1.05),\n          verbose_eval = 50\n        )\n    )\n    \nsub_preds = []\nfor i in range(repeat_cv):\n    sub_preds.append(sub_models[i].predict(dtest))\n    \nsub[\"Pred\"] = pd.DataFrame(sub_preds).mean(axis=0)\nsub[['ID','Pred']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_preds = []\nfor i in range(repeat_cv):\n    sub_preds.append(sub_models[i].predict(dtest))\n    \nsub[\"Pred\"] = pd.DataFrame(sub_preds).mean(axis=0)\nsub[['ID','Pred']].to_csv(\"submission.csv\", index = False)\ntourney_data.to_csv('tourney_data.csv',index=False)","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}