Kaggle competition: Aerial Cactus Identification
def make_deep_model(num_features):
num_input_features = num_features
num_hidden_neurons = 13
deep_model = tf.keras.models.Sequential([
tf.keras.layers.Dense(num_input_features, activation='relu'),
tf.keras.layers.Dense(num_hidden_neurons, activation='sigmoid'),
tf.keras.layers.Dropout(rate=0.2, seed=0),
tf.keras.layers.Dense(2, activation='softmax')
 ])
 deep_model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 return deep_model<choose_model_class>
!pip install -q efficientnet_pytorch
deep_model = make_deep_model(num_features=X_train_standardized.shape[1] )<train_model>
from efficientnet_pytorch import EfficientNet
deep_model_history = deep_model.fit(x=X_train_standardized, y=y_train, epochs=40, verbose=0, validation_split=.1 )<predict_on_test>
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()
deep_probas = deep_model.predict(X_test_standardized)[:, 1]
deep_predictions = deep_probas.copy()
deep_predictions[deep_predictions<.5] = 0
deep_predictions[deep_predictions>=.5] = 1
deep_predictions=deep_predictions.astype('int')
 deep_accuracy = deep_model.evaluate(x=X_test_standardized, y=y_test, verbose=0)[1]
deep_crossvalscores = deep_val_acc[-10] 
<save_to_csv>
print('Make sure cuda is installed:', torch.cuda.is_available())
 print('Make sure cudnn is enabled:', torch.backends.cudnn.enabled )
write_model_results('DeepModel', deep_accuracy, deep_crossvalscores, deep_predictions, y_test )<prepare_x_and_y>
hack_path = Path('.. /input' )
train['PctLived'] = train_copy.PctLived
X_train, y_train = train.loc[:, train.columns!='Survived'], train.loc[:,'Survived']
feature_importances = drop_column_feature_importances(X_train, y_train )<predict_on_test>
train_df = pd.read_csv(hack_path/'train.csv')
 test_df = pd.read_csv(hack_path/'sample_submission.csv' )
allmodels_predictions = [adaboost_predictions, logit_predictions, randomforest_predictions, deep_predictions]
ada_rf_deep_predictions = [adaboost_predictions, randomforest_predictions, deep_predictions]
all_no_deep_predictions = [adaboost_predictions, logit_predictions, randomforest_predictions]
voted_allmodels_predictions = create_voted_predictions(allmodels_predictions)
 voted_ada_rf_deep_predictions = create_voted_predictions(ada_rf_deep_predictions)
 voted_all_no_deep_predictions = create_voted_predictions(all_no_deep_predictions )<feature_engineering>
def get_data(bs, size):
data = ImageDataBunch.from_df(df=train_df, path=hack_path/'train', folder='train',
bs=bs, size=size, valid_pct=0.1,
resize_method=ResizeMethod.SQUISH,
ds_tfms=get_transforms(do_flip=True, flip_vert=True,
max_lighting=0.2, max_zoom=1.1,
max_warp=0.2, max_rotate=10))
test_data = ImageList.from_df(test_df, path=hack_path/'test', folder='test')
 data.add_test(test_data)
 data.normalize(imagenet_stats)
 return data
voted_allmodels_accuracy = len(np.where(voted_allmodels_predictions==y_test)[0])/y_test.size
voted_ada_rf_deep_accuracy = len(np.where(voted_ada_rf_deep_predictions==y_test)[0])/y_test.size
voted_all_no_deep_accuracy = len(np.where(voted_all_no_deep_predictions==y_test)[0])/y_test.size<save_to_csv>
data = get_data(bs=64, size=128 )
write_model_results('Voted_AllModels', voted_allmodels_accuracy, np.array(0), voted_allmodels_predictions, y_test)
 write_model_results('Voted_Ada_Rf_Deep', voted_ada_rf_deep_accuracy, np.array(0), voted_ada_rf_deep_predictions, y_test)
 write_model_results('Voted_All_No_Deep', voted_all_no_deep_accuracy, np.array(0), voted_all_no_deep_predictions, y_test)
 model_results.sort_values('TestAccuracy', ascending=False ).style.format({
'Crossval':'{:.1%}', 'CV_std':'{:.1%}', 'TestAccuracy':'{:.1%}',
'ErrorPct':'{:.1%}', 'Type1Pct':'{:.1%}', 'Type2Pct':'{:.1%}'} )<compute_train_metric>
model_name = 'efficientnet-b3'
del model_results
model_successes = np.zeros(len(X_full))
model_results_initial_features_no_pct_lived = cross_validate_entire_process(k=10)
 model_results_initial_features_no_pct_lived<compute_train_metric>
