Kaggle competition: Google Cloud & NCAA® ML Competition 2020-NCAAM
<SOS> metric: logloss Kaggle data source: google-cloud-ncaa-march-madness-2020-division-1-mens-tournament<define_variables>
warnings.filterwarnings("ignore")
prediction.index = range(0,len(prediction))<load_from_csv>
result = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyCompactResults.csv')
 result = result.drop(columns=['WLoc', 'NumOT', 'DayNum'])
 result.head()
sub_format = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/submission.csv' )<drop_column>
seeds = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneySeeds.csv')
 seeds.Seed = seeds.Seed.map(lambda string : int(string[1:3]))
seeds.head()
sub_format = sub_format['ForecastId']<concatenate>
team_name = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MTeamSpellings.csv',
encoding='cp1252')
 team_name = team_name.drop_duplicates(subset=['TeamID'], keep='last' ).reset_index(drop=True)
 team_name.TeamNameSpelling = team_name.TeamNameSpelling.map(lambda string : string.upper())
 team_name.head()
final = pd.concat([sub_format,prediction],axis=1 )<save_to_csv>
Wname = team_name.rename(columns={'TeamNameSpelling':'Wteam_name', 'TeamID':'WTeamID'})
 team_result = pd.merge(left=result, right=Wname, how='left', on=['WTeamID'])
 win_by_year = team_result.groupby(['Season', 'Wteam_name'])\
['WScore'].count().reset_index() \ 
.rename(columns={'Wteam_name':'Team', 'WScore':'Win_matches'})
 df = win_by_year[win_by_year.Win_matches == win_by_year.Win_matches.max() ].drop(columns=['Win_matches'] ).set_index('Season')
 df
final.to_csv('submission.csv',index=False )<set_options>
win_by_year = win_by_year.groupby(['Team', 'Season'] ).Win_matches.sum() \ 
.unstack(fill_value=0 ).cumsum(axis=1 ).sort_values(by=2019, ascending=False ).head(15)
 cum_win = pd.melt(win_by_year, value_vars=win_by_year.columns, value_name="Win_matches")
 cum_win['Team'] = list(win_by_year.index)*35
cum_win = cum_win[['Season', 'Team', 'Win_matches']]
pd.set_option('display.max.columns',3000 )<load_from_csv>
Wseeds = seeds.rename(columns={'TeamID':'WTeamID', 'Seed':'WSeed'})
 Lseeds = seeds.rename(columns={'TeamID':'LTeamID', 'Seed':'LSeed'})
 data = pd.merge(left=result, right=Wseeds, how='left', on=['Season', 'WTeamID'])
 data = pd.merge(left=data, right=Lseeds, on=['Season', 'LTeamID'])
 data.head()
train=pd.read_csv('/kaggle/input/knit-hacks/train.csv')
 test=pd.read_csv('/kaggle/input/knit-hacks/test.csv' )<count_values>
scores = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MRegularSeasonCompactResults.csv')
 scores.head()
