{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport eli5\n\nfrom sklearn.linear_model import LogisticRegression, LinearRegression\nfrom sklearn.utils import shuffle\nfrom sklearn.model_selection import GridSearchCV, KFold, train_test_split\nfrom sklearn import preprocessing\nfrom sklearn.metrics import confusion_matrix\nimport lightgbm as lgb\nimport xgboost as xgb\n\nimport gc\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"men_folder_path = '../input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/'\nwomen_folder_path = '../kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-womens-tournament/'\nMstage1_folder_path = 'MDataFiles_Stage1/'\nWstage1_folder_path = 'WDataFiles_Stage1/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mfolder_path = men_folder_path\nMnacc_section1 = pd.read_csv(Mfolder_path+Mstage1_folder_path+'M'+'NCAATourneyCompactResults.csv')\nMseed_section1 = pd.read_csv(Mfolder_path+Mstage1_folder_path+'M'+'NCAATourneySeeds.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mseed_section1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1 = Mnacc_section1.drop(['WScore', 'LScore', 'WLoc', 'NumOT'], axis = 1)\nMnacc_section1 = Mnacc_section1.merge(Mseed_section1,left_on = ['Season', 'WTeamID'], right_on = ['Season', 'TeamID'], how='left').drop('TeamID',axis = 1)\nMnacc_section1 = Mnacc_section1.merge(Mseed_section1,left_on = ['Season', 'LTeamID'], right_on = ['Season', 'TeamID'], suffixes=('_W','_L'),how='left').drop('TeamID',axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1['Region_W'] = Mnacc_section1['Seed_W'].map(lambda x: 'Region' + str(x[0]))\nMnacc_section1['Region_L'] = Mnacc_section1['Seed_L'].map(lambda x: 'Region' + str(x[0]))\nMnacc_section1['Seed_W'] = Mnacc_section1['Seed_W'].map(lambda x: int(x[1:3]))\nMnacc_section1['Seed_L'] = Mnacc_section1['Seed_L'].map(lambda x: int(x[1:3]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mseason_section1 = pd.read_csv(Mfolder_path+Mstage1_folder_path+'M'+'Seasons.csv')\nMseason_section1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1 = Mnacc_section1.merge(Mseason_section1, left_on=['Season'], right_on=['Season']).drop(['DayZero'], axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1['Region_W'] = Mnacc_section1.apply(lambda row : row[row['Region_W']], axis = 1)\nMnacc_section1['Region_L'] = Mnacc_section1.apply(lambda row : row[row['Region_L']], axis = 1)\nMnacc_section1 = Mnacc_section1.drop(['RegionW','RegionX', 'RegionY', 'RegionZ'], axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1.loc[:,'Region_W'].value_counts().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Mnacc_section1.query('DayNum == 154').groupby('Region_W')['Season'].count()\nteam2_W = Mnacc_section1.loc[(Mnacc_section1['DayNum'] == 154), 'Region_W'].value_counts()\nteam2_L = Mnacc_section1.loc[(Mnacc_section1['DayNum'] == 154), 'Region_L'].value_counts()\nMnacc_section1.loc[:,'Region_W'] = Mnacc_section1.loc[:,'Region_W'].map(team2_W)\nMnacc_section1.loc[:,'Region_L'] = Mnacc_section1.loc[:,'Region_L'].map(team2_L)\n#np.where(Mnacc_section1.loc[:,'Region_W'].isnull())\nMnacc_section1 = Mnacc_section1.replace(np.nan,0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1.Region_W.value_counts().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#每个球队每个赛季常规赛的总得分\nMregular_section1 = pd.read_csv(Mfolder_path+Mstage1_folder_path+'M'+'RegularSeasonCompactResults.csv')\nMregular_section1_win = Mregular_section1[['Season', 'WTeamID', 'WScore']]\nMregular_section1_lose = Mregular_section1[['Season', 'LTeamID', 'LScore']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mregular_section1_win.rename(columns={'WTeamID':'TeamID', 'WScore':'Score'}, inplace=True)\nMregular_section1_lose.rename(columns={'LTeamID':'TeamID', 'LScore':'Score'}, inplace=True)\nMregular_section1 = pd.concat((Mregular_section1_win, Mregular_section1_lose)).reset_index(drop=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mseason_score = Mregular_section1.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mregular_section1.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mregular_section1_win.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mregular_section1_lose.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mseason_score = Mregular_section1.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()\nMseason_score_win = Mregular_section1_win.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()\nMseason_score_lose = Mregular_section1_lose.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()\nMseason_score_win.rename(columns={'Score': 'Score_W'}, inplace=True)\nMseason_score_lose.rename(columns={'Score': 'Score_L'}, inplace=True)\nMseason_score = Mseason_score.merge(Mseason_score_win, left_on=['Season', 'TeamID'], right_on=['Season', 'TeamID'], how = 'outer')\nMseason_score = Mseason_score.merge(Mseason_score_lose, left_on=['Season', 'TeamID'], right_on=['Season', 'TeamID'], how = 'outer')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mseason_score = Mseason_score.replace(np.nan, 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mseason_count  = Mregular_section1.groupby(['Season', 'TeamID']).count().reset_index()\nMseason_count_win  = Mregular_section1_win.groupby(['Season', 'TeamID']).count().reset_index()\nMseason_count_lose = Mregular_section1_lose.groupby(['Season', 'TeamID']).count().reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mseason_count.rename(columns={'Score': 'Count'}, inplace=True)\nMseason_count_win.rename(columns={'Score': 'Count_W'}, inplace=True)\nMseason_count_lose.rename(columns={'Score': 'Count_L'}, inplace=True)\nMseason_count = Mseason_count.merge(Mseason_count_win, left_on=['Season', 'TeamID'], right_on=['Season', 'TeamID'], how = 'outer')\nMseason_count = Mseason_count.merge(Mseason_count_lose, left_on=['Season', 'TeamID'], right_on=['Season', 'TeamID'], how = 'outer')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mseason_count = Mseason_count.replace(np.nan, 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1 = Mnacc_section1.merge(Mseason_score, left_on = ['Season', 'WTeamID'], right_on = ['Season', 'TeamID'], how='left').drop('TeamID',axis = 1)\nMnacc_section1 = Mnacc_section1.merge(Mseason_score, left_on = ['Season', 'LTeamID'], right_on = ['Season', 'TeamID'], suffixes=('_W','_L'), how='left').drop('TeamID',axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Mnacc_section1 = Mnacc_section1.merge(Mseason_count, left_on = ['Season', 'WTeamID'], right_on = ['Season', 'TeamID'], how='left').drop('TeamID',axis = 1)\nMnacc_section1 = Mnacc_section1.merge(Mseason_count, left_on = ['Season', 'LTeamID'], right_on = ['Season', 'TeamID'], suffixes=('_W','_L'), how='left').drop('TeamID',axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#构造result为1的训练集\ntrain_win = Mnacc_section1.copy()\ntrain_win = train_win.drop(['Season', 'WTeamID', 'LTeamID', 'DayNum'], axis=1)\ntrain_win.rename(columns={'Seed_W':'Seed1', 'Seed_L':'Seed2', 'Score_W':'Score1', 'Score_L':'Score2', 'Region_W': 'Region1', 'Region_L': 'Region2', 'Count_W': 'Count1', 'Count_L': 'Count2'}, inplace=True)\ntrain_win['result'] = 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Score_W_W  意味着第一支队伍赢球场次的总分\n#Score_L_W  意味着第一支队伍输球场次的总分\ntrain_lose = train_win.copy()\ntrain_lose['Seed1'] = train_win['Seed2']\ntrain_lose['Seed2'] = train_win['Seed1']\ntrain_lose['Score1'] = train_win['Score2']\ntrain_lose['Score2'] = train_win['Score1']\ntrain_lose['Region1'] = train_win['Region2']\ntrain_lose['Region2'] = train_win['Region1']\ntrain_lose['Score_W_L'] = train_win['Score_W_W']\ntrain_lose['Score_L_L'] = train_win['Score_L_W']\ntrain_lose['Score_W_W'] = train_win['Score_W_L']\ntrain_lose['Score_L_W'] = train_win['Score_L_L']\ntrain_lose['Count_W_L'] = train_win['Count_W_W']\ntrain_lose['Count_W_W'] = train_win['Count_W_L']\ntrain_lose['Count_L_L'] = train_win['Count_L_W']\ntrain_lose['Count_L_W'] = train_win['Count_L_L']\ntrain_lose['result'] = 0\ntrain_lose.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.concat((train_win,train_lose)).reset_index(drop = True)\ntrain_df['Seed_diff'] = train_df['Seed1'] - train_df['Seed2'] \ntrain_df['Score_diff'] = train_df['Score1'] - train_df['Score2'] \ntrain_df['Count_diff'] = train_df['Count1'] - train_df['Count2'] \ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Test"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.read_csv(Mfolder_path+'M'+'SampleSubmissionStage1_2020.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['ID'][0]","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 = test_df.merge(Mseed_section1,left_on = ['Season', 'WTeamID'], right_on = ['Season', 'TeamID'], how='left').drop('TeamID',axis = 1)\ntest_df = test_df.merge(Mseed_section1,left_on = ['Season', 'LTeamID'], right_on = ['Season', 'TeamID'], suffixes=('_W','_L'),how='left').drop('TeamID',axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['Region_W'] = test_df['Seed_W'].map(lambda x: 'Region' + str(x[0]))\ntest_df['Region_L'] = test_df['Seed_L'].map(lambda x: 'Region' + str(x[0]))\ntest_df['Seed_W'] = test_df['Seed_W'].map(lambda x: int(x[1:3]))\ntest_df['Seed_L'] = test_df['Seed_L'].map(lambda x: int(x[1:3]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = test_df.merge(Mseason_section1, left_on=['Season'], right_on=['Season']).drop(['DayZero'], axis = 1)\ntest_df['Region_W'] = test_df.apply(lambda row : row[row['Region_W']], axis = 1)\ntest_df['Region_L'] = test_df.apply(lambda row : row[row['Region_L']], axis = 1)\ntest_df = test_df.drop(['RegionW','RegionX', 'RegionY', 'RegionZ'], axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.loc[:,'Region_W'] = test_df.loc[:,'Region_W'].map(team2_W)\ntest_df.loc[:,'Region_L'] = test_df.loc[:,'Region_L'].map(team2_L)\ntest_df = test_df.replace(np.nan,0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = test_df.merge(Mseason_score, left_on = ['Season', 'WTeamID'], right_on = ['Season', 'TeamID'], how='left').drop('TeamID',axis = 1)\ntest_df = test_df.merge(Mseason_score, left_on = ['Season', 'LTeamID'], right_on = ['Season', 'TeamID'], suffixes=('_W','_L'), how='left').drop('TeamID',axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = test_df.merge(Mseason_count, left_on = ['Season', 'WTeamID'], right_on = ['Season', 'TeamID'], how='left').drop('TeamID',axis = 1)\ntest_df = test_df.merge(Mseason_count, left_on = ['Season', 'LTeamID'], right_on = ['Season', 'TeamID'], suffixes=('_W','_L'), how='left').drop('TeamID',axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.rename(columns={'Seed_W':'Seed1', 'Seed_L':'Seed2', 'Score_W':'Score1', 'Score_L':'Score2', 'Count_W': 'Count1', 'Count_L': 'Count2','Region_W':'Region1', 'Region_L':'Region2'}, inplace=True)\ntest_df['Seed_diff'] = test_df['Seed1'] - test_df['Seed2'] \ntest_df['Score_diff'] = test_df['Score1'] - test_df['Score2'] \ntest_df['Count_diff'] = test_df['Count1'] - test_df['Count2'] \n#test_df = test_df.drop(['Score1', 'Score2', 'Count1', 'Count2'], axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = test_df.drop(['ID', 'Pred', 'Season', 'WTeamID', 'LTeamID'], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.columns","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#  Model"},{"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":"params_lgb = {'num_leaves': 70,\n          'min_child_weight': 0.034,\n          'feature_fraction': 0.379,\n          'bagging_fraction': 0.418,\n          'min_data_in_leaf': 106,\n          'objective': 'binary',\n          'max_depth': -1,\n          'learning_rate': 0.0068,\n          \"boosting_type\": \"gbdt\",\n          \"bagging_seed\": 11,\n          \"metric\": 'logloss',\n          \"verbosity\": -1,\n          'reg_alpha': 0.3899,\n          'reg_lambda': 0.648,\n          'random_state': 47,\n         }","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"NFOLDS = 10\nfolds = KFold(n_splits=NFOLDS)\n\ncolumns = X.columns\nsplits = folds.split(X, y)\ny_preds_lgb = np.zeros(test_df.shape[0])\ny_train_lgb = np.zeros(X.shape[0])\ny_oof = np.zeros(X.shape[0])\n\nfeature_importances = pd.DataFrame()\nfeature_importances['feature'] = columns\n  \nfor fold_n, (train_index, valid_index) in enumerate(splits):\n    print('Fold:',fold_n+1)\n    X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]\n    y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]\n    \n    dtrain = lgb.Dataset(X_train, label=y_train)\n    dvalid = lgb.Dataset(X_valid, label=y_valid)\n\n    clf = lgb.train(params_lgb, dtrain, 10000, valid_sets = [dtrain, dvalid], verbose_eval=200)\n    \n    feature_importances[f'fold_{fold_n + 1}'] = clf.feature_importance()\n    \n    y_pred_valid = clf.predict(X_valid)\n    y_oof[valid_index] = y_pred_valid\n    \n    y_train_lgb += clf.predict(X) / NFOLDS\n    y_preds_lgb += clf.predict(test_df) / NFOLDS\n    \n    del X_train, X_valid, y_train, y_valid\n    gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = pd.read_csv('../input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')\nsubmission_df['Pred'] = y_preds_lgb\nsubmission_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df['Pred'].hist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","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}