def get_model(pretrained=True, **kwargs):
model = EfficientNet.from_pretrained(model_name)
 model._fc = nn.Linear(model._fc.in_features, data.c)
 return model
del model_results
model_successes = np.zeros(len(X_full))
model_results_initial_features_with_pct_lived = cross_validate_entire_process(k=10,ticket_survival_feature=True)
 model_results_initial_features_with_pct_lived<data_type_conversions>
learn = Learner(data, get_model() ,
metrics=[AUROC() , FBeta() , accuracy],
callback_fns=[partial(SaveModelCallback)],
wd=0.1,
path = '.' )
train_survived, test_survived = get_ticket_survival_arrays()
train_copy['PctLived'] = train_survived
train_copy['model_successes'] = model_successes
train_copy.loc[:, 'model_successes'] = train_copy.loc[:, 'model_successes'].astype('int' )<feature_engineering>
min_grad_lr = learn.recorder.min_grad_lr
min_grad_lr
test['CabinLetter'] = test['Cabin'].fillna('X' ).apply(lambda x:x[0] )<feature_engineering>
learn.fit_one_cycle(10, slice(min_grad_lr))
test['Title']=test['Name'].str.extract(r'^.+,\s (.{0,12}\.) ',expand=False)
 test.loc[test.Title.isin(['Mlle.','Ms.']),'Title'] = 'Miss.'
test.loc[test.Title.isin(['Capt.','Col.','Jonkheer.','Major.']),'Title'] = 'Officer.'
test.loc[test.Title.isin(['Lady.','Sir.','the Countess.']),'Title'] = 'Aristocrat.'
test.loc[test.Title.isin(['Mme.','Dona.']),'Title'] = 'Mrs.'
test.loc[test.Title.isin(['Don.']),'Title'] = 'Mr.'<randomize_order>
validation = learn.validate()
print("Final model validation loss: {0}".format(validation[0]))
for title in test['Title'].unique() :
nans = test.loc[(test['Title']==title)&(test['Age'].isna())]
non_nan_sample = train_ages.loc[train_ages['Title']==title,'Age'].dropna().sample(n=len(nans), \
replace=False, \
random_state=0 ).values
test.loc[nans.index,'Age'] = non_nan_sample<categorify>
learn.save('efficientnet-cactus', return_path=True )
test.loc[:, 'AgeGroup'] = test.loc[:, 'Age'].transform(code_age_group )<feature_engineering>
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )
test['Fare']=test['Fare'].fillna(0 )<feature_engineering>
probability, _ = learn.TTA(ds_type=DatasetType.Test )
test['Embarked']=test['Embarked'].fillna('S' )<data_type_conversions>
probability.argmin(dim=1)[:10]
test['TicketStub']=test['Ticket'].str.extract(r'([A-Za-z///.]+)',expand=False)
 test['TicketStub']=test['TicketStub'].fillna('numeric:'+ test['Ticket'].str.len().astype('str'))
test['TicketStub']=test['TicketStub'].str.replace('.','' ).str.upper()
test.loc[test.groupby('TicketStub')['PassengerId'].transform(len)<=5,'TicketStub']='OTHER'<categorify>
test_df.has_cactus = probability.numpy() [:, 0]
<feature_engineering><EOS> 
test_df.to_csv('submission.csv', index=False )