Kaggle competition: Google Cloud & NCAA® ML Competition 2019-Men's
<SOS> metric: LogLoss Kaggle data source: mens-machine-learning-competition-2019<choose_model_class>
import math
import numpy as np
import pandas as pd
from sklearn.metrics import log_loss
from sklearn.preprocessing import StandardScaler
learn1 = cnn_learner(data, models.resnet34, metrics=[error_rate, accuracy], model_dir = '/kaggle/output/model')
 learn2 = cnn_learner(data, models.resnet50, metrics=[error_rate, accuracy], model_dir = '/kaggle/output/model' )<find_best_params>
def Aggregate(teamcompactresults1,
teamcompactresults2,
merged_results,
regularseasoncompactresults):
winningteam1compactresults = pd.merge(how='left',
left=teamcompactresults1,
right=regularseasoncompactresults,
left_on=['year', 'team1'],
right_on=['Season', 'WTeamID'])
 winningteam1compactresults.drop(['Season',
'DayNum',
'WTeamID',
'LTeamID',
'LScore',
'WLoc',
'NumOT'],
inplace=True,
axis=1)
 grpwinningteam1resultsaverage = \
winningteam1compactresults.groupby(['year', 'team1'] ).mean()
winningteam1resultsaverage = grpwinningteam1resultsaverage.reset_index()
winningteam1resultsaverage.rename(columns={'WScore': 'team1WAverage'},
inplace=True)
 grpwinningteam1resultsmin = \
winningteam1compactresults.groupby(['year', 'team1'] ).min()
winningteam1resultsmin = grpwinningteam1resultsmin.reset_index()
winningteam1resultsmin.rename(columns={'WScore': 'team1Wmin'},
inplace=True)
 grpwinningteam1resultsmax = \
winningteam1compactresults.groupby(['year', 'team1'] ).max()
winningteam1resultsmax = grpwinningteam1resultsmax.reset_index()
winningteam1resultsmax.rename(columns={'WScore': 'team1Wmax'},
inplace=True)
 grpwinningteam1resultsmedian = \
winningteam1compactresults.groupby(['year', 'team1'] ).median()
winningteam1resultsmedian = grpwinningteam1resultsmedian.reset_index()
winningteam1resultsmedian.rename(columns={'WScore': 'team1Wmedian'},
inplace=True)
 grpwinningteam1resultsstd = \
winningteam1compactresults.groupby(['year', 'team1'] ).std()
winningteam1resultsstd = grpwinningteam1resultsstd.reset_index()
winningteam1resultsstd.rename(columns={'WScore': 'team1Wstd'},
inplace=True)
 losingteam1compactresults = pd.merge(how='left',
left=teamcompactresults1,
right=regularseasoncompactresults,
left_on=['year', 'team1'],
right_on=['Season', 'LTeamID'])
 losingteam1compactresults.drop(['Season',
'DayNum',
'WTeamID',
'LTeamID',
'WScore',
'WLoc',
'NumOT'],
inplace=True,
axis=1)
 grplosingteam1resultsaverage = \
losingteam1compactresults.groupby(['year', 'team1'] ).mean()
losingteam1resultsaverage = grplosingteam1resultsaverage.reset_index()
losingteam1resultsaverage.rename(columns={'LScore': 'team1LAverage'},
inplace=True)
 grplosingteam1resultsmin = \
losingteam1compactresults.groupby(['year', 'team1'] ).min()
losingteam1resultsmin = grplosingteam1resultsmin.reset_index()
losingteam1resultsmin.rename(columns={'LScore': 'team1Lmin'},
inplace=True)
 grplosingteam1resultsmax = \
losingteam1compactresults.groupby(['year', 'team1'] ).max()
losingteam1resultsmax = grplosingteam1resultsmax.reset_index()
losingteam1resultsmax.rename(columns={'LScore': 'team1Lmax'},
inplace=True)
 grplosingteam1resultsmedian = \
losingteam1compactresults.groupby(['year', 'team1'] ).median()
losingteam1resultsmedian = grplosingteam1resultsmedian.reset_index()
losingteam1resultsmedian.rename(columns={'LScore': 'team1Lmedian'},
inplace=True)
 grplosingteam1resultsstd = \
losingteam1compactresults.groupby(['year', 'team1'] ).std()
losingteam1resultsstd = grplosingteam1resultsstd.reset_index()
losingteam1resultsstd.rename(columns={'LScore': 'team1Lstd'},
inplace=True)
 winningteam2compactresults = pd.merge(how='left',
left=teamcompactresults2,
right=regularseasoncompactresults,
left_on=['year', 'team2'],
right_on=['Season', 'WTeamID'])
 winningteam2compactresults.drop(['Season',
'DayNum',
'WTeamID',
'LTeamID',
'LScore',
'WLoc',
'NumOT'],
inplace=True,
axis=1)
 grpwinningteam2resultsaverage = \
