Kaggle competition: Google Cloud & NCAA® ML Competition 2020-NCAAW
for column in predict.columns:
sub[['ID', column]].rename(columns={column: 'pred'} ).to_csv('{}.csv'.format(column), index=False )<import_modules>
from sklearn.preprocessing import StandardScaler, MinMaxScaler, OneHotEncoder
from sklearn.model_selection import train_test_split, KFold, cross_val_score, GridSearchCV
from sklearn import preprocessing
import lightgbm as lgb
import optuna
import glob
import os
from pathlib import Path
import numpy as np
import pandas as pd
from sklearn.model_selection import cross_validate, GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score<define_variables>
path = '.. /input/google-cloud-ncaa-march-madness-2020-division-1-womens-tournament/'
Files = 'WDataFiles_Stage1/'
FILEDIR = Path('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament' )<load_from_csv>
TourneyCompactResults = pd.read_csv(path + Files + 'WNCAATourneyCompactResults.csv')
 GameCities = pd.read_csv(path+Files+'WGameCities.csv')
 Seasons = pd.read_csv(path+Files+'WSeasons.csv')
 TourneySeeds = pd.read_csv(path+Files+'WNCAATourneySeeds.csv')
 RegularSeasonCompactResults = pd.read_csv(path+Files+'WRegularSeasonCompactResults.csv' )
sub = pd.read_csv(FILEDIR / 'MSampleSubmissionStage1_2020.csv')
 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()<count_duplicates>
test= pd.read_csv(path +'WSampleSubmissionStage1_2020.csv' )
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() }<load_from_csv>
TourneySeeds['Seed'] = TourneySeeds['Seed'].apply(lambda x: int(x[1:3]))
print(TourneySeeds.shape)
 TourneySeeds.head()
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()<load_from_csv>
train = train.replace({'H':0,'A':1,'N':2})
 train.head()
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']]<load_from_csv>
le = preprocessing.LabelEncoder()
for column in ['CRType']:
le.fit(GameCities[column])
 GameCities[column] = le.transform(GameCities[column])
 GameCities.head()
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()<load_from_csv>
le = preprocessing.LabelEncoder()
for column in ['RegionW','RegionX','RegionY','RegionZ']:
le.fit(Seasons[column])
 Seasons[column] = le.transform(Seasons[column])
 for i in range(0,23):
print(Seasons['DayZero'][i].split('/'))
Seasons['ZeroMonth'] = Seasons['DayZero'][i].split('/')[0]
Seasons['ZeroDay'] = Seasons['DayZero'][i].split('/')[1]
Seasons['ZeroYear'] = Seasons['DayZero'][i].split('/')[2]
Seasons = Seasons.drop('DayZero',axis=1)
 Seasons['ZeroMonth'] = Seasons['ZeroMonth'].astype(int)
 Seasons['ZeroDay'] = Seasons['ZeroMonth'].astype(int)
 Seasons['ZeroYear'] = Seasons['ZeroMonth'].astype(int)
 Seasons.head()
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']<load_from_csv>
train = train.merge(Seasons, how='left',on=['Season'])
 train.head()
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'] )<concatenate>
train = train.merge(TourneySeeds, how='left', left_on=['Season', 'WTeamID'], right_on=['Season','TeamID'])
 train = train.drop('TeamID',axis=1)
 train = train.rename(columns={'Seed': 'WSeed'})
 train = train.merge(TourneySeeds, how='left', left_on=['Season', 'LTeamID'], right_on=['Season','TeamID'])
 train = train.drop('TeamID',axis=1)
 train = train.rename(columns={'Seed': 'LSeed'})
 train.head()
ctcsr = pd.concat([coaches, teams, conferences, seeds, regular], axis=1 )<load_from_csv>
test = test.drop(['Pred'], axis=1)
 test['Season'] = test['ID'].apply(lambda x: int(x.split('_')[0]))
test['WTeamID'] = test['ID'].apply(lambda x: int(x.split('_')[1]))
test['LTeamID'] = test['ID'].apply(lambda x: int(x.split('_')[2]))
test.head()
result = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MNCAATourneyCompactResults.csv')
 result = result[result['Season'] >= 2015].set_index(['Season', 'WTeamID', 'LTeamID'] )<concatenate>
test = test.merge(TourneyCompactResults,how='left',on=['Season','WTeamID','LTeamID'] )
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()<categorify>
test = test.replace({'H':0,'A':1,'N':2})
 test = test.merge(Seasons, how='left',on=['Season'])
 test = test.merge(TourneySeeds, how='left', left_on=['Season', 'WTeamID'], right_on=['Season','TeamID'])
 test = test.drop('TeamID',axis=1)
 test = test.rename(columns={'Seed': 'WSeed'})
 test = test.merge(TourneySeeds, how='left', left_on=['Season', 'LTeamID'], right_on=['Season','TeamID'])
 test = test.drop('TeamID',axis=1)
 test = test.rename(columns={'Seed': 'LSeed'})
