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<drop_column>
pd.set_option('max_columns', None)
 plt.style.use('seaborn')
 %matplotlib inline
py.init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore')
train.drop(['Name','Ticket','Cabin'],axis = 1,inplace = True )<correct_missing_values>
data_dict = {}
for i in glob.glob('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/*'):
name = i.split('/')[-1].split('.')[0]
if name != 'MTeamSpellings':
data_dict[name] = pd.read_csv(i)
 else:
data_dict[name] = pd.read_csv(i, encoding='cp1252' )
train.dropna(inplace=True )<categorify>
data_dict['MNCAATourneySeeds']['Seed'] = data_dict['MNCAATourneySeeds']['Seed'].apply(lambda x: int(x[1:3]))
data_dict['MNCAATourneySeeds'] = data_dict['MNCAATourneySeeds'][['Season', 'TeamID', 'Seed']]
data_dict['MNCAATourneyCompactResults'] = data_dict['MNCAATourneyCompactResults'][['Season','WTeamID', 'LTeamID']]
df = pd.merge(data_dict['MNCAATourneyCompactResults'], data_dict['MNCAATourneySeeds'],
how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'])
 df = pd.merge(df, data_dict['MNCAATourneySeeds'], how='left', left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'])
 df = df.drop(['TeamID_x', 'TeamID_y'], axis=1)
 df.columns = ['Season', 'WTeamID', 'LTeamID', 'WSeed', 'LSeed']
df.head()
embarked =pd.get_dummies(train['Embarked'], drop_first = True)
 embarked.head()<categorify>
team_win_score = data_dict['MRegularSeasonCompactResults'].groupby(['Season', 'WTeamID'] ).agg({'WScore':['sum', 'count']} ).reset_index()
team_win_score.columns = ['Season', 'WTeamID', 'WScore_sum', 'WScore_count']
team_loss_score = data_dict['MRegularSeasonCompactResults'].groupby(['Season', 'LTeamID'] ).agg({'LScore':['sum', 'count']} ).reset_index()
team_loss_score.columns = ['Season', 'LTeamID', 'LScore_sum', 'LScore_count']
df = pd.merge(df, team_win_score, how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'WTeamID'])
 df = pd.merge(df, team_loss_score, how='left', left_on=['Season', 'LTeamID'], right_on=['Season', 'LTeamID'])
 df = pd.merge(df, team_loss_score, how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'LTeamID'])
 df = pd.merge(df, team_win_score, how='left', left_on=['Season', 'LTeamID_x'], right_on=['Season', 'WTeamID'])
 df.drop(['LTeamID_y', 'WTeamID_y'], axis=1, inplace=True)
 df['x_score'] = df['WScore_sum_x'] + df['LScore_sum_y']
df['y_score'] = df['WScore_sum_y'] + df['LScore_sum_x']
df['x_count'] = df['WScore_count_x'] + df['LScore_count_y']
df['y_count'] = df['WScore_count_y'] + df['WScore_count_x']
df = df.drop(['WScore_sum_x','WScore_count_x','LScore_sum_x','LScore_count_x',
'LScore_sum_y','LScore_count_y','WScore_sum_y','WScore_count_y'], axis =1)
 df.head()
pcl =pd.get_dummies(train['Pclass'], drop_first = True)
 pcl.head()<categorify>
df_win = df.copy()
df_los = df.copy()
df_win = df_win[['WSeed', 'LSeed', 'x_score', 'y_score', 'x_count', 'y_count']]
df_los = df_los[['LSeed', 'WSeed', 'y_score', 'x_score', 'x_count', 'y_count']]
df_win.columns = ['Seed_1', 'Seed_2', 'Score_1', 'Score_2', 'Count_1', 'Count_2']
df_los.columns = ['Seed_1', 'Seed_2', 'Score_1', 'Score_2', 'Count_1', 'Count_2']
df_win['Seed_diff'] = df_win['Seed_1'] - df_win['Seed_2']
df_win['Score_diff'] = df_win['Score_1'] - df_win['Score_2']
df_los['Seed_diff'] = df_los['Seed_1'] - df_los['Seed_2']
df_los['Score_diff'] = df_los['Score_1'] - df_los['Score_2']
sex = pd.get_dummies(train['Sex'], drop_first = True)
 sex.head()<concatenate>