train['Col2'].value_counts() /train.shape[0]<count_missing_values>
season_score = result_scores.groupby(['Season', 'TeamID'])['Score'].sum()
season_score.sort_values(ascending=False ).head()
train.isnull().sum().any()<define_variables>
result_scores.sort_values(by='Score', ascending=False ).head(15 )
cat_col=[col for col in train.columns if train[col].dtype=='O']<drop_column>
team_name[team_name.TeamID.isin([1258, 1328])]
train=train.drop(cat_col,axis=1 )<create_dataframe>
data = pd.merge(data, season_score, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 data = data.rename(columns={'Score':'WScoreT'})
 data = pd.merge(data, season_score, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 data = data.rename(columns={'Score':'LScoreT'})
 data = data.drop(columns=['WScore', 'LScore'])
 data.head()
test_new=test.copy(deep=True )<drop_column>
Wdata = data.drop(columns=['Season',
])
 Wdata.rename(columns={'WSeed':'Seed1', 'LSeed':'Seed2',
'WScoreT':'ScoreT1', 'LScoreT':'ScoreT2',
'WTeamID':'TeamID_1', 'LTeamID': 'TeamID_2'}, inplace=True)
 Wdata.head()
test_new=test_new.drop(cat_col,axis=1 )<correct_missing_values>
Ldata = data[['LTeamID', 'WTeamID', 'LSeed', 'WSeed', 'LScoreT', 'WScoreT']]
Ldata.rename(columns={'LTeamID':'TeamID_1', 'WTeamID':'TeamID_2',
'LSeed':'Seed1', 'WSeed':'Seed2',
'LScoreT':'ScoreT1', 'WScoreT':'ScoreT2',}, inplace=True)
 Ldata.head()
train=train.fillna(0 )<correct_missing_values>
Wdata['Seed_diff'] = Wdata['Seed1'] - Wdata['Seed2']
Wdata['ScoreT_diff'] = Wdata['ScoreT1'] - Wdata['ScoreT2']
Ldata['Seed_diff'] = Ldata['Seed1'] - Ldata['Seed2']
Ldata['ScoreT_diff'] = Ldata['ScoreT1'] - Ldata['ScoreT2']
test_new=test_new.fillna(0 )<install_modules>
Wdata['result'] = 1
Ldata['result'] = 0
train = pd.concat(( Wdata, Ldata)).reset_index(drop=True)
 train.head()
! pip install -U sliced<create_dataframe>
test = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 test['Season'] = test.ID.map(lambda string : int(string.split('_')[0]))
test['TeamID_1'] = test.ID.map(lambda string : int(string.split('_')[1]))
test['TeamID_2'] = test.ID.map(lambda string : int(string.split('_')[2]))
test = test.drop(columns=['ID'])
 test.head()
train_copy=train.copy(deep=True )<drop_column>
test = pd.merge(test, seeds, left_on=['Season', 'TeamID_1'], right_on=['Season', 'TeamID'], how='left')
 test.rename(columns={'Seed':'Seed1'}, inplace=True)
 test = test.drop('TeamID', axis=1)
 test = pd.merge(test, seeds, left_on=['Season', 'TeamID_2'], right_on=['Season', 'TeamID'], how='left')
 test.rename(columns={'Seed':'Seed2'}, inplace=True)
 test = test.drop('TeamID', axis=1)
 test = pd.merge(test, season_score, left_on=['Season', 'TeamID_1'], right_on=['Season', 'TeamID'], how='left')
 test.rename(columns={'Score':'ScoreT1'}, inplace=True)
 test = pd.merge(test, season_score, left_on=['Season', 'TeamID_2'], right_on=['Season', 'TeamID'], how='left')
 test.rename(columns={'Score':'ScoreT2'}, inplace=True)
 test['Seed_diff'] = test['Seed1'] - test['Seed2']
test['ScoreT_diff'] = test['ScoreT1'] - test['ScoreT2']
test = test.drop(columns=['Pred', 'Season'])
 test.head()
train_copy=train_copy.drop('Col2',axis=1 )<normalization>
X_train = train.drop(columns=['result'])
 y_train = train.result
X_test = test.copy()
data_full = pd.concat([X_train, X_test])
 data_full.shape
vt=VarianceThreshold(threshold=0)
 vt_x=vt.fit_transform(train_copy )<categorify>
OH_cols = ['TeamID_1', 'TeamID_2']
OH_full = pd.get_dummies(
 data_full[OH_cols],
columns=OH_cols,
drop_first=True,
dummy_na=True,
sparse=True,
).sparse.to_coo()
vt_test=vt.transform(test_new )<choose_model_class>
retain_full = data_full.drop(columns=OH_cols)
 retain_full = retain_full/retain_full.max()
retain_full.head()
sir = SlicedInverseRegression(n_directions=2)
<train_model>
encoded_full = scipy.sparse.hstack([OH_full, retain_full, retain_full**2] ).tocsr()
print(encoded_full.shape)
 encoded_train = encoded_full[:len(X_train)]
encoded_test = encoded_full[len(X_train):]
sir.fit(vt_x,train['Col2'] )<normalization>
submission = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 submission.head()
X_sir=sir.transform(vt_x )<normalization>
LGB = LGBMClassifier(
 n_estimators=10000,
learning_rate =0.005,
max_depth=-1,
objective= 'binary',
eval_metric='cross_entropy',
first_metric_only=True, )
X_test=sir.transform(vt_test )<import_modules>
n_folds = 10
cv = StratifiedKFold(n_splits=n_folds, shuffle=True)
 losses = []
LGB_predicts = []
for i,(train,valid)in enumerate(cv.split(encoded_train, y_train)) :
LGB.fit(encoded_train[train], y_train[train],
eval_set=[(encoded_train[train], y_train[train]),(encoded_train[valid, :], y_train[valid])],
verbose=False)
 test_pred = LGB.predict_proba(encoded_test)[:,1]
LGB_predicts.append(test_pred)
 LGB_predicts = np.asarray(LGB_predicts)
 LGB_predict = np.mean(LGB_predicts, axis=0 )
from sklearn.linear_model import LogisticRegression<train_model>
submission.Pred = LGB_predict
submission.to_csv(f'LGB.csv', index=False )
lr=LogisticRegression().fit(X_sir,train['Col2'] )<create_dataframe>
opt_result = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyCompactResults.csv')
 opt_result = opt_result[opt_result.Season > 2014].reset_index()
opt_result = opt_result[['Season', 'WTeamID', 'LTeamID']]
opt_result.head()
submission=pd.DataFrame({'Col1':test.Col1,'Col2':lr.predict(X_test)} )<save_to_csv>
for index, match in opt_result.iterrows() :
opt_result.loc[index, 'ID'] = f"{match.Season}_{'_'.join(str(num)for num in sorted([match.WTeamID,match.LTeamID])) }"
opt_result.loc[index, 'Pred'] = 1 if match.WTeamID < match.LTeamID else 0
opt_result = opt_result[['ID', 'Pred']]
opt_result.head()
submission.to_csv('submission_reduction.csv',index=False )<load_from_csv>
opt_test = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv' ).drop(columns='Pred')
 opt_test.head()
<feature_engineering><EOS> 
predict = pd.merge(left=opt_test, right=opt_result, how='left', on='ID')
 predict.Pred = predict.Pred.fillna(0)
 predict.to_csv('Phan_Viet_Hoang.csv', index=False )