x_columns = merged_teams.columns[merged_teams.columns != 'Res']
X = merged_teams[x_columns]
columns_number = X.select_dtypes(include='number' ).columns
X = pd.get_dummies(X, columns=x_columns[X.dtypes == 'object'])
 X<prepare_x_and_y>
test.merge(test,how='left',on=['ID','Season','WTeamID','LTeamID'] )
y = merged_teams['Res']
y<train_on_grid>
not_exist_in_test = [c for c in train.columns.values.tolist() if c not in test.columns.values.tolist() ]
print(not_exist_in_test)
 train = train.drop(not_exist_in_test, axis=1)
 train.head()
params = {
'n_estimators': [25, 50, 100],
'criterion': ['gini', 'entropy'],
'max_depth': [10, 25, 50, None]
}
gs = GridSearchCV(RandomForestClassifier(n_jobs=-1, random_state=0), param_grid=params, cv=8, n_jobs=-1)
 gs.fit(X, y )<train_model>
team_win_score = RegularSeasonCompactResults.groupby(['Season', 'WTeamID'] ).agg({'WScore':['sum', 'count', 'var']} ).reset_index()
team_win_score.columns = [' '.join(col ).strip() for col in team_win_score.columns.values]
team_loss_score = RegularSeasonCompactResults.groupby(['Season', 'LTeamID'] ).agg({'LScore':['sum', 'count', 'var']} ).reset_index()
team_loss_score.columns = [' '.join(col ).strip() for col in team_loss_score.columns.values]
print(team_win_score.shape)
 team_win_score.head()
clf = RandomForestClassifier(n_jobs=-1, random_state=0, **gs.best_params_)
 clf.fit(X, y)
 accuracy_score(y, clf.predict(X))<predict_on_test>
train = pd.merge(train, team_win_score, how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'WTeamID'])
 train = pd.merge(train, team_loss_score, how='left', left_on=['Season', 'LTeamID'], right_on=['Season', 'LTeamID'])
 train = pd.merge(train, team_loss_score, how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'LTeamID'])
 train = pd.merge(train, team_win_score, how='left', left_on=['Season', 'LTeamID_x'], right_on=['Season', 'WTeamID'])
 train.drop(['LTeamID_y', 'WTeamID_y'], axis=1, inplace=True)
 train.head()
predict = pd.DataFrame(clf.predict_proba(X), index=X.index, columns=clf.classes_)
 predict<save_to_csv>
test = pd.merge(test, team_win_score, how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'WTeamID'])
 test = pd.merge(test, team_loss_score, how='left', left_on=['Season', 'LTeamID'], right_on=['Season', 'LTeamID'])
 test = pd.merge(test, team_loss_score, how='left', left_on=['Season', 'WTeamID'], right_on=['Season', 'LTeamID'])
 test = pd.merge(test, team_win_score, how='left', left_on=['Season', 'LTeamID_x'], right_on=['Season', 'WTeamID'])
 test.drop(['LTeamID_y', 'WTeamID_y'], axis=1, inplace=True)
 test.head()
sub.to_csv('RandomForest.csv', index=False )<set_options>
def preprocess(df):
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['x_var'] = df['WScore var_x'] + df['LScore count_y']
df['y_var'] = df['WScore var_y'] + df['WScore var_x']
return df
train = preprocess(train)
 test = preprocess(test)
 test.shape
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
train_win = train.copy()
train_los = train.copy()
train_win = train_win[['WSeed', 'LSeed',
'x_score', 'y_score', 'x_count', 'y_count', 'x_var', 'y_var']]
train_los = train_los[['LSeed', 'WSeed',
'y_score', 'x_score', 'x_count', 'y_count', 'x_var', 'y_var']]
train_win.columns = ['Seed_1', 'Seed_2',
'Score_1', 'Score_2', 'Count_1', 'Count_2', 'Var_1', 'Var_2']
train_los.columns = ['Seed_1', 'Seed_2',
'Score_1', 'Score_2', 'Count_1', 'Count_2', 'Var_1', 'Var_2']
test = test[['ID', 'WSeed', 'LSeed',
'x_score', 'y_score', 'x_count', 'y_count', 'x_var', 'y_var']]
test.columns = ['ID', 'Seed_1', 'Seed_2',
'Score_1', 'Score_2', 'Count_1', 'Count_2', 'Var_1', 'Var_2']
from fastai import *
from fastai.vision import *
import pandas as pd<set_options>
def feature_engineering(df):
df['Seed_diff'] = df['Seed_1'] - df['Seed_2']
df['Score_diff'] = df['Score_1'] - df['Score_2']
df['Count_diff'] = df['Count_1'] - df['Count_2']
df['Var_diff'] = df['Var_1'] - df['Var_2']
df['Mean_score1'] = df['Score_1'] / df['Count_1']
df['Mean_score2'] = df['Score_2'] / df['Count_2']
df['Mean_score_diff'] = df['Mean_score1'] - df['Mean_score2']
df['FanoFactor_1'] = df['Var_1'] / df['Mean_score1']
df['FanoFactor_2'] = df['Var_2'] / df['Mean_score2']
return df
train_win = feature_engineering(train_win)
 train_los = feature_engineering(train_los)
 test = feature_engineering(test)
 test.shape
