Kaggle competition: Google Cloud & NCAA® ML Competition 2019-Men's
<SOS> metric: LogLoss Kaggle data source: mens-machine-learning-competition-2019<prepare_x_and_y>
import numpy as np
import pandas as pd
import os
finalX = np.array(finalX)
 finalY = np.array(finalY )<prepare_x_and_y>
tourney_cresults = pd.read_csv('.. /input/stage2datafiles/NCAATourneyCompactResults.csv')
 tourney_cresults = tourney_cresults.loc[tourney_cresults['Season'] >= 2003]
training_set = pd.DataFrame()
training_set['Result'] = np.random.randint(0,2,len(tourney_cresults.index))
training_set['Season'] = tourney_cresults['Season'].values
training_set['Team1'] = training_set['Result'].values * tourney_cresults['WTeamID'].values +(1-training_set['Result'].values)* tourney_cresults['LTeamID'].values
training_set['Team2'] =(1-training_set['Result'].values)* tourney_cresults['WTeamID'].values + training_set['Result'].values * tourney_cresults['LTeamID'].values
finalY = finalY.reshape(( 60000, 10))<import_modules>
seeds = pd.read_csv('.. /input/stage2datafiles/NCAATourneySeeds.csv')
 seeds['Seed'] = pd.to_numeric(seeds['Seed'].str[1:3], downcast='integer',errors='coerce')
 def delta_seed(row):
cond =(seeds['Season'] == row['Season'])
 return seeds[cond &(seeds['TeamID'] == row['Team1'])]['Seed'].iloc[0] - seeds[cond &(seeds['TeamID'] == row['Team2'])]['Seed'].iloc[0]
training_set['deltaSeed'] = training_set.apply(delta_seed,axis=1 )
<choose_model_class>
mo = pd.read_csv('.. /input/masseyordinals_thru_2019_day_128/MasseyOrdinals_thru_2019_day_128.csv')
 mo = mo[(mo['RankingDayNum'] == 128)&(mo['Season'] >= 2003)]
def delta_ord(row):
cond =(mo['Season'] == row['Season'])
 cond1 =(mo['TeamID'] == row['Team1'])& cond
cond2 =(mo['TeamID'] == row['Team2'])& cond
t1 = mo[cond1]['OrdinalRank'].mean()
t2 = mo[cond2]['OrdinalRank'].mean()
return t1-t2
training_set['deltaMO'] = training_set.apply(delta_ord,axis=1)
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape = X.shape[1:]))
model.add(Conv2D(filters = 32, kernel_size =(4,4),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.3))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.3))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 model.summary()<train_model>
season_dresults = pd.read_csv('.. /input/stage2datafiles/RegularSeasonDetailedResults.csv')
 record = pd.DataFrame({'wins': season_dresults.groupby(['Season','WTeamID'] ).size() } ).reset_index() ;
losses = pd.DataFrame({'losses': season_dresults.groupby(['Season','LTeamID'] ).size() } ).reset_index() ;
record = record.merge(losses, how='outer', left_on=['Season','WTeamID'], right_on=['Season','LTeamID'])
 record = record.fillna(0)
 record['games'] = record['wins'] + record['losses']
def delta_winPct(row):
cond1 =(record['Season'] == row['Season'])&(record['WTeamID'] == row['Team1'])
 cond2 =(record['Season'] == row['Season'])&(record['WTeamID'] == row['Team2'])
 return(record[cond1]['wins']/record[cond1]['games'] ).mean() -(record[cond2]['wins']/record[cond2]['games'] ).mean()
training_set['deltaWinPct'] = training_set.apply(delta_winPct,axis=1)
validation_split = 0.01
model.fit(finalX, finalY, batch_size=1000, class_weight=class_weight, epochs=80, verbose=1, validation_split=validation_split )<load_pretrained>
dfW = season_dresults.groupby(['Season','WTeamID'] ).sum().reset_index()
dfL = season_dresults.groupby(['Season','LTeamID'] ).sum().reset_index()
def get_points_for(row):
wcond =(dfW['Season'] == row['Season'])&(dfW['WTeamID'] == row['WTeamID'])
 fld1 = 'WScore'
lcond =(dfL['Season'] == row['Season'])&(dfL['LTeamID'] == row['WTeamID'])
 fld2 = 'LScore'
retVal = dfW[wcond][fld1].sum()
if len(dfL[lcond][fld2])> 0:
retVal = retVal + dfL[lcond][fld2].sum()
return retVal
def get_points_against(row):
wcond =(dfW['Season'] == row['Season'])&(dfW['WTeamID'] == row['WTeamID'])
 fld1 = 'LScore'
lcond =(dfL['Season'] == row['Season'])&(dfL['LTeamID'] == row['WTeamID'])
 fld2 = 'WScore'
