Kaggle competition: Google Cloud & NCAA® ML Competition 2019-Men's
<SOS> metric: LogLoss Kaggle data source: mens-machine-learning-competition-2019<save_model>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
model.save('my_model.h5' )<import_modules>
path = Path('data/MarchMadness')
 dest = path
dest.mkdir(parents=True, exist_ok=True)
 input_path = '.. /input/mens-machine-learning-competition-2019'
data_path = '.. /input/ncaa-19-dataprep/data/MarchMadness'
import shutil
import cv2
import re
from fastai import *
from fastai.vision import *
from fastai.metrics import error_rate<set_options>
df_test = pd.read_csv(f'{data_path}/df_test.csv', low_memory=False)
 df_msr = pd.read_csv(f'{data_path}/df_msr.csv', low_memory=False)
 df = pd.read_csv(f'{data_path}/df.csv', low_memory=False)
 sub = pd.read_csv(f'{input_path}/SampleSubmissionStage2.csv', low_memory=False)
 seeds = pd.read_csv(f'{input_path}/datafiles/NCAATourneySeeds.csv', low_memory=False)
 val_idxs = np.load(f'{data_path}/val_idxs.npy' )
%reload_ext autoreload
%autoreload 2
%matplotlib inline<define_variables>
def random_seed(seed_value, use_cuda):
np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 random.seed(seed_value)
 if use_cuda:
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
bs = 64<load_from_csv>
def join_df(left, right, left_on, right_on=None, on=None, how='left', suffix='_y'):
if right_on is None: right_on = left_on
return left.merge(right, left_on=left_on, right_on=right_on,
on=on, how=how, suffixes=("", suffix))
path = Path('/kaggle/input/Kannada-MNIST')
 train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test =pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 sub = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')
 dig = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv' )<count_missing_values>
base_cols = ['Score']
drop_cols = ['Loc', 'PointDiff_1', 'RankDiff_1', 'Seed_1', 'Seed_2']
for c in base_cols:
drop_cols.append(c+'_1')
 drop_cols.append(c+'_Opp_1')
 drop_cols.append(c+'_2')
 drop_cols.append(c+'_Opp_2')
 df.drop(drop_cols, axis=1, inplace=True)
 df_test.drop(drop_cols, axis=1, inplace=True)
print(train.isna().sum().sum());print(test.isna().sum().sum());print(dig.isna().sum().sum() )<train_model>
dep_var = 'result'
cat_vars = ['Season', 'TeamId_1', 'TeamId_2', 'Coach_1', 'Coach_2',
'Top5_1', 'Top5_2', 'Top25_1', 'Top25_2', 'Top50_1', 'Top50_2',
'ConfAbbrev_1', 'ConfAbbrev_2', 'Is_ConfGm', 'isMajor_1', 'isMajor_2']
cont_vars = [c for c in df.columns if c not in cat_vars]
cont_vars.remove('result')
 test = TabularList.from_df(df_test.copy() , path=path, cat_names=cat_vars, cont_names=cont_vars)
 procs=[FillMissing, Categorify, Normalize]
def save_images(outpath,df):
for index, row in df.iterrows() :
pixels=np.asarray(row['pixel0':'pixel783'])
 img=pixels.reshape(( 28,28))
pathname=os.path.join(outpath,'image_'+str(row['label'])+'_'+str(index)+'.jpg')
 cv2.imwrite(pathname,img )<save_model>
learn = tabular_learner(data, layers=[200,100], emb_drop=0.2,
metrics=[accuracy])
 learn.model
outpath = '/kaggle/output/train'
save_images(outpath,train )<define_variables>
learn.fit_one_cycle(2, wd=0.05 )
path_img = Path(outpath)
 fnames = get_image_files(path_img)
 fnames[:5]<load_pretrained>
learn.save('m_stage2_1')
<define_variables><EOS> 
preds, _ = learn.get_preds(DatasetType.Test)
 eps = 1e-5
df_test['Pred'] = np.clip(preds[:,1], eps, 1-eps)
 df_test = df_test[['Season', 'TeamId_1', 'TeamId_2', 'Pred']]
df_msr = df_msr[['Season', 'TeamId_1', 'TeamId_2', 'result']]
df_msr.reset_index(inplace=True, drop=True)
 df_m = join_df(df_msr, df_test, ['Season', 'TeamId_1', 'TeamId_2'])
 df_m = df_m[df_m.Pred.notnull() ]; df_m.head()
y_true = df_m.result
y_pred = df_m.Pred
log_loss(y_true, y_pred, eps=eps )