print('Make sure cuda is installed:', torch.cuda.is_available())
 print('Make sure cudnn is enabled:', torch.backends.cudnn.enabled )<define_variables>
train_win["result"] = 1
print(train_win.shape)
 train_win.head()
path = Path('.. /input' )<load_from_csv>
data = pd.concat(( train_win, train_los)).reset_index(drop=True)
 print(data.shape)
 data.head()
train_df = pd.read_csv(path/'train.csv')
 train_df = pd.concat([train_df['id'],train_df['category_id']],axis=1,keys=['id','category_id'])
 train_df.head()<load_from_csv>
test = test.drop(['ID'],axis=1)
 test.head()
test_df = pd.read_csv(path/'test.csv')
 test_df = pd.DataFrame(test_df['id'])
 test_df['predicted'] = 0
test_df.head()<define_variables>
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import KFold
import lightgbm as lgb
import xgboost as xgb
from xgboost import XGBClassifier
data.show_batch()<choose_model_class>
y_train=data['result']
X_train=data.drop(columns='result' )
learn = cnn_learner(data, base_arch=models.densenet121, metrics=[FBeta() ,accuracy], wd=1e-5 ).mixup()<train_model>
params_lgb = {'num_leaves': 400,
'min_child_weight': 0.034,
'feature_fraction': 0.379,
'bagging_fraction': 0.418,
'min_data_in_leaf': 106,
'objective': 'binary',
'max_depth': 50,
'learning_rate': 0.0068,
"boosting_type": "gbdt",
"bagging_seed": 11,
"metric": 'logloss',
"verbosity": -1,
'reg_alpha': 0.3899,
'reg_lambda': 0.648,
'random_state': 47,
}
params_xgb = {'colsample_bytree': 0.8,
'learning_rate': 0.0004,
'max_depth': 31,
'subsample': 1,
'objective':'binary:logistic',
'eval_metric':'logloss',
'min_child_weight':3,
'gamma':0.25,
'n_estimators':5000
}
lr = 2e-2
learn.fit_one_cycle(2, slice(lr))<save_model>
NFOLDS = 10
folds = KFold(n_splits=NFOLDS)
 columns = X_train.columns
splits = folds.split(X_train, y_train )
learn.save('stage-1-sz32' )<train_model>
y_preds_lgb = np.zeros(test.shape[0])
 y_oof_lgb = np.zeros(X_train.shape[0])
lr = 1e-3
learn.fit_one_cycle(4, slice(lr/100, lr))<save_model>
for fold_n,(train_index, valid_index)in enumerate(splits):
print('Fold:',fold_n+1)
 X_train1, X_valid1 = X_train[columns].iloc[train_index], X_train[columns].iloc[valid_index]
y_train1, y_valid1 = y_train.iloc[train_index], y_train.iloc[valid_index]
dtrain = lgb.Dataset(X_train1, label=y_train1)
 dvalid = lgb.Dataset(X_valid1, label=y_valid1)
 clf = lgb.train(params_lgb, dtrain, 10000, valid_sets = [dtrain, dvalid], verbose_eval=200)
 y_pred_valid = clf.predict(X_valid1)
 y_oof_lgb[valid_index] = y_pred_valid
y_preds_lgb += clf.predict(test)/ NFOLDS
learn.save('stage-2-sz32' )<find_best_params>
submission_df = pd.read_csv(path + 'WSampleSubmissionStage1_2020.csv')
 submission_df['Pred'] = y_preds_lgb
submission_df
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )<feature_engineering>
test= pd.read_csv(path +'WSampleSubmissionStage1_2020.csv')
 test.shape
test_preds = learn.TTA(DatasetType.Test)
 test_df['predicted'] = test_preds[0].argmax(dim=1 )<save_to_csv>
submission_df.to_csv('submission.csv', index=False )