df_win['result'] = 1
df_los['result'] = 0
data = pd.concat(( df_win, df_los)).reset_index(drop=True )
train = pd.concat([train,embarked,pcl,sex],axis = 1 )<drop_column>
test = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 test = test.drop(['Pred'], axis=1)
 test['Season'] = test['ID'].apply(lambda x: int(x.split('_')[0]))
test['Team1'] = test['ID'].apply(lambda x: int(x.split('_')[1]))
test['Team2'] = test['ID'].apply(lambda x: int(x.split('_')[2]))
test = pd.merge(test, data_dict['MNCAATourneySeeds'], how='left', left_on=['Season', 'Team1'], right_on=['Season', 'TeamID'])
 test = pd.merge(test, data_dict['MNCAATourneySeeds'], how='left', left_on=['Season', 'Team2'], right_on=['Season', 'TeamID'])
 test = pd.merge(test, team_win_score, how='left', left_on=['Season', 'Team1'], right_on=['Season', 'WTeamID'])
 test = pd.merge(test, team_loss_score, how='left', left_on=['Season', 'Team2'], right_on=['Season', 'LTeamID'])
 test = pd.merge(test, team_loss_score, how='left', left_on=['Season', 'Team1'], right_on=['Season', 'LTeamID'])
 test = pd.merge(test, team_win_score, how='left', left_on=['Season', 'Team2'], right_on=['Season', 'WTeamID'])
 test.drop(['LTeamID_y', 'WTeamID_y'], axis=1, inplace=True)
 test.head()
train.drop(['Embarked','Pclass','Sex'],axis = 1,inplace = True )<data_type_conversions>
test['x_score'] = test['WScore_sum_x'] + test['LScore_sum_y']
test['y_score'] = test['WScore_sum_y'] + test['LScore_sum_x']
test['x_count'] = test['WScore_count_x'] + test['LScore_count_y']
test['y_count'] = test['WScore_count_y'] + test['WScore_count_x']
test = test[['Seed_x', 'Seed_y', 'x_score', 'y_score', 'x_count', 'y_count']]
test.columns = ['Seed_1', 'Seed_2', 'Score_1', 'Score_2', 'Count_1', 'Count_2']
test['Seed_diff'] = test['Seed_1'] - test['Seed_2']
test['Score_diff'] = test['Score_1'] - test['Score_2']
test_df = test
train.fillna(train.mean() , inplace=True )<prepare_x_and_y>
params_lgb = {'num_leaves': 127,
'min_data_in_leaf': 10,
'objective': 'binary',
'max_depth': -1,
'learning_rate': 0.01,
"boosting_type": "gbdt",
"bagging_seed": 11,
"metric": 'logloss',
"verbosity": -1,
'random_state': 42,
}
X = data.drop('result', axis=1)
 y = data['result']
X = train.drop(['Survived','PassengerId'],axis = 1)
 y = train['Survived']<split>
NFOLDS = 10
folds = KFold(n_splits=NFOLDS)
 columns = X.columns
splits = folds.split(X, y)
 y_preds_lgb = np.zeros(test_df.shape[0])
 y_train_lgb = np.zeros(X.shape[0])
 y_oof = np.zeros(X.shape[0])
 feature_importances = pd.DataFrame()
feature_importances['feature'] = columns
for fold_n,(train_index, valid_index)in enumerate(splits):
print('Fold:',fold_n+1)
 X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
dtrain = lgb.Dataset(X_train, label=y_train)
 dvalid = lgb.Dataset(X_valid, label=y_valid)
 clf = lgb.train(params_lgb, dtrain, 10000, valid_sets = [dtrain, dvalid], verbose_eval=200)
 feature_importances[f'fold_{fold_n + 1}'] = clf.feature_importance()
