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<categorify>
import os
from pathlib import Path
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import cross_validate, GridSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
le = preprocessing.LabelEncoder()
train['province_encoder'] = le.fit_transform(train['Province_State'])
 test['province_encoder'] = le.transform(test['Province_State'] )<count_duplicates>
FILEDIR = Path('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament' )
start_time = time.time()
country = train['Country_Region'].drop_duplicates()
train_df = train.copy()
train_df.rename(columns={'Date': 'date', 'ConfirmedCases': 'cc_cases', 'Fatalities': 'ft_cases', 'Country_Region': 'country', 'Province_State': 'province'}, inplace=True)
 lags = np.arange(1,8,1)
 with tqdm(total = len(list(train_df['date'].unique())))as pbar:
for d in train_df['date'].drop_duplicates() :
for i in country:
province = train_df[train_df['country'] == i]['province'].drop_duplicates()
for j in province:
mask =(train_df['date'] == d)&(train_df['country'] == i)&(train_df['province'] == j)
 for lag in lags:
mask_org =(train_df['date'] ==(d - pd.Timedelta(days=lag)))&(train_df['country'] == i)&(train_df['province'] == j)
 try:
train_df.loc[mask, 'cc_cases_' + str(lag)] = train_df.loc[mask_org, 'cc_cases'].values
except:
train_df.loc[mask, 'cc_cases_' + str(lag)] = 0
try:
train_df.loc[mask, 'ft_cases_' + str(lag)] = train_df.loc[mask_org, 'ft_cases'].values
except:
train_df.loc[mask, 'ft_cases_' + str(lag)] = 0
pbar.update(1)
 print('Time spent for building features is {} minutes'.format(round(( time.time() -start_time)/60,1)) )<load_from_csv>
sub = pd.read_csv(FILEDIR / 'MSampleSubmissionStage1_2020.csv', usecols=['ID'])
 id_splited = sub['ID'].str.split('_', expand=True ).astype(int ).rename(columns={0: 'Season', 1: 'Team1', 2: 'Team2'})
 sub = pd.concat([sub, id_splited], axis=1 ).set_index(['Season', 'Team1', 'Team2'] ).sort_index()
train_df = train_df[train_df['date_int']>=301]
train_df['weekday'] = train_df['date'].dt.weekday
train_df[train_df['country'] == 'Italy'].tail(10 )<split>
tourney_teams = {}
tourney_teams_all = set()
for season in sub.index.get_level_values('Season' ).drop_duplicates() :
tourney_teams[season] = set()
tourney_teams[season].update(sub.loc[season].index.get_level_values('Team1'))
tourney_teams[season].update(sub.loc[season].index.get_level_values('Team2'))
tourney_teams_all.update(tourney_teams[season])
 {k: len(v)for k, v in tourney_teams.items() }
def split_train_val(df, val_ratio):
val_len = int(len(df)* val_ratio)
 train_set = df[:-val_len]
val_set = df[-val_len:]
return train_set, val_set<prepare_x_and_y>
conferences = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MTeamConferences.csv')
 conferences = pd.concat(
 [conferences.query('Season == @season and TeamID in @teams')for season, teams in tourney_teams.items() ])
 conferences = conferences.set_index(['Season', 'TeamID'] ).sort_index()
test_fixed_cols = ['ForecastId', 'Province_State', 'Country_Region', 'Date']
fixed_cols = ['Id', 'province', 'country', 'date']
output_cols = ['cc_cases', 'ft_cases']
input_cols = list(set(train_df.columns.to_list())- set(fixed_cols)- set(output_cols))
print('output columns are ', output_cols)
 print('input columns are ', input_cols)
 X = train_df[input_cols]
y = train_df[output_cols]<categorify>
coaches = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MTeamCoaches.csv')
 coaches = pd.concat(
 [coaches.query('Season == @season and TeamID in @team')for season, team in tourney_teams.items() ])
 coaches = coaches[coaches['LastDayNum'] == 154].set_index(['Season', 'TeamID'] ).sort_index() [['CoachName']]
cc_input = ['country_encoder', 'province_encoder', 'weekday', 'date_int','cc_cases_1', 'cc_cases_2', 'cc_cases_3', 'cc_cases_4', 'cc_cases_5', 'cc_cases_6', 'cc_cases_7']
ft_input = ['country_encoder', 'province_encoder', 'weekday' , 'date_int', 'ft_cases_1', 'ft_cases_2', 'ft_cases_3', 'ft_cases_4', 'ft_cases_5', 'ft_cases_6', 'ft_cases_7']
cc_output = ['cc_cases']
ft_output = ['ft_cases']
val_ratio = 0.05
X_cc = X[cc_input]
X_ft = X[ft_input]
y_cc = y[cc_output]
y_ft = y[ft_output]
train_X_cc, val_X_cc = split_train_val(df = X_cc, val_ratio = val_ratio)
 train_y_cc, val_y_cc = split_train_val(df = y_cc, val_ratio = val_ratio)
 train_X_ft, val_X_ft = split_train_val(df = X_ft, val_ratio = val_ratio)
 train_y_ft, val_y_ft = split_train_val(df = y_ft, val_ratio = val_ratio )<normalization>
