{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr = pd.read_csv('../input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyCompactResults.csv')\nts = pd.read_csv('../input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneySeeds.csv')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ts","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr = tr.drop(['DayNum', 'WScore', 'LScore', 'WLoc', 'NumOT'], axis=1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr = pd.merge(tr, ts, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')\ntr.rename(columns={'Seed':'WSeed'}, inplace=True)\n\ntr = tr.drop('TeamID', axis=1)\n\ntr = pd.merge(tr, ts, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')\ntr.rename(columns={'Seed':'LSeed'}, inplace=True)\n\ntr = tr.drop('TeamID', axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_seed(x):\n    return int(x[1:3])\n\ntr['WSeed'] = tr['WSeed'].map(lambda x: get_seed(x))\ntr['LSeed'] = tr['LSeed'].map(lambda x: get_seed(x))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sr = pd.read_csv('../input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MRegularSeasonCompactResults.csv')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"swr = sr[['Season', 'WTeamID', 'WScore']]\nslr = sr[['Season', 'LTeamID', 'LScore']]\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"swr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"slr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"swr.rename(columns={'WTeamID':'TeamID', 'WScore':'Score'}, inplace=True)\nslr.rename(columns={'LTeamID':'TeamID', 'LScore':'Score'}, inplace=True)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"slr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"swr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sr = pd.concat((swr, slr)).reset_index(drop=True)\nss = sr.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ss","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr = pd.merge(tr, ss, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')\ntr.rename(columns={'Score':'WScoreT'}, inplace=True)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr = tr.drop('TeamID', axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr = pd.merge(tr, ss, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')\ntr.rename(columns={'Score':'LScoreT'}, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr = tr.drop('TeamID', axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"twr = tr.drop(['Season', 'WTeamID', 'LTeamID'], axis=1)\ntwr.rename(columns={'WSeed':'Seed1', 'LSeed':'Seed2', 'WScoreT':'ScoreT1', 'LScoreT':'ScoreT2'}, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"twr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tlr = twr.copy()\ntlr['Seed1'] = twr['Seed2']\ntlr['Seed2'] = twr['Seed1']\ntlr['ScoreT1'] = twr['ScoreT2']\ntlr['ScoreT2'] = twr['ScoreT1']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"twr['Seed_diff'] = twr['Seed1'] - twr['Seed2']\ntwr['ScoreT_diff'] = twr['ScoreT1'] - twr['ScoreT2']\ntlr['Seed_diff'] = tlr['Seed1'] - tlr['Seed2']\ntlr['ScoreT_diff'] = tlr['ScoreT1'] - tlr['ScoreT2']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"twr['result'] = 1\ntlr['result'] = 0\ntrain_df = pd.concat((twr, tlr)).reset_index(drop=True)\ntrain_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.read_csv('../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":"test_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['Season'] = test_df['ID'].map(lambda x: int(x[:4]))\ntest_df['WTeamID'] = test_df['ID'].map(lambda x: int(x[5:9]))\ntest_df['LTeamID'] = test_df['ID'].map(lambda x: int(x[10:14]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.merge(test_df, ts, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')\ntest_df.rename(columns={'Seed':'Seed1'}, inplace=True)\ntest_df = test_df.drop('TeamID', axis=1)\ntest_df = pd.merge(test_df, ts, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')\ntest_df.rename(columns={'Seed':'Seed2'}, inplace=True)\ntest_df = test_df.drop('TeamID', axis=1)\ntest_df = pd.merge(test_df, ss, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')\ntest_df.rename(columns={'Score':'ScoreT1'}, inplace=True)\ntest_df = test_df.drop('TeamID', axis=1)\ntest_df = pd.merge(test_df, ss, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')\ntest_df.rename(columns={'Score':'ScoreT2'}, inplace=True)\ntest_df = test_df.drop('TeamID', axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['Seed1'] = test_df['Seed1'].map(lambda x: get_seed(x))\ntest_df['Seed2'] = test_df['Seed2'].map(lambda x: get_seed(x))\ntest_df['Seed_diff'] = test_df['Seed1'] - test_df['Seed2']\ntest_df['ScoreT_diff'] = test_df['ScoreT1'] - test_df['ScoreT2']\ntest_df = test_df.drop(['ID', 'Pred', 'Season', 'WTeamID', 'LTeamID'], axis=1)\ntest_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = train_df.drop('result', axis=1)\ny = train_df.result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.read_csv('../input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')\nMNCAATourneyCompactResults = pd.read_csv('../input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyCompactResults.csv')\nsub = pd.concat([sub, sub['ID'].str.split('_', expand=True).rename(columns={0: 'Season', 1: 'Team1', 2: 'Team2'}).astype(np.int64)], axis=1)\nmerge = pd.merge(sub, MNCAATourneyCompactResults[['Season', 'WTeamID', 'LTeamID']], how='left', left_on=['Season', 'Team1', 'Team2'], right_on=['Season', 'WTeamID', 'LTeamID'])\nsub.loc[~merge['WTeamID'].isnull(), 'Pred'] = 1\nmerge = pd.merge(sub, MNCAATourneyCompactResults[['Season', 'WTeamID', 'LTeamID']], how='left', left_on=['Season', 'Team2', 'Team1'], right_on=['Season', 'WTeamID', 'LTeamID'])\nsub.loc[~merge['WTeamID'].isnull(), 'Pred'] = 0\nsub = sub.drop(['Season', 'Team1', 'Team2'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor,AdaBoostRegressor,GradientBoostingRegressor\n\nmodel = GradientBoostingRegressor(loss='ls',\n    learning_rate=0.1,\n    n_estimators=4556,\n    subsample=1.0,\n    criterion='friedman_mse',\n    min_samples_split=2,\n    min_samples_leaf=1,\n    min_weight_fraction_leaf=0.0,\n    max_depth=3,\n    min_impurity_decrease=0.0,\n    min_impurity_split=None,\n    init=None,\n    random_state=None,\n    max_features=None,\n    alpha=0.9,\n    verbose=0,\n    max_leaf_nodes=None,\n    warm_start=False,\n    presort='deprecated',\n    validation_fraction=0.1,\n    n_iter_no_change=None,\n    tol=0.0001,\n    ccp_alpha=0.0)\n\nmodel.fit(X,y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = model.predict(test_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub['Pred'] = y_pred\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor,AdaBoostRegressor,GradientBoostingRegressor\n\nmodel = GradientBoostingRegressor(loss='ls',\n    learning_rate=0.1,\n    n_estimators=25000,\n    subsample=1.0,\n    criterion='friedman_mse',\n    min_samples_split=2,\n    min_samples_leaf=1,\n    min_weight_fraction_leaf=0.0,\n    max_depth=10,\n    min_impurity_decrease=0.0,\n    min_impurity_split=None,\n    init=None,\n    random_state=None,\n    max_features=None,\n    alpha=0.9,\n    verbose=0,\n    max_leaf_nodes=None,\n    warm_start=False,\n    presort='deprecated',\n    validation_fraction=0.1,\n    n_iter_no_change=None,\n    tol=0.0001,\n    ccp_alpha=0.0)\n\nmodel.fit(X,y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = model.predict(test_df)\n\nsub['Pred'] = y_pred\nsub.head()\n\nsub.to_csv('Results.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from xgboost import XGBRegressor,XGBRFRegressor,XGBModel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = XGBRegressor()\nmodel.fit(X,y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = model.predict(test_df)\n\nsub['Pred'] = y_pred\nsub.head()\n\nsub.to_csv('XGBoost.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = XGBRFRegressor(learning_rate=1,\n    subsample=0.8,\n    colsample_bynode=0.8,\n    reg_lambda=1e-05)\nmodel.fit(X,y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = model.predict(test_df)\n\nsub['Pred'] = y_pred\nsub.head()\n\nsub.to_csv('xgbrf.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from lightgbm import LGBMRegressor","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = LGBMRegressor(boosting_type='gbdt',\n    num_leaves=31,\n    max_depth=-1,\n    learning_rate=0.1,\n    n_estimators=100000,\n    subsample_for_bin=200000,\n    objective=None,\n    class_weight=None,\n    min_split_gain=0.0,\n    min_child_weight=0.001,\n    min_child_samples=20,\n    subsample=1.0,\n    subsample_freq=0,\n    colsample_bytree=1.0,\n    reg_alpha=0.0,\n    reg_lambda=0.0,\n    random_state=None,\n    n_jobs=-1,\n    silent=True,\n    importance_type='split')\nmodel.fit(X,y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = model.predict(test_df)\n\nsub['Pred'] = y_pred\nsub.head()\n\nsub.to_csv('FinalSubmission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.ensemble import BaggingRegressor","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = BaggingRegressor(base_estimator=None,\n    n_estimators=100000,\n    max_samples=1.0,\n    max_features=1.0,\n    bootstrap=True,\n    bootstrap_features=False,\n    oob_score=False,\n    warm_start=False,\n    n_jobs=None,\n    random_state=None,\n    verbose=0)\nmodel.fit(X,y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred = model.predict(test_df)\n\nsub['Pred'] = y_pred\nsub.head()\n\nsub.to_csv('Bagging.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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.4"}},"nbformat":4,"nbformat_minor":4}