Kaggle competition: Google Cloud & NCAA® ML Competition 2020-NCAAM
my_model_2 = XGBRegressor(n_estimators=300000, learning_rate=1.2)
 my_model_2.fit(X, y,
early_stopping_rounds = 5,
eval_set = [(X_valid, y_valid)],
verbose=False)
 predictions_2 = my_model_2.predict(X_test)
<save_to_csv>
tourney_result = pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyDetailedResults.csv')
 tourney_seed = pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneySeeds.csv')
 season_result = pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MRegularSeasonDetailedResults.csv')
 test_df = pd.read_csv('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv' )
output = pd.DataFrame({'PassengerId': X_test.index,
'Survived': predictions_2})
 output.to_csv('submission_final_XGBoost.csv', index=False )<set_options>
season_win_result = season_result[['Season', 'WTeamID', 'WScore', 'WFGM', 'WFGA', 'WFGM3', 'WFGA3', 'WFTM', 'WFTA', 'WOR', 'WDR',
'WAst', 'WTO', 'WStl', 'WBlk', 'WPF']]
season_lose_result = season_result[['Season', 'LTeamID', 'LScore', 'LFGM', 'LFGA', 'LFGM3', 'LFGA3', 'LFTM', 'LFTA', 'LOR', 'LDR',
'LAst', 'LTO', 'LStl', 'LBlk', 'LPF']]
season_win_result.rename(columns={'WTeamID':'TeamID', 'WScore':'Score', 'WFGM':'FGM', 'WFGA':'FGA', 'WFGM3':'FGM3', 'WFGA3':'FGA3',
'WFTM':'FTM', 'WFTA':'FTA', 'WOR':'OR', 'WDR':'DR', 'WAst':'Ast', 'WTO':'TO', 'WStl':'Stl',
'WBlk':'Blk', 'WPF':'PF'}, inplace=True)
 season_lose_result.rename(columns={'LTeamID':'TeamID', 'LScore':'Score', 'LFGM':'FGM', 'LFGA':'FGA', 'LFGM3':'FGM3', 'LFGA3':'FGA3',
'LFTM':'FTM', 'LFTA':'FTA', 'LOR':'OR', 'LDR':'DR', 'LAst':'Ast', 'LTO':'TO', 'LStl':'Stl',
'LBlk':'Blk', 'LPF':'PF'}, inplace=True)
 season_result = pd.concat(( season_win_result, season_lose_result)).reset_index(drop=True )