teams = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MTeams.csv', usecols=['TeamID', 'FirstD1Season'])
 teams['FirstD1Season'] = 2020 - teams['FirstD1Season']
teams = pd.concat(
 [teams.query('TeamID in @team' ).assign(Season=season)for season, team in tourney_teams.items() ])
 teams = teams.set_index(['Season', 'TeamID'] ).sort_index()
<normalization>
seeds = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MNCAATourneySeeds.csv')
 seeds = pd.concat(
 [seeds.query('Season == @season and TeamID in @teams')for season, teams in tourney_teams.items() ])
 seeds = seeds.set_index(['Season', 'TeamID'] ).sort_index()
seeds['Region'] = seeds['Seed'].str[0]
seeds['Number'] = seeds['Seed'].str[1:3].astype(int)
 del seeds['Seed']
<data_type_conversions>
regular = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MRegularSeasonDetailedResults.csv')
 regular = regular.drop(columns=['DayNum', 'LTeamID'])
 regular = pd.concat(
 [regular.query('Season == @season and WTeamID in @teams')for season, teams in tourney_teams.items() ])
 regular = regular.groupby(['Season', 'WTeamID'] ).sum()
regular = regular.rename_axis(index=['Season', 'TeamID'] )
X_train_cc = train_X_cc.to_numpy()
X_val_cc = val_X_cc.to_numpy()
X_train_ft = train_X_ft.to_numpy()
X_val_ft = val_X_ft.to_numpy()
y_train_cc = train_y_cc.to_numpy()
y_val_cc = val_y_cc.to_numpy()
y_train_ft = train_y_ft.to_numpy()
y_val_ft = val_y_ft.to_numpy()<compute_test_metric>
ctcsr = pd.concat([coaches, teams, conferences, seeds, regular], axis=1 )
def root_mean_squared_log_error(y_true, y_pred):
return K.sqrt(K.mean(K.square(K.log(y_pred + 1)- K.log(y_true + 1))))<choose_model_class>
result = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MNCAATourneyCompactResults.csv')
 result = result[result['Season'] >= 2015].set_index(['Season', 'WTeamID', 'LTeamID'] )
def LSTM_model(n_1, input_dim, output_dim):
model = Sequential()
model.add(LSTM(n_1,input_shape=(1, input_dim), activation='relu'))
model.add(Dense(output_dim, activation='relu'))
model.compile(loss=root_mean_squared_log_error, optimizer='adam')
 return model<choose_model_class>
merged_teams = pd.concat(
 [ctcsr.loc[[(season, wteam),(season, lteam)], :] for season, wteam, lteam, in result.index])
 team1 = merged_teams.iloc[::2, :].reset_index('TeamID')
 team2 = merged_teams.iloc[1::2, :].reset_index('TeamID')
 merged_teams = pd.concat([
pd.concat([team1.add_suffix('1'), team2.add_suffix('2')], axis=1 ).assign(Res=1),
pd.concat([team2.add_suffix('1'), team1.add_suffix('2')], axis=1 ).assign(Res=0),
] ).reset_index().set_index(['Season', 'TeamID1', 'TeamID2'] ).sort_index()
K.clear_session()
model_cc = LSTM_model(4, X_train_cc.shape[-1], y_train_cc.shape[-1])
 model_ft = LSTM_model(4, X_train_ft.shape[-1], y_train_ft.shape[-1])
 early_stop_cc = EarlyStopping(monitor='val_loss', patience=5, verbose=0, mode='min')
 early_stop_ft = EarlyStopping(monitor='val_loss', patience=5, verbose=0, mode='min' )<train_model>
x_columns = merged_teams.columns[merged_teams.columns != 'Res']
X = merged_teams[x_columns]
columns_number = X.select_dtypes(include='number' ).columns
X.loc[:, columns_number] = MinMaxScaler().fit_transform(X[columns_number])
 X = pd.get_dummies(X, columns=x_columns[X.dtypes == 'object'])
 X
print('Start model training')
 start_time = time.time()
history_cc = model_cc.fit(X_train_cc, y_train_cc, batch_size = 16, epochs = 100,validation_data =(X_val_cc, y_val_cc), verbose = 2, callbacks=[early_stop_cc])
 model_cc.save("model_cc.h5")
 print('Time spent for model training is {} minutes'.format(round(( time.time() -start_time)/60,1)) )<train_model>