retVal = dfW[wcond][fld1].sum()
if len(dfL[lcond][fld2])> 0:
retVal = retVal + dfL[lcond][fld2].sum()
return retVal
record['PointsFor'] = record.apply(get_points_for, axis=1)
 record['PointsAgainst'] = record.apply(get_points_against, axis=1 )
open('model.json', 'w' ).write(model.to_json())
 model.save_weights('weights.h5')
 print("Model Saved" )<prepare_x_and_y>
def get_remaining_stats(row, field):
wcond =(dfW['Season'] == row['Season'])&(dfW['WTeamID'] == row['WTeamID'])
 fld1 = 'W' + field
lcond =(dfL['Season'] == row['Season'])&(dfL['LTeamID'] == row['WTeamID'])
 fld2 = 'L'+ field
retVal = dfW[wcond][fld1].sum()
if len(dfL[lcond][fld2])> 0:
retVal = retVal + dfL[lcond][fld2].sum()
return retVal
cols = ['FGM','FGA','FGM3','FGA3','FTM','FTA','OR','DR','Ast','TO','Stl','Blk','PF']
for col in cols:
print("Processing",col)
 record[col] = record.apply(get_remaining_stats, args=(col,), axis=1)
test.drop(['id'],axis=1,inplace=True)
 Y = test.as_matrix(columns=None)
 Y.shape<predict_on_test>
def delta_stat(row, field):
cond1 =(record['Season'] == row['Season'])&(record['WTeamID'] == row['Team1'])
 cond2 =(record['Season'] == row['Season'])&(record['WTeamID'] == row['Team2'])
 return(record[cond1][field]/record[cond1]['games'] ).mean() -(record[cond2][field]/record[cond2]['games'] ).mean()
cols = ['PointsFor','PointsAgainst','FGM','FGA','FGM3','FGA3','FTM','FTA','OR','DR','Ast','TO','Stl','Blk','PF']
for col in cols:
print("Processing",col)
 training_set['delta' + col] = training_set.apply(delta_stat,args=(col,),axis=1)
 training_set.describe()
predict = model.predict(Y )<prepare_output>
cols = ['deltaSeed','deltaMO', 'deltaWinPct','deltaPointsFor','deltaPointsAgainst','deltaFGM','deltaFGA','deltaFGM3','deltaFGA3','deltaFTM',
'deltaFTA','deltaOR','deltaDR','deltaAst','deltaTO','deltaStl','deltaBlk','deltaPF']
X = training_set[cols]
y = training_set['Result']
logit_model=sm.Logit(y,X)
 result=logit_model.fit()
print(result.summary2())
results = np.argmax(predict,axis = 1)
 results<prepare_x_and_y>
winner=[0.52951,0.438933,0.481309,0.438575,0.531942]
top5=[0.54772,0.453872,0.519706,0.461073,0.549876]
topprctvalues=[5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100]
topprctscores=[[0.55620,0.465207,0.536976,0.472893,0.576667,],
[0.56035,0.469617,0.543597,0.479488,0.581399,],
[0.56411,0.474137,0.550067,0.486432,0.586220,],
[0.56958,0.478107,0.556954,0.491655,0.590554,],
[0.57177,0.481519,0.563566,0.495595,0.594101,],
[0.57495,0.486253,0.569619,0.500194,0.597493,],
[0.57946,0.490536,0.577442,0.505821,0.601023,],
[0.58676,0.496779,0.582541,0.511053,0.603920,],
[0.59418,0.500862,0.585897,0.515844,0.608200,],
[0.59921,0.508830,0.589459,0.522958,0.613297,],
[0.60593,0.516735,0.594342,0.528533,0.621995,],
[0.61288,0.523082,0.607036,0.534732,0.631227,],
[0.61915,0.528358,0.627708,0.541466,0.645569,],
[0.62780,0.539478,0.646980,0.556034,0.664821,],
[0.65659,0.552809,0.682279,0.576019,0.688618,],
[0.67932,0.574223,0.764058,0.605337,0.705595,],
[0.73514,0.625012,0.865465,0.643514,0.776256,],
[0.98764,0.692878,0.865465,0.693147,1.071796],
[1.59656,0.882637,1.045207,0.897480,1.204277,],
[19.18820,11.513103,9.092529,12.609623,18.092158,],]
Y=Y.reshape(test.shape[0],28,28 )<drop_column>
cols = ['deltaMO', 'deltaWinPct', 'deltaPointsAgainst']
biases = {
'1163':1.2,
'1181':1.2,
'1437':1.2,
'1314':1.2,
}
errs = []
for year in range(2003,2019):
print("Evaluation on tournament year",year)
 Xtrain = training_set[training_set['Season'] != year][cols]