winningteam2compactresults.groupby(['year', 'team2'] ).mean()
winningteam2resultsaverage = grpwinningteam2resultsaverage.reset_index()
winningteam2resultsaverage.rename(columns={'WScore': 'team2WAverage'},
inplace=True)
 grpwinningteam2resultsmin = \
winningteam2compactresults.groupby(['year', 'team2'] ).min()
winningteam2resultsmin = grpwinningteam2resultsmin.reset_index()
winningteam2resultsmin.rename(columns={'WScore': 'team2Wmin'},
inplace=True)
 grpwinningteam2resultsmax = \
winningteam2compactresults.groupby(['year', 'team2'] ).max()
winningteam2resultsmax = grpwinningteam2resultsmax.reset_index()
winningteam2resultsmax.rename(columns={'WScore': 'team2Wmax'},
inplace=True)
 grpwinningteam2resultsmedian = \
winningteam2compactresults.groupby(['year', 'team2'] ).median()
winningteam2resultsmedian = grpwinningteam2resultsmedian.reset_index()
winningteam2resultsmedian.rename(columns={'WScore': 'team2Wmedian'},
inplace=True)
 grpwinningteam2resultsstd = \
winningteam2compactresults.groupby(['year', 'team2'] ).std()
winningteam2resultsstd = grpwinningteam2resultsstd.reset_index()
winningteam2resultsstd.rename(columns={'WScore': 'team2Wstd'},
inplace=True)
 losingteam2compactresults = pd.merge(how='left',
left=teamcompactresults2,
right=regularseasoncompactresults,
left_on=['year', 'team2'],
right_on=['Season', 'LTeamID'])
 losingteam2compactresults.drop(['Season',
'DayNum',
'WTeamID',
'LTeamID',
'WScore',
'WLoc',
'NumOT'],
inplace=True,
axis=1)
 grplosingteam2resultsaverage = \
losingteam2compactresults.groupby(['year', 'team2'] ).mean()
losingteam2resultsaverage = grplosingteam2resultsaverage.reset_index()
losingteam2resultsaverage.rename(columns={'LScore': 'team2LAverage'},
inplace=True)
 grplosingteam2resultsmin = \
losingteam2compactresults.groupby(['year', 'team2'] ).min()
losingteam2resultsmin = grplosingteam2resultsmin.reset_index()
losingteam2resultsmin.rename(columns={'LScore': 'team2Lmin'},
inplace=True)
 grplosingteam2resultsmax = \
losingteam2compactresults.groupby(['year', 'team2'] ).max()
losingteam2resultsmax = grplosingteam2resultsmax.reset_index()
losingteam2resultsmax.rename(columns={'LScore': 'team2Lmax'},
inplace=True)
 grplosingteam2resultsmedian = \
losingteam2compactresults.groupby(['year', 'team2'] ).median()
losingteam2resultsmedian = grplosingteam2resultsmedian.reset_index()
losingteam2resultsmedian.rename(columns={'LScore': 'team2Lmedian'},
inplace=True)
 grplosingteam2resultsstd = \