y = merged_teams['Res']
y
print('Start model training')
 start_time = time.time()
history_ft = model_ft.fit(X_train_ft, y_train_ft, batch_size = 16, epochs = 8,validation_data =(X_val_ft, y_val_ft), verbose = 2, callbacks=[early_stop_ft])
 model_ft.save("model_ft.h5")
 print('Time spent for model training is {} minutes'.format(round(( time.time() -start_time)/60,1)) )<predict_on_test>
clfs = {}
clfs['SVC'] = {
'instance': SVC(probability=True),
'params': [
{'kernel': ['linear'], 'C': [0.01, 0.05, 0.1, 0.5, 1]},
{'kernel': ['rbf'], 'C': [1, 10, 50, 100, 250], 'gamma': [0.1, 0.2, 0.3]}
]
}
clfs['RandomForestClassifier'] = {
'instance': RandomForestClassifier(n_jobs=-1),
'params': {
'n_estimators': [25, 50, 100],
'criterion': ['gini', 'entropy'],
'max_depth': [10, 25, 50, None]
}
}
clfs['LogisticRegression'] = {
'instance': LogisticRegression(max_iter=200, n_jobs=-1),
'params': [
{'penalty': ['l2'], 'C': [0.1, 0.5, 1, 5, 10]},
{'penalty': ['l1'], 'solver': ['liblinear', 'saga'], 'C': [0.1, 0.5, 1, 5, 10]},
{'penalty': ['elasticnet'], 'C': [0.1, 0.5, 1, 5, 10], 'l1_ratio': [0.1, 0.3, 0.5, 0.7, 0.9]}
]
}
yhat_val_cc = model_cc.predict(X_val_cc)
 print(yhat_val_cc[50:70] )<predict_on_test>
for clf_name, clf in clfs.items() :
print('<{}>'.format(clf_name))
print(' training...')
 gs = GridSearchCV(clf['instance'], param_grid=clf['params'], cv=5, n_jobs=-1)
 gs.fit(X, y)
 print(' best_score: {:.3f}'.format(gs.best_score_))
print(' best_params: {}'.format(gs.best_params_))
clfs[clf_name]['best_params'] = gs.best_params_
yhat_val_ft = model_ft.predict(X_val_ft)
 print(yhat_val_ft[60:70] )<create_dataframe>
for clf_name, clf in clfs.items() :
clf['best_estimator'] = clf['instance'].set_params(**clf['best_params'] ).fit(X, y )
submission = pd.DataFrame()
submission['ForecastId'] = test_df['ForecastId']
submission['ConfirmedCases'] = test_df['cc_cases']
submission['Fatalities'] = test_df['ft_cases']<save_to_csv>
[clf['best_estimator'] for clf in clfs.values() ]
submission.to_csv("submission.csv",index=False )<compute_test_metric>
vote = VotingClassifier(
 estimators=[(clf_name, clf['best_estimator'])for clf_name, clf in clfs.items() ],
voting='soft',
n_jobs=-1 
)
 vote.fit(X, y)
 clfs['Vote'] = {}
clfs['Vote']['best_estimator'] = vote
plotly.offline.init_notebook_mode()
%matplotlib inline
def RMSLE(pred,actual):
return np.sqrt(np.mean(np.power(( np.log(pred+1)-np.log(actual+1)) ,2)) )<load_from_csv>
for clf_name, clf in clfs.items() :
score = accuracy_score(y, clf['best_estimator'].predict(X))
print(clf_name, score )
pd.set_option('mode.chained_assignment', None)
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-3/test.csv")
 train = pd.read_csv(".. /input/covid19-global-forecasting-week-3/train.csv")
 train['Province_State'].fillna('', inplace=True)
 test['Province_State'].fillna('', inplace=True)
 train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'])
 train = train.sort_values(['Country_Region','Province_State','Date'])
 test = test.sort_values(['Country_Region','Province_State','Date'] )<categorify>
predict = pd.DataFrame(
 {
**{'pp_' + clf_name: clf['best_estimator'].predict_proba(X)[:, 1] for clf_name, clf in clfs.items() },
**{'p_' + clf_name: clf['best_estimator'].predict(X)for clf_name, clf in clfs.items() }
},
index=X.index)
 predict
train[['ConfirmedCases', 'Fatalities']] = train.groupby(['Country_Region', 'Province_State'])[['ConfirmedCases', 'Fatalities']].transform('cummax')
<feature_engineering>
for column in predict.columns:
sub[column] = 0.5
mask = [idx for idx in sub.index if idx in X.index]
sub.loc[mask, predict.columns] = predict.loc[mask, predict.columns]
sub
<save_to_csv><EOS> 
for column in predict.columns:
sub[['ID', column]].rename(columns={column: 'pred'} ).to_csv('{}.csv'.format(column), index=False )