y_pred_valid = clf.predict(X_valid)
 y_oof[valid_index] = y_pred_valid
y_train_lgb += clf.predict(X)/ NFOLDS
y_preds_lgb += clf.predict(test_df)/ NFOLDS
del X_train, X_valid, y_train, y_valid
gc.collect()
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.33, random_state=123 )<train_model>
clf = GradientBoostingClassifier()
clf.fit(X_train,y_train )<predict_on_test>
df_compact = data_dict['MNCAATourneyCompactResults']
df_detailed = data_dict['MNCAATourneyDetailedResults']
results_merged = pd.merge(df_compact, df_detailed)
 results_merged.head()
y_pred = clf.predict(X_valid )<compute_test_metric>
results_merged['WeFG%'] =(results_merged['WFGM']+0.5*results_merged['WFGM3'])/ results_merged['WFGA']
results_merged['LeFG%'] =(results_merged['LFGM']+0.5*results_merged['LFGM3'])/ results_merged['LFGA']
results_merged['WTO%'] = results_merged['WTO'] /(results_merged['WFGA']+0.44*results_merged['WFTA'] +results_merged['WTO'])
 results_merged['LTO%'] = results_merged['LTO'] /(results_merged['LFGA']+0.44*results_merged['LFTA'] +results_merged['LTO'])
 results_merged['WFTR%'] = results_merged['WFTA'] / results_merged['WFGA']
results_merged['LFTR%'] = results_merged['LFTA'] / results_merged['LFGA']
results_merged['WORB%'] = results_merged['WOR'] / results_merged['LDR']
results_merged['LORB%'] = results_merged['LOR'] / results_merged['WDR']
report = classification_report(y_valid,y_pred)
 print(report )<compute_test_metric>
ts = TrueSkill(draw_probability=0.01)
 beta = 25 / 6
def win_probability(p1, p2):
delta_mu = p1.mu - p2.mu
sum_sigma = p1.sigma * p1.sigma + p2.sigma * p2.sigma
denom = np.sqrt(2 *(beta * beta)+ sum_sigma)
 return ts.cdf(delta_mu / denom)
 submit = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 submit[['Season', 'Team1', 'Team2']] = submit.apply(lambda r:pd.Series([int(t)for t in r.ID.split('_')]), axis=1)
 df_tour = results_merged
teamIds = np.unique(np.concatenate([df_tour.WTeamID.values, df_tour.LTeamID.values]))
ratings = { tid:ts.Rating() for tid in teamIds }
def feed_season_results(season):
print("season = {}".format(season))
df1 = df_tour[df_tour.Season == season]
for r in df1.itertuples() :
ratings[r.WTeamID], ratings[r.LTeamID] = rate_1vs1(ratings[r.WTeamID], ratings[r.LTeamID])
 def update_pred(season):
beta = np.std([r.mu for r in ratings.values() ])
 print("beta = {}".format(beta))
submit.loc[submit.Season==season, 'Pred'] = submit[submit.Season==season].apply(lambda r:win_probability(ratings[r.Team1], ratings[r.Team2]), axis=1)
 for season in sorted(df_tour.Season.unique())[:-5]:
feed_season_results(season)
 update_pred(2015)
 feed_season_results(2015)
 update_pred(2016)
 feed_season_results(2016)
 update_pred(2017)
 feed_season_results(2017)
 update_pred(2018)
 feed_season_results(2018)
 update_pred(2019)
 submit.drop(['Season', 'Team1', 'Team2'], axis=1, inplace=True)
 y_preds_ts = submit['Pred']
accuracy_score(y_valid,y_pred )<categorify>
submission_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 submission_df['Pred'] = 0.4*y_preds_lgb + 0.6*y_preds_ts
sex1 = pd.get_dummies(test['Sex'], drop_first = True )<categorify>
submission_df.to_csv('submission.csv', index=False )