losingteam2compactresults.groupby(['year', 'team2'] ).std()
losingteam2resultsstd = grplosingteam2resultsstd.reset_index()
losingteam2resultsstd.rename(columns={'LScore': 'team2Lstd'},
inplace=True)
 agg_results = pd.merge(how='left',
left=merged_results,
right=winningteam1resultsaverage,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam1resultsaverage,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam1resultsmin,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam1resultsmin,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam1resultsmax,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam1resultsmax,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam1resultsmedian,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam1resultsmedian,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam1resultsstd,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam1resultsstd,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam2resultsaverage,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam2resultsaverage,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam2resultsmin,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam2resultsmin,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam2resultsmax,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam2resultsmax,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam2resultsmedian,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam2resultsmedian,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=winningteam2resultsstd,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 agg_results = pd.merge(how='left',
left=agg_results,
right=losingteam2resultsstd,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 return agg_results
learn1.unfreeze()<train_model>
def GrabData() :
tourneyresults = pd.read_csv('.. /input/stage2datafiles/NCAATourneyCompactResults.csv')
 tourneyseeds = pd.read_csv('.. /input/stage2datafiles/NCAATourneySeeds.csv')
 regularseasoncompactresults = \
pd.read_csv('.. /input/stage2datafiles/RegularSeasonCompactResults.csv')
 sample = pd.read_csv('.. /input/SampleSubmissionStage2.csv')
 results = pd.DataFrame()
results['year'] = tourneyresults.Season
results['team1'] = np.minimum(tourneyresults.WTeamID, tourneyresults.LTeamID)
 results['team2'] = np.maximum(tourneyresults.WTeamID, tourneyresults.LTeamID)
 results['result'] =(tourneyresults.WTeamID <
tourneyresults.LTeamID ).astype(int)
 merged_results = pd.merge(left=results,
right=tourneyseeds,
left_on=['year', 'team1'],
right_on=['Season', 'TeamID'])
 merged_results.drop(['Season', 'TeamID'], inplace=True, axis=1)
 merged_results.rename(columns={'Seed': 'team1Seed'}, inplace=True)
 merged_results = pd.merge(left=merged_results,
right=tourneyseeds,
left_on=['year', 'team2'],
right_on=['Season', 'TeamID'])
 merged_results.drop(['Season', 'TeamID'], inplace=True, axis=1)
 merged_results.rename(columns={'Seed': 'team2Seed'}, inplace=True)
 merged_results['team1Seed'] = \
merged_results['team1Seed'].apply(lambda x: str(x)[1:3])
 merged_results['team2Seed'] = \
merged_results['team2Seed'].apply(lambda x: str(x)[1:3])
 merged_results = merged_results.astype(int)
 winsbyyear = regularseasoncompactresults[['Season', 'WTeamID']].copy()
winsbyyear['wins'] = 1
wins = winsbyyear.groupby(['Season', 'WTeamID'] ).sum()
wins = wins.reset_index()
lossesbyyear = regularseasoncompactresults[['Season', 'LTeamID']].copy()
lossesbyyear['losses'] = 1
losses = lossesbyyear.groupby(['Season', 'LTeamID'] ).sum()
losses = losses.reset_index()
winsteam1 = wins.copy()
winsteam1.rename(columns={'Season': 'year',
'WTeamID': 'team1',
'wins': 'team1wins'}, inplace=True)
 winsteam2 = wins.copy()
winsteam2.rename(columns={'Season': 'year',
'WTeamID': 'team2',
'wins': 'team2wins'}, inplace=True)
 lossesteam1 = losses.copy()
lossesteam1.rename(columns={'Season': 'year',
'LTeamID': 'team1',
'losses': 'team1losses'}, inplace=True)
 lossesteam2 = losses.copy()
lossesteam2.rename(columns={'Season': 'year',
'LTeamID': 'team2',
'losses': 'team2losses'}, inplace=True)
 merged_results = pd.merge(how='left',
left=merged_results,
right=winsteam1,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=lossesteam1,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=winsteam2,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=lossesteam2,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 teamcompactresults1 = merged_results[['year', 'team1']].copy()
teamcompactresults2 = merged_results[['year', 'team2']].copy()
train = Aggregate(teamcompactresults1,
teamcompactresults2,
merged_results,
regularseasoncompactresults)
 sample['year'] = sample.ID.apply(lambda x: str(x)[:4] ).astype(int)
 sample['team1'] = sample.ID.apply(lambda x: str(x)[5:9] ).astype(int)
 sample['team2'] = sample.ID.apply(lambda x: str(x)[10:14] ).astype(int)
 merged_results = pd.merge(how='left',
left=sample,
right=tourneyseeds,
left_on=['year', 'team1'],
right_on=['Season', 'TeamID'])
 merged_results.drop(['Season', 'TeamID'], inplace=True, axis=1)
 merged_results.rename(columns={'Seed': 'team1Seed'}, inplace=True)
 merged_results = pd.merge(how='left',
left=merged_results,
right=tourneyseeds,
left_on=['year', 'team2'],
right_on=['Season', 'TeamID'])
 merged_results.drop(['Season', 'TeamID'], inplace=True, axis=1)
 merged_results.rename(columns={'Seed': 'team2Seed'}, inplace=True)
 merged_results['team1Seed'] = \
merged_results['team1Seed'].apply(lambda x: str(x)[1:3] ).astype(int)
 merged_results['team2Seed'] = \
merged_results['team2Seed'].apply(lambda x: str(x)[1:3] ).astype(int)
 merged_results = pd.merge(how='left',
left=merged_results,
right=winsteam1,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=lossesteam1,
left_on=['year', 'team1'],
right_on=['year', 'team1'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=winsteam2,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 merged_results = pd.merge(how='left',
left=merged_results,
right=lossesteam2,
left_on=['year', 'team2'],
right_on=['year', 'team2'])
 teamcompactresults1 = merged_results[['year', 'team1']].copy()
teamcompactresults2 = merged_results[['year', 'team2']].copy()
test = Aggregate(teamcompactresults1,
teamcompactresults2,
merged_results,
regularseasoncompactresults)
 return train, test
learn1.fit_one_cycle(6,3e-5)
<find_best_params>
train, test = GrabData()
trainlabels = train.result.values
train.drop('result', inplace=True, axis=1)
 train.fillna(-1, inplace=True)
 testids = test.ID.values
test.drop(['ID', 'Pred'], inplace=True, axis=1)
 test.fillna(-1, inplace=True )
learn2.unfreeze()<train_model>
ss = StandardScaler()
train[train.columns[3:]] = np.round(ss.fit_transform(train[train.columns[3:]]), 6)
 train['target'] = trainlabels
<train_model>
train[train.columns[3:]].to_csv('mensdata.csv',index=False )
learn2.fit_one_cycle(6,3e-5)
<prepare_output>
def Outputs(data):
return 1./(1.+np.exp(-data))
def GPIndividual1(data):
predictions =(1.0*np.tanh(((((((((((( data["team2Seed"])+(data["team1Lmin"])) /2.0)) +(data["team2Seed"])) /2.0)) +(data["team2Seed"])) /2.0)) -(data["team1Seed"])))+
1.0*np.tanh(((((((((( data["team1Lmin"])/ 2.0)) +(data["team2Seed"])) /2.0)) *(((data["team2Seed"])*(((data["team2Seed"])-(data["team1Seed"])))))))/ 2.0)) +
0.943544*np.tanh(((((((( np.where(data["team1wins"]>0,(( data["team1losses"])/ 2.0),(( data["team1Lmin"])-(data["team1WAverage"])))) +(data["team1wins"])) /2.0)) / 2.0)) / 2.0)))
 return Outputs(predictions)
 def GPIndividual2(data):
predictions =(1.0*np.tanh(((((( data["team2Seed"])+(((((((data["team1Lmin"])+(data["team2Seed"])) /2.0)) +(data["team2Seed"])) /2.0)))/2.0)) -(data["team1Seed"])))+
1.0*np.tanh(((((((data["team1Seed"])*(data["team1Seed"])))-(data["team1Seed"])))*(((((((data["team2Seed"])-(data["team1Seed"])))/ 2.0)) / 2.0)))) +
0.942762*np.tanh(((((((((data["team2Seed"])/ 2.0)) *(data["team1Seed"])))*(((( data["team2wins"])+(data["team1Seed"])) /2.0)))) / 2.0)))
 return Outputs(predictions)
 def GPIndividual3(data):
predictions =(1.0*np.tanh(((((( data["team2Seed"])+(((((((data["team2Seed"])+(data["team1Lmin"])) /2.0)) +(data["team2Seed"])) /2.0)))/2.0)) -(data["team1Seed"])))+
1.0*np.tanh(((((((((data["team2Seed"])-(data["team1Seed"])))*(((( data["team1losses"])+(((data["team1Seed"])/ 2.0)))/2.0)))) *(data["team1Seed"])))/ 2.0)) +
1.0*np.tanh(((((((data["team1Seed"])-(data["team2Seed"])))*(((((((data["team2Seed"])/ 2.0)) / 2.0)) *(data["team1Seed"])))))/ 2.0)))
 return Outputs(predictions)
 def GP(data):
return(GPIndividual1(data)+GPIndividual2(data)+GPIndividual3(data)) /3 .
submit = pd.DataFrame(columns=['id','label'])
 submit['id'] = test['id']<predict_on_test>
print(log_loss(train.target,GPIndividual1(train)))
 print(log_loss(train.target,GPIndividual2(train)))
 print(log_loss(train.target,GPIndividual3(train)))
 print(log_loss(train.target,GP(train)) )
for index, row in test.iterrows() :
pixels=np.asarray(row['pixel0':'pixel783'])
 arr=pixels.reshape(( 28,28)) /255
arr = np.stack([arr]*3,axis=0)
 img = Image(FloatTensor(arr))
submit.at[index, 'label'] = int(( learn1.predict(img)[1] + learn2.predict(img)[1])/2 )<save_to_csv>
test[test.columns[3:]] = np.round(ss.transform(test[test.columns[3:]]), 6)
 predictions = GP(test)
 submission = pd.DataFrame({'ID': testids,
'Pred': np.clip(predictions.values,.01,.99)})
 submission.to_csv('submission.csv', index=False )