%matplotlib inline<load_from_csv>
tourney_result['Score_difference'] = tourney_result['WScore'] - tourney_result['LScore']
tourney_result = tourney_result[['Season', 'WTeamID', 'LTeamID', 'Score_difference']]
tourney_result = pd.merge(tourney_result, tourney_seed, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Seed':'WSeed'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result = pd.merge(tourney_result, tourney_seed, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Seed':'LSeed'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result['WSeed'] = tourney_result['WSeed'].apply(lambda x: int(x[1:3]))
tourney_result['LSeed'] = tourney_result['LSeed'].apply(lambda x: int(x[1:3]))
print(tourney_result.info(null_counts=True))
train_data = pd.read_csv('.. /input/titanic/train.csv')
 train_data.head(10 )<load_from_csv>
test_df['Season'] = test_df['ID'].map(lambda x: int(x[:4]))
test_df['WTeamID'] = test_df['ID'].map(lambda x: int(x[5:9]))
test_df['LTeamID'] = test_df['ID'].map(lambda x: int(x[10:14]))
test_df = pd.merge(test_df, tourney_seed, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Seed':'Seed1'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, tourney_seed, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Seed':'Seed2'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1 )
test_data = pd.read_csv('.. /input/titanic/test.csv')
 test_data.head(10 )<count_missing_values>
for col in season_result.columns[2:]:
season_result_map_mean = season_result.groupby(['Season', 'TeamID'])[col].mean().reset_index()
tourney_result = pd.merge(tourney_result, season_result_map_mean, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={f'{col}':f'W{col}MeanT'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result = pd.merge(tourney_result, season_result_map_mean, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={f'{col}':f'L{col}MeanT'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, season_result_map_mean, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={f'{col}':f'W{col}MeanT'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, season_result_map_mean, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={f'{col}':f'L{col}MeanT'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1 )
train_data.isnull().sum()<count_missing_values>
tourney_win_result = tourney_result.drop(['WTeamID', 'LTeamID'], axis=1)
 for col in tourney_win_result.columns[2:]:
if col[0] == 'W':
tourney_win_result.rename(columns={f'{col}':f'{col[1:]+"1"}'}, inplace=True)
 elif col[0] == 'L':
tourney_win_result.rename(columns={f'{col}':f'{col[1:]+"2"}'}, inplace=True)
 tourney_lose_result = tourney_win_result.copy()
for col in tourney_lose_result.columns:
if col[-1] == '1':
col2 = col[:-1] + '2'
tourney_lose_result[col] = tourney_win_result[col2]
tourney_lose_result[col2] = tourney_win_result[col]
tourney_lose_result.columns
test_data.isnull().sum()<feature_engineering>
tourney_win_result['Seed_diff'] = tourney_win_result['Seed1'] - tourney_win_result['Seed2']
tourney_win_result['ScoreMeanT_diff'] = tourney_win_result['ScoreMeanT1'] - tourney_win_result['ScoreMeanT2']
tourney_lose_result['Seed_diff'] = tourney_lose_result['Seed1'] - tourney_lose_result['Seed2']
tourney_lose_result['ScoreMeanT_diff'] = tourney_lose_result['ScoreMeanT1'] - tourney_lose_result['ScoreMeanT2']
tourney_lose_result['Score_difference'] = -tourney_lose_result['Score_difference']
tourney_win_result['result'] = 1
tourney_lose_result['result'] = 0
tourney_result = pd.concat(( tourney_win_result, tourney_lose_result)).reset_index(drop=True )
train_data['Age'] = train_data['Age'].fillna(train_data['Age'].mean())
 test_data['Age'] = test_data['Age'].fillna(test_data['Age'].mean() )<count_missing_values>
for col in test_df.columns[2:]:
if col[0] == 'W':
test_df.rename(columns={f'{col}':f'{col[1:]+"1"}'}, inplace=True)
 elif col[0] == 'L':
test_df.rename(columns={f'{col}':f'{col[1:]+"2"}'}, inplace=True)
 test_df['Seed1'] = test_df['Seed1'].apply(lambda x: int(x[1:3]))
test_df['Seed2'] = test_df['Seed2'].apply(lambda x: int(x[1:3]))
test_df['Seed_diff'] = test_df['Seed1'] - test_df['Seed2']
test_df['ScoreMeanT_diff'] = test_df['ScoreMeanT1'] - test_df['ScoreMeanT2']
test_df = test_df.drop(['ID', 'Pred', 'Season'], axis=1 )
train_data.isnull().sum()<count_missing_values>
features = [x for x in tourney_result.columns if x not in ['result', 'Score_difference', 'Season']]
params = {'num_leaves': 400,
'min_child_weight': 0.034,
'feature_fraction': 0.379,
'bagging_fraction': 0.418,
'min_data_in_leaf': 106,
'max_depth': -1,
'learning_rate': 0.0068,
"boosting_type": "gbdt",
"bagging_seed": 11,
'reg_alpha': 0.3899,
'reg_lambda': 0.648,
'random_state': 47,
}
step_size = 20
steps = 250
boosting_rounds = [step_size*(x+1)for x in range(steps)]
def run_boost_round_test(boosting_rounds, step_size):
training_scores, oof_scores, holdback_scores = [], [], []
model = NCAA_model(params, tourney_result, test_df, use_holdback=[2019], regression=False, verbose=False)
 print(f'Training for {step_size*steps} rounds.')
 for rounds in range(step_size,boosting_rounds+1,step_size):
print(f'{"*"*50}')
 print(f'Rounds: {rounds}')
 if model.use_holdback:
tr_score, oof_score, hb_score = model.train(features, n_splits=10, n_boost_round=step_size, early_stopping_rounds=None)
 else:
tr_score, oof_score = model.train(features, n_splits=10, n_boost_round=step_size, early_stopping_rounds=None)
 clips, clip_s = model.fit_clipper(verbose=True)
 spline, spline_s = model.fit_spline_model(verbose=True)
 training_scores.append([tr_score, model.postprocess_preds(clips, use_data = 'train'),
model.postprocess_preds(spline, use_data = 'train', method='spline')])
 oof_scores.append([oof_score, model.postprocess_preds(clips, use_data = 'oof'),
model.postprocess_preds(spline, use_data = 'oof', method='spline')])
 holdback_scores.append([hb_score, model.postprocess_preds(clips, use_data = 'hb'),
model.postprocess_preds(spline, use_data = 'hb', method='spline')])
 return training_scores, oof_scores, holdback_scores, model, clips, spline
training_scores, oof_scores, holdback_scores, model, clips, spline = run_boost_round_test(boosting_rounds[-1], step_size )
<feature_engineering><EOS> 
y_preds = model.postprocess_preds(spline, method='spline')
 submission_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 submission_df['Pred'] = y_preds
submission_df.to_csv('submission.csv', index=False)
 submission_df.describe()