Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_x_and_y>
%matplotlib inline
oof_pred_deepfm = np.zeros(( len(train),))
y_pred_deepfm = np.zeros(( len(test),))
skf = StratifiedKFold(n_splits=N_Splits, shuffle=True, random_state=SEED)
 for fold,(tr_ind, val_ind)in enumerate(skf.split(train, train[target])) :
X_train, X_val = train[sparse_features].iloc[tr_ind], train[sparse_features].iloc[val_ind]
y_train, y_val = train[target].iloc[tr_ind], train[target].iloc[val_ind]
train_model_input = {name:X_train[name] for name in feature_names}
val_model_input = {name:X_val[name] for name in feature_names}
test_model_input = {name:test[name] for name in feature_names}
model = DeepFM(linear_feature_columns, dnn_feature_columns, dnn_hidden_units=(256, 256), dnn_dropout=0.0, dnn_activation='Mish', dnn_use_bn=False, task='binary')
 model.compile('adam', loss = 'binary_crossentropy', metrics=[auc],)
 es = callbacks.EarlyStopping(monitor='val_auc', min_delta=0.001, patience=3, verbose=Verbose, mode='max', baseline=None, restore_best_weights=True)
 sb = callbacks.ModelCheckpoint('./nn_model.w8', save_weights_only=True, save_best_only=True, verbose=Verbose)
 clr = CyclicLR(base_lr=1e-7, max_lr = 1e-4, step_size= int(1.0*(test.shape[0])/(Batch_S_T*4)) , mode='exp_range', gamma=1.0, scale_fn=None, scale_mode='cycle')
 history = model.fit(train_model_input, y_train,
validation_data=(val_model_input, y_val),
batch_size=Batch_S_T, epochs=Epochs, verbose=Verbose,
callbacks=[es, sb, clr],)
 model.load_weights('./nn_model.w8')
 val_pred = model.predict(val_model_input, batch_size=Batch_S_P)
 print(f'validation AUC fold {fold+1} : {round(roc_auc_score(y_val, val_pred), 5)}')
 oof_pred_deepfm[val_ind] = val_pred.ravel()
y_pred_deepfm += model.predict(test_model_input, batch_size=Batch_S_P ).ravel() /(N_Splits)
 K.clear_session() 
<compute_test_metric>
df_test = pd.read_csv('.. /input/titanic/test.csv')
 df_train = pd.read_csv('.. /input/titanic/train.csv' )
print(f'OOF AUC : {round(roc_auc_score(train.target.values, oof_pred_deepfm), 5)}' )<save_to_csv>
df_train.drop(['Cabin','Fare','Parch','Ticket','Name'],axis=1,inplace = True )
test_idx = test.id.values
submission = pd.DataFrame.from_dict({
'id': test_idx,
'target': y_pred_deepfm
})
 submission.to_csv('submission.csv', index=False)
 print('Submission file saved!' )<save_model>
df_test.drop(['Cabin','Fare','Parch','Ticket','Name'],axis=1,inplace = True )
np.save('oof_pred_deepfm.npy',oof_pred_deepfm)
 np.save('y_pred_deepfm.npy', y_pred_deepfm )<define_variables>
df_test.fillna(35,inplace=True )
path = Path('.. /input/cassava-leaf-disease-classification' )<load_from_csv>
df_train.fillna(35,inplace=True )
train_df = pd.read_csv(path/'train.csv')
 train_df = train_df[~train_df['image_id'].isin(['1562043567.jpg', '3551135685.jpg', '2252529694.jpg'])]
train_df.head()<set_options>
embark = pd.get_dummies(df_train['Embarked'],drop_first=True)
 sex = pd.get_dummies(df_train['Sex'],drop_first=True)
 train = pd.concat([df_train.drop(['Embarked','Sex'],axis=1),sex,embark],axis=1 )
item_tfms = RandomResizedCrop(460, min_scale=0.75, ratio=(1.,1.))
 batch_tfms = [*aug_transforms(size=384, max_warp=0), Normalize.from_stats(*imagenet_stats)]
bs=16<prepare_x_and_y>
embark = pd.get_dummies(df_test['Embarked'],drop_first=True)
 sex = pd.get_dummies(df_test['Sex'],drop_first=True)
 test = pd.concat([df_test.drop(['Embarked','Sex'],axis=1),sex,embark],axis=1 )
def get_x(r):
return path/'train_images'/r['image_id']
def get_y(r):
return r['label']<load_pretrained>
train = train.drop(['C','PassengerId'],axis=1)
 test = test.drop(['PassengerId'],axis=1 )
pets = DataBlock(blocks=(ImageBlock, CategoryBlock),
get_x=get_x,
get_y=get_y,
splitter=RandomSplitter(0.2, seed=42),
item_tfms=item_tfms,
batch_tfms=batch_tfms)
 dataloader = pets.dataloaders(train_df, bs=bs )<define_variables>
model = Sequential()
model.add(Dense(19,activation='relu'))
model.add(Dense(19,activation='relu'))
model.add(Dense(19,activation='relu'))
model.add(Dense(19,activation='relu'))
model.add(Dense(1))
model.compile(optimizer='rmsprop',loss='mse' )
dataloader.show_batch()<choose_model_class>
model.fit(train.drop(['Survived'],axis=1),train['Survived'],epochs=200 )
learn = cnn_learner(dataloader, resnet50,
loss_func = LabelSmoothingCrossEntropy() ,
cbs=[MixUp() ],
metrics=[error_rate,accuracy])
 learn.fine_tune(8,freeze_epochs = 2,base_lr=1e-2 )<categorify>
pred = model.predict_classes(test )
learn = learn.to_native_fp32()<load_from_csv>
idpred = []
for i in range(len(pred)) :
idpred.append(pred[i][0] )
sample_df = pd.read_csv(path/'sample_submission.csv')
 sample_df.head()<train_model>
submit = pd.concat([df_test['PassengerId'],p],axis=1 )
test_data_path = sample_df['image_id'].apply(lambda x: path/'test_images'/x)
 tst_dl = learn.dls.test_dl(test_data_path)
 predictions = learn.tta(dl = tst_dl, n=10)
<prepare_output>
submit.rename(columns={0:'Survived'},inplace = True )
<save_to_csv><EOS> 
submit.to_csv('Submission.csv',index=False )