ytrain = training_set[training_set['Season'] != year]['Result']
logit_model=sm.Logit(ytrain,Xtrain)
 result=logit_model.fit()
Xtest = training_set[training_set['Season'] == year][cols]
ytest = training_set[training_set['Season'] == year]['Result']
pred = result.predict(Xtest)
 for bias in biases:
pred.loc[(training_set['Season'] == year)&(training_set['Team1'] == int(bias)) ] = pred.loc[(training_set['Season'] == year)&(training_set['Team1'] == int(bias)) ] * biases[bias]
pred.loc[(training_set['Season'] == year)&(training_set['Team2'] == int(bias)) ] = pred.loc[(training_set['Season'] == year)&(training_set['Team2'] == int(bias)) ] / biases[bias]
pred.loc[(pred >= 0.9999)] = 0.9999
pred.loc[(pred <= 0.0001)] = 0.0001
pred.loc[training_set[training_set['Season'] == year]['Result'] == 0] = 1 - pred.loc[training_set[training_set['Season'] == year]['Result'] == 0]
err = -np.log(pred ).mean()
errs.append(err)
 print("Mean log loss: ",np.mean(errs))
print("Log losses by season")
 print("--------------------")
 print("year","your score",sep="\t")
 years1 = range(2003,2014)
 for i in range(len(years1)) :
print(years1[i], "{0:.6f}".format(errs[i]),sep="\t")
 print("Log losses by season")
 print("--------------------")
 print("year","your score","your result","winning score",sep="\t")
 years2 = range(2014,2019)
 for i in range(len(years2)) :
result = None
if errs[i+len(years1)] < winner[i]:
result="Win competition"
elif errs[i+len(years1)] < top5[i]:
result="Top 5 score"
else:
for j in range(len(topprctvalues)) :
if errs[i+len(years1)] < topprctscores[j][i]:
result = "Top " + str(topprctvalues[j])+ "%"
break
if result is None:
result = "Worst score on Kaggle"
print(years2[i], "{0:.6f}".format(errs[i+len(years1)]),result+"\t",winner[i],sep="\t")
ss.drop(n,axis=1,inplace=True )<feature_engineering>
sub = pd.read_csv('.. /input/SampleSubmissionStage2.csv')
 sub['Season'], sub['Team1'], sub['Team2'] = sub['ID'].str.split('_' ).str
sub[['Season', 'Team1', 'Team2']] = sub[['Season', 'Team1', 'Team2']].apply(pd.to_numeric)
 sub['deltaSeed'] = sub.apply(delta_seed,axis=1)
 sub['deltaMO'] = sub.apply(delta_ord,axis=1)
 sub['deltaWinPct'] = sub.apply(delta_winPct,axis=1)
 cols = ['PointsFor','PointsAgainst','FGM','FGA','FGM3','FGA3','FTM','FTA','OR','DR','Ast','TO','Stl','Blk','PF']
for col in cols:
print("Processing",col)
 sub['delta' + col] = sub.apply(delta_stat,args=(col,),axis=1)
 cols = ['deltaMO', 'deltaWinPct', 'deltaPointsAgainst']
biases = {
'1163':1.2,
'1181':1.2,
'1437':1.2,
'1314':1.2,
}
Xtrain = training_set[cols]
ytrain = training_set['Result']
logit_model=sm.Logit(ytrain,Xtrain)
 result=logit_model.fit()
Xtest = sub[cols]
pred = result.predict(Xtest)
 for bias in biases:
pred.loc[sub['Team1'] == int(bias)] = pred.loc[sub['Team1'] == int(bias)] * biases[bias]
pred.loc[sub['Team2'] == int(bias)] = pred.loc[sub['Team2'] == int(bias)] / biases[bias]
pred.loc[(pred >= 0.9999)] = 0.9999
pred.loc[(pred <= 0.0001)] = 0.0001
pred.loc[(sub['Team1'] == 1163)&(sub['Team2'] == 1181)] = 0.8
sub['Pred'] = pred
sub[['ID', 'Pred']].to_csv('submission_lr.csv', index=False)
ss[n] = results<save_to_csv>
model = en.ExtraTreesClassifier(n_estimators=200)
 model.fit(Xtrain.fillna(-1), ytrain)
 predictions = model.predict(Xtrain.fillna(-1)).clip(0,1)
 print('Log Loss:', log_loss(ytrain, predictions))
ss.to_csv('submission.csv',index=False )<set_options>
predictions = model.predict(Xtest.fillna(-1)).clip(0,1)
 sub['Pred'] = predictions
sub[['ID', 'Pred']].to_csv('submission_et.csv', index=False )
<set_options><EOS> 
FileLink('./submission_et.csv' )