Kaggle competition: Titanic - Machine Learning from Disaster
tfidf = TfidfVectorizer(ngram_range=(1, 3))
tsvd = TruncatedSVD(n_components = 128, n_iter=5)
 tfquestion_title = tfidf.fit_transform(train["question_title"].values)
 tfquestion_title_test = tfidf.transform(test["question_title"].values)
 tfquestion_title = tsvd.fit_transform(tfquestion_title)
 tfquestion_title_test = tsvd.transform(tfquestion_title_test)
 tfquestion_body = tfidf.fit_transform(train["question_body"].values)
 tfquestion_body_test = tfidf.transform(test["question_body"].values)
 tfquestion_body = tsvd.fit_transform(tfquestion_body)
 tfquestion_body_test = tsvd.transform(tfquestion_body_test)
 tfanswer = tfidf.fit_transform(train["answer"].values)
 tfanswer_test = tfidf.transform(test["answer"].values)
 tfanswer = tsvd.fit_transform(tfanswer)
 tfanswer_test = tsvd.transform(tfanswer_test )<set_options>
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import BatchNormalization
from keras.utils import np_utils
from keras.optimizers import Adam
torch.cuda.empty_cache()<load_pretrained>
train_data=pd.read_csv('.. /input/train.csv' ).drop(columns=['PassengerId','Name','Ticket','Cabin'] )
try:
embeddings_train = load_obj(".. /input/questembeddings/use_embeddings_train")
 embeddings_test = load_obj(".. /input/questembeddings/use_embeddings_test")
 except:
print("Load failed, build embedding")
 try:
module_url = INPUT_PATH+'universalsentenceencoderlarge4/'
embed = hub.load(module_url)
 def UniversalEmbedding(x):
results = embed(tf.squeeze(tf.cast(x, tf.string)))["outputs"]
return keras.backend.concatenate([results])
 except:
module_url = INPUT_PATH+'universalsentenceencoderlarge3/'
embed = hub.Module(module_url)
 def UniversalEmbedding(x):
results = embed(tf.squeeze(tf.cast(x, tf.string)) , signature="default", as_dict=True)["default"]
return keras.backend.concatenate([results])
 embeddings_train = {}
embeddings_test = {}
for text in input_columns:
print(text)
 train_text = train[text].str.replace('?', '.' ).str.replace('!', '.' ).tolist()
test_text = test[text].str.replace('?', '.' ).str.replace('!', '.' ).tolist()
curr_train_emb = []
curr_test_emb = []
batch_size = 4
ind = 0
while ind*batch_size < len(train_text):
curr_train_emb.append(embed(train_text[ind*batch_size:(ind + 1)*batch_size])["outputs"].numpy())
 ind += 1
ind = 0
while ind*batch_size < len(test_text):
curr_test_emb.append(embed(test_text[ind*batch_size:(ind + 1)*batch_size])["outputs"].numpy())
 ind += 1
embeddings_train[text + '_embedding'] = np.vstack(curr_train_emb)
 embeddings_test[text + '_embedding'] = np.vstack(curr_test_emb)
 del embed
K.clear_session()
gc.collect() 
<feature_engineering>
sum(train_data['Fare']==0 )
find = re.compile(r"^[^.]*")
 train['netloc'] = train['url'].apply(lambda x: re.findall(find, urlparse(x ).netloc)[0])
 test['netloc'] = test['url'].apply(lambda x: re.findall(find, urlparse(x ).netloc)[0])
 features_lrg = ['category', 'netloc', 'question_user_name','answer_user_name','host']
features_sml = ['category', 'netloc', 'host']
features_train = []
features_test = []
for feature in features_sml:
merged = pd.concat([train[feature], test[feature]] ).to_numpy().reshape(-1, 1 ).squeeze()
encoded_size = len(max(merged, key=len))
print(feature,encoded_size)
 ctr = 0
column = []
for val in train[feature].values:
ctr +=1
output = np.zeros(encoded_size)
 output[:len(val)] = [ord(i)/128.for i in val]
column.append(output)
 features_train.append(column)
 column = []
for val in test[feature].values:
output = np.zeros(encoded_size)
 output[:len(val)] = [ord(i)/128.for i in val]
column.append(output)
 features_test.append(column)
 features_train_lrg = []
features_test_lrg = []
for feature in features_lrg:
merged = pd.concat([train[feature], test[feature]] ).to_numpy().reshape(-1, 1 ).squeeze()
encoded_size = len(max(merged, key=len))
print(feature,encoded_size)
 ctr = 0
column = []
for val in train[feature].values:
ctr +=1
output = np.zeros(encoded_size)
 output[:len(val)] = [ord(i)/128.for i in val]
column.append(output)
 features_train.append(column)
 column = []
for val in test[feature].values:
output = np.zeros(encoded_size)
 output[:len(val)] = [ord(i)/128.for i in val]
column.append(output)
 features_test.append(column)
 l2_dist = lambda x, y: np.power(x - y, 2 ).sum(axis=1)
 cos_dist = lambda x, y:(x*y ).sum(axis=1)
 abs_dist = lambda x, y: np.abs(x-y ).sum(axis=1)
 sum_dist = lambda x, y:(x+y ).sum(axis=1)
 dist_features_train = np.array([
l2_dist(embeddings_train['question_title_embedding'], embeddings_train['answer_embedding']),
l2_dist(embeddings_train['question_body_embedding'], embeddings_train['answer_embedding']),
l2_dist(embeddings_train['question_body_embedding'], embeddings_train['question_title_embedding']),
l2_dist(np.array([x for x in train.question_body_vec.values]), np.array([x for x in train.answer_vec.values])) ,
l2_dist(np.array([x for x in train.question_title_vec.values]), np.array([x for x in train.answer_vec.values])) ,
l2_dist(np.array([x for x in train.question_body_vec.values]), np.array([x for x in train.question_title_vec.values])) ,
cos_dist(embeddings_train['question_title_embedding'], embeddings_train['answer_embedding']),
cos_dist(embeddings_train['question_body_embedding'], embeddings_train['answer_embedding']),
cos_dist(embeddings_train['question_body_embedding'], embeddings_train['question_title_embedding']),
cos_dist(np.array([x for x in train.question_body_vec.values]), np.array([x for x in train.answer_vec.values])) ,
cos_dist(np.array([x for x in train.question_title_vec.values]), np.array([x for x in train.answer_vec.values])) ,
cos_dist(np.array([x for x in train.question_body_vec.values]), np.array([x for x in train.question_title_vec.values])) ,
abs_dist(embeddings_train['question_title_embedding'], embeddings_train['answer_embedding']),
abs_dist(embeddings_train['question_body_embedding'], embeddings_train['answer_embedding']),
abs_dist(embeddings_train['question_body_embedding'], embeddings_train['question_title_embedding']),
abs_dist(np.array([x for x in train.question_body_vec.values]), np.array([x for x in train.answer_vec.values])) ,
abs_dist(np.array([x for x in train.question_title_vec.values]), np.array([x for x in train.answer_vec.values])) ,
abs_dist(np.array([x for x in train.question_body_vec.values]), np.array([x for x in train.question_title_vec.values])) ,
sum_dist(embeddings_train['question_title_embedding'], embeddings_train['answer_embedding']),
sum_dist(embeddings_train['question_body_embedding'], embeddings_train['answer_embedding']),
sum_dist(embeddings_train['question_body_embedding'], embeddings_train['question_title_embedding']),
sum_dist(np.array([x for x in train.question_body_vec.values]), np.array([x for x in train.answer_vec.values])) ,
sum_dist(np.array([x for x in train.question_title_vec.values]), np.array([x for x in train.answer_vec.values])) ,
sum_dist(np.array([x for x in train.question_body_vec.values]), np.array([x for x in train.question_title_vec.values])) ,
l2_dist(train_question_body_dense, train_answer_dense),
cos_dist(train_question_body_dense, train_answer_dense),
abs_dist(train_question_body_dense, train_answer_dense),
sum_dist(train_question_body_dense, train_answer_dense),
l2_dist(train_question_body_dense, train_question_title_dense),
cos_dist(train_question_body_dense, train_question_title_dense),
abs_dist(train_question_body_dense, train_question_title_dense),
sum_dist(train_question_body_dense, train_question_title_dense),
l2_dist(train_answer_dense, train_question_title_dense),
cos_dist(train_answer_dense, train_question_title_dense),
abs_dist(train_answer_dense, train_question_title_dense),
sum_dist(train_answer_dense, train_question_title_dense),
] ).T
dist_features_test = np.array([
l2_dist(embeddings_test['question_title_embedding'], embeddings_test['answer_embedding']),
l2_dist(embeddings_test['question_body_embedding'], embeddings_test['answer_embedding']),
l2_dist(embeddings_test['question_body_embedding'], embeddings_test['question_title_embedding']),
l2_dist(np.array([x for x in test.question_body_vec.values]), np.array([x for x in test.answer_vec.values])) ,
l2_dist(np.array([x for x in test.question_title_vec.values]), np.array([x for x in test.answer_vec.values])) ,
l2_dist(np.array([x for x in test.question_body_vec.values]), np.array([x for x in test.question_title_vec.values])) ,
cos_dist(embeddings_test['question_title_embedding'], embeddings_test['answer_embedding']),
cos_dist(embeddings_test['question_body_embedding'], embeddings_test['answer_embedding']),
cos_dist(embeddings_test['question_body_embedding'], embeddings_test['question_title_embedding']),
cos_dist(np.array([x for x in test.question_body_vec.values]), np.array([x for x in test.answer_vec.values])) ,
cos_dist(np.array([x for x in test.question_title_vec.values]), np.array([x for x in test.answer_vec.values])) ,
cos_dist(np.array([x for x in test.question_body_vec.values]), np.array([x for x in test.question_title_vec.values])) ,
abs_dist(embeddings_test['question_title_embedding'], embeddings_test['answer_embedding']),
abs_dist(embeddings_test['question_body_embedding'], embeddings_test['answer_embedding']),
abs_dist(embeddings_test['question_body_embedding'], embeddings_test['question_title_embedding']),
abs_dist(np.array([x for x in test.question_body_vec.values]), np.array([x for x in test.answer_vec.values])) ,
abs_dist(np.array([x for x in test.question_title_vec.values]), np.array([x for x in test.answer_vec.values])) ,
abs_dist(np.array([x for x in test.question_body_vec.values]), np.array([x for x in test.question_title_vec.values])) ,
sum_dist(embeddings_test['question_title_embedding'], embeddings_test['answer_embedding']),
sum_dist(embeddings_test['question_body_embedding'], embeddings_test['answer_embedding']),
sum_dist(embeddings_test['question_body_embedding'], embeddings_test['question_title_embedding']),
sum_dist(np.array([x for x in test.question_body_vec.values]), np.array([x for x in test.answer_vec.values])) ,
sum_dist(np.array([x for x in test.question_title_vec.values]), np.array([x for x in test.answer_vec.values])) ,
sum_dist(np.array([x for x in test.question_body_vec.values]), np.array([x for x in test.question_title_vec.values])) ,
l2_dist(test_question_body_dense, test_answer_dense),
cos_dist(test_question_body_dense, test_answer_dense),
abs_dist(test_question_body_dense, test_answer_dense),
sum_dist(test_question_body_dense, test_answer_dense),
l2_dist(test_question_body_dense, test_question_title_dense),
cos_dist(test_question_body_dense, test_question_title_dense),
abs_dist(test_question_body_dense, test_question_title_dense),
sum_dist(test_question_body_dense, test_question_title_dense),
l2_dist(test_answer_dense, test_question_title_dense),
cos_dist(test_answer_dense, test_question_title_dense),
abs_dist(test_answer_dense, test_question_title_dense),
sum_dist(test_answer_dense, test_question_title_dense),
] ).T 
<define_variables>
train_data.groupby(["Embarked", "Pclass"] ).Fare.mean()
possible_features_train = [
[item for k, item in embeddings_train.items() ],
features_train,
features_train_lrg,
[ dist_features_train ],
[ [x for x in train.question_body_vec.values] ],
[ [x for x in train.question_title_vec.values] ],
[ [x for x in train.answer_vec.values] ],
[ train_question_body_dense ],
[ train_answer_dense ],
[ tfquestion_title ],
[ tfquestion_body ],
[ tfanswer ]
]
possible_features_test = [
[item for k, item in embeddings_test.items() ],
features_test,
features_test_lrg,
[ dist_features_test ],
[ [x for x in test.question_body_vec.values] ],
[ [x for x in test.question_title_vec.values] ],
[ [x for x in test.answer_vec.values] ],
[ test_question_body_dense ],
[ test_answer_dense ],
[ tfquestion_title_test ],
[ tfquestion_body_test ],
[ tfanswer_test ]
]
def get_train_test(split=0.8):
total_len = len(possible_features_train)
 r_idx = random.sample(range(total_len), int(total_len * split))
train = [ train_question_title_dense ]
test = [ test_question_title_dense ]
for i in r_idx:
train += possible_features_train[i]
test += possible_features_test[i]
return np.hstack(train),np.hstack(test)
 X_train,X_test = get_train_test()
y_train = train[targets].values<compute_test_metric>
train_data.loc[(train_data['Pclass'] == 1)&(train_data['Fare'] == 0.0),'Fare'] = 70.36
train_data.loc[(train_data['Pclass'] == 2)&(train_data['Fare'] == 0.0),'Fare'] = 20.33
train_data.loc[(train_data['Pclass'] == 3)&(train_data['Fare'] == 0.0),'Fare'] = 14.64
def bce(t,p):
return binary_crossentropy(t,p)
 def custom_loss(true,pred):
bce = binary_crossentropy(true,pred)
 return bce + logcosh(true,pred)
 def swish(x):
return K.sigmoid(x)* x
def relu1(x):
return keras.activations.relu(x, alpha=0.0, max_value=1., threshold=0.0)
 def create_model1(X_train):
input1 = Input(shape=(X_train.shape[1],))
x = Dense(64, activation='elu',kernel_initializer='lecun_normal' )(input1)
 x = Dense(128, activation='elu',kernel_initializer='lecun_normal' )(x)
 x = Dropout(0.4 )(x)
 output = Dense(len(targets),activation='sigmoid',name='output' )(x)
 model = Model(inputs=input1, outputs=output)
 return model
def create_model2(X_train):
input1 = Input(shape=(X_train.shape[1],))
x = Dense(512, activation='elu',kernel_initializer='lecun_normal' )(input1)
 x = Dense(256, activation='elu',
kernel_initializer='lecun_normal',
)(x)
 output = Dense(len(targets),activation='sigmoid',name='output' )(x)
 model = Model(inputs=input1, outputs=output)
 return model
def create_model3(X_train):
input1 = Input(shape=(X_train.shape[1],))
x = Dense(200, activation='selu',kernel_initializer='lecun_normal' )(input1)
 x = Dense(512, activation='selu',
kernel_initializer='lecun_normal',
)(x)
 x = Dropout(0.4 )(x)
 output = Dense(len(targets),activation='sigmoid',name='output' )(x)
 model = Model(inputs=input1, outputs=output)
 return model
def create_model4(X_train):
input1 = Input(shape=(X_train.shape[1],))
x = Dense(512, activation='elu',
kernel_initializer='lecun_normal',
)(input1)
 x = Dropout(0.2 )(x)
 output = Dense(len(targets),activation='sigmoid',name='output' )(x)
 model = Model(inputs=input1, outputs=output)
 return model
def create_model5(X_train):
input1 = Input(shape=(X_train.shape[1],))
x = Dense(4096, activation='selu',kernel_initializer='lecun_normal' )(input1)
 x = Dense(512, activation='selu',
kernel_initializer='lecun_normal',
kernel_regularizer=keras.regularizers.l2(0.01)
 )(x)
 output = Dense(len(targets),activation='sigmoid',name='output' )(x)
 model = Model(inputs=input1, outputs=output)
 return model
def create_model7(X_train):
input1 = Input(shape=(X_train.shape[1],))
x = Dense(4096, activation='selu',kernel_initializer='lecun_normal' )(input1)
 x = Dense(512, activation='selu',
kernel_initializer='lecun_normal',
kernel_regularizer=keras.regularizers.l2(0.01)
 )(x)
 x = Dropout(0.2 )(x)
 output = Dense(len(targets),activation='sigmoid',name='output' )(x)
 model = Model(inputs=input1, outputs=output)
 return model
def create_model(X_train):
model = random.choice([create_model1, create_model2,create_model3, create_model4,create_model7] )(X_train)
 model.summary()
return model 
<set_options>
train_data.Age.isna().sum()
print(gc.collect() )<compute_test_metric>
means = train_data.groupby(['Sex', 'Pclass'] ).Age.mean()
train_data.Age = train_data.apply(lambda x: means[x.Sex][x.Pclass] if pd.isnull(x.Age)else x.Age, axis=1 )
def pearson_metric(y_true, y_pred):
y_true = K.clip(y_true, K.epsilon() , 1)
 y_pred = K.clip(y_pred, K.epsilon() , 1)
 y_true -= K.mean(y_true)
 y_pred -= K.mean(y_pred)
 y_true = K.l2_normalize(y_true, axis=-1)
 y_pred = K.l2_normalize(y_pred, axis=-1)
 pearson_correlation = K.sum(y_true * y_pred, axis=-1)
 return 1-pearson_correlation 
<prepare_x_and_y>
train_data['Sex'] = pd.Categorical(train_data['Sex'])
 dfDummies = pd.get_dummies(train_data['Sex'], prefix = 'category')
 train_data = pd.concat([train_data.drop(columns=['Sex']), dfDummies], axis=1)
 train_data['Pclass'] = pd.Categorical(train_data['Pclass'])
 dfDummies = pd.get_dummies(train_data['Pclass'], prefix = 'category')
 train_data = pd.concat([train_data.drop(columns=['Pclass']), dfDummies], axis=1)
 train_data['Embarked'] = pd.Categorical(train_data['Embarked'])
 dfDummies = pd.get_dummies(train_data['Embarked'], prefix = 'category')
 train_data = pd.concat([train_data.drop(columns=['Embarked']), dfDummies], axis=1 )
error_pred_y = None
error_y = None
class SpearmanRhoCallback(Callback):
def __init__(self, training_data, validation_data, patience, model_name, reload=False):
global noise
self.x = training_data[0]
self.y = training_data[1]
self.x_val = validation_data[0]
self.y_val = validation_data[1]
self.patience = patience
self.value = -1
self.bad_epochs = 0
self.model_name = model_name
self.reload = reload
def on_train_begin(self, logs={}):
return
def on_train_end(self, logs={}):
y_pred_val = self.model.predict(self.x_val)
 noise = np.random.normal(0, 1e-7, y_pred_val.shape[0])
 rho_val = np.mean([spearmanr(self.y_val[:, ind] + noise, y_pred_val[:, ind] + noise ).correlation for ind in range(y_pred_val.shape[1])])
 print('\r val_spearman-rho: %s' %(str(round(rho_val, 4))), end=100*' '+'
')
 if rho_val >= self.value:
self.model.save_weights(self.model_name)
 self.value = rho_val
else:
self.model.load_weights(self.model_name)
 return
def on_epoch_begin(self, epoch, logs={}):
return
def on_epoch_end(self, epoch, logs={}):
global error_pred_y, error_y
y_pred_val = self.model.predict(self.x_val)
 noise = np.random.normal(0, 1e-7, y_pred_val.shape[0])
 rho_val = np.mean([spearmanr(self.y_val[:, ind] + noise, y_pred_val[:, ind] + noise ).correlation for ind in range(y_pred_val.shape[1])])
 if np.isnan(rho_val):
print("Error")
 error_pred_y = y_pred_val
error_y = self.y_val
print(y_pred_val)
 print(self.x_val)
 print(self.y_val)
 raise "bogus error"
if rho_val >= self.value:
self.model.save_weights(self.model_name)
 self.value = rho_val
else:
self.bad_epochs += 1
print('\r val_spearman-rho: %s' %(str(round(rho_val, 4))), end=100*' '+'
')
 ')
 if self.reload:
print(' reload best: %s' %(str(round(self.value, 4))))
self.model.load_weights(self.model_name)
 return rho_val
def on_batch_begin(self, batch, logs={}):
return
def on_batch_end(self, batch, logs={}):
return<train_model>
for i in range(len(train_data)) :
if train_data.loc[i, "SibSp"] + train_data.loc[i, "Parch"] == 0:
train_data.loc[i, "Alone"] = 1
else:
train_data.loc[i, "Alone"] = 0
train_data.Alone = train_data.Alone.astype(int )
all_predictions = []
model_idx =0
def run_model() :
global y_train,all_predictions, model_idx
X_train,X_test = get_train_test()
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1,
patience=7, min_lr=1e-6, verbose=1)
 early_stop = EarlyStopping(monitor='val_loss',
min_delta=0,
patience=15,
mode='auto')
 kf = KFold(n_splits=5, random_state=random.randint(0,1000), shuffle=True)
 for ind,(tr, val)in enumerate(kf.split(X_train)) :
X_tr = X_train[tr]
y_tr = y_train[tr]
X_vl = X_train[val]
y_vl = y_train[val]
model_idx+=1
model = create_model(X_train)
 optimizer = Adam(lr=5e-4,clipnorm=1.4)
 model.compile(optimizer=optimizer, loss=custom_loss, metrics=[bce,logcosh])
 model.fit(
 X_tr, y_tr,
epochs=100, batch_size=64,
validation_data=(X_vl, y_vl),
verbose=True,
callbacks=[SpearmanRhoCallback(training_data=(X_tr, y_tr),
validation_data=(X_vl, y_vl),
patience=10,
model_name=f'best_model_batch{model_idx}.h5',
reload=True),
reduce_lr,
early_stop
] 
)
 optimizer = SGD(lr=0.01,clipnorm=1.1)
 model.compile(optimizer=optimizer, loss=custom_loss, metrics=[bce,logcosh])
 history = model.fit(
 X_tr, y_tr,
epochs=50, batch_size=64,
validation_data=(X_vl, y_vl),
verbose=True,
callbacks=[SpearmanRhoCallback(training_data=(X_tr, y_tr),
validation_data=(X_vl, y_vl),
patience=10,
model_name=f'best_model_batch_sgd{model_idx}.h5'),
reduce_lr,
early_stop
] 
)
 if min(history.history['val_bce'])< 0.375:
all_predictions.append(model.predict(X_test))
optimizer = Adam(lr=2e-4,clipnorm=1.9)
 model.compile(optimizer=optimizer, loss=bce)
 model.fit(
 X_train, y_train,
epochs=30, batch_size=128,
validation_data=(X_train, y_train),
verbose=True,
callbacks=[SpearmanRhoCallback(training_data=(X_train, y_train),
validation_data=(X_train, y_train),
patience=10,
model_name=f'best_model_batchoverfit{model_idx}.h5',
reload=False),
reduce_lr,
early_stop
] 
)
 all_predictions.append(model.predict(X_test)) 
<define_variables>
features = ['Age','SibSp','Parch','Fare','category_female','category_male','category_1','category_2','category_3','category_C','category_Q','category_S','Alone']
all_predictions = []
while len(all_predictions)< 20:
run_model() 
<set_options>
y = train_data['Survived']
x = train_data.drop(columns=['Survived'] )
K.clear_session()
gc.collect()<prepare_output>
scaler = MinMaxScaler()
test_preds = np.array([np.array([rankdata(c)for c in p.T] ).T for p in all_predictions] ).mean(axis=0)
 max_val = test_preds.max() + 1
test_preds = test_preds/max_val + 1e-12<load_from_csv>
scaler.fit(x )
submission = pd.read_csv(INPUT_PATH+'google-quest-challenge/sample_submission.csv')
 submission[targets] = test_preds
submission.head(20 )<save_to_csv>
x = scaler.transform(x )
submission.to_csv("submission.csv", index = False)
<import_modules>
x = pd.DataFrame(x, columns=features )
import numpy as np
import pandas as pd
from fastai import *
from fastai.vision import *<load_from_csv>
model = Sequential()
model.add(Dense(64, input_shape=(13,), activation='sigmoid'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(64, activation='sigmoid'))
model.add(Dense(1, activation="sigmoid"))
model.compile(optimizer="adadelta", loss='binary_crossentropy', metrics=["binary_accuracy"])
data_folder = Path(".. /input/aerial-cactus-identification")
 train_df = pd.read_csv(".. /input/aerial-cactus-identification/train.csv")
 test_df = pd.read_csv(".. /input/aerial-cactus-identification/sample_submission.csv")
 test_img = ImageList.from_df(test_df, path=data_folder/'test', folder='test')
 trfm = get_transforms(do_flip=True, flip_vert=True, max_rotate=10.0, max_zoom=1.1, max_lighting=0.2, max_warp=0.2, p_affine=0.75, p_lighting=0.75)
 train_img =(ImageList.from_df(train_df, path=data_folder/'train', folder='train')
.split_by_rand_pct(0.01)
.label_from_df() 
.add_test(test_img)
.transform(trfm, size=128)
.databunch(bs=64, device= torch.device('cuda:0')) 
.normalize(imagenet_stats)
 )<choose_model_class>
model_result = model.fit(x, y, batch_size=100, epochs=200, validation_split= 0.2, shuffle = True )
learn = cnn_learner(train_img, models.resnet18, metrics=[error_rate, accuracy])
<train_model>
print("<-------Final Metrics------->")
 print("Loss = ",model_result.history['val_loss'][199])
 print("Accuracy = ",model_result.history['val_binary_accuracy'][199] )
lr = 3e-02
learn.fit_one_cycle(5, slice(lr))<save_to_csv>
test=pd.read_csv('.. /input/test.csv')
 test_data=pd.read_csv('.. /input/test.csv' ).drop(columns=['PassengerId','Name','Ticket','Cabin'] )
preds,_ = learn.get_preds(ds_type=DatasetType.Test)
 test_df.has_cactus = preds.numpy() [:, 0]
test_df.to_csv('submission.csv', index=False )<import_modules>
sum(test_data['Fare']==0 )
FileLink('submission.csv' )<load_from_csv>
test_data.groupby(["Embarked", "Pclass"] ).Fare.mean()
train = pd.read_csv('.. /input/train.csv')
 test_file = pd.read_csv('.. /input/sample_submission.csv')
<choose_model_class>
test_data.loc[(test_data['Pclass'] == 1)&(test_data['Fare'] == 0.0),'Fare'] = 76.68
test_data.loc[(test_data['Pclass'] == 2)&(test_data['Fare'] == 0.0),'Fare'] = 23.06
test_data.loc[(test_data['Pclass'] == 3)&(test_data['Fare'] == 0.0),'Fare'] = 13.91
learn = cnn_learner(train_img , models.densenet201, metrics = [error_rate,accuracy] )<find_best_params>
test_data.Age.isna().sum()
learn.lr_find() 
<train_model>
means = test_data.groupby(['Sex', 'Pclass'] ).Age.mean()
test_data.Age = test_data.apply(lambda x: means[x.Sex][x.Pclass] if pd.isnull(x.Age)else x.Age, axis=1 )
alpha = 3e-02
learn.fit_one_cycle(5,slice(alpha))<predict_on_test>
test_data['Sex'] = pd.Categorical(test_data['Sex'])
 dfDummies = pd.get_dummies(test_data['Sex'], prefix = 'category')
 test_data = pd.concat([test_data.drop(columns=['Sex']), dfDummies], axis=1)
 test_data['Pclass'] = pd.Categorical(test_data['Pclass'])
 dfDummies = pd.get_dummies(test_data['Pclass'], prefix = 'category')
 test_data = pd.concat([test_data.drop(columns=['Pclass']), dfDummies], axis=1)
 test_data['Embarked'] = pd.Categorical(test_data['Embarked'])
 dfDummies = pd.get_dummies(test_data['Embarked'], prefix = 'category')
 test_data = pd.concat([test_data.drop(columns=['Embarked']), dfDummies], axis=1 )
preds,_ = learn.get_preds(ds_type = DatasetType.Test)
<prepare_output>
for i in range(len(test_data)) :
if test_data.loc[i, "SibSp"] + test_data.loc[i, "Parch"] == 0:
test_data.loc[i, "Alone"] = 1
else:
test_data.loc[i, "Alone"] = 0
test_data.Alone = test_data.Alone.astype(int )
test_file.has_cactus = preds.numpy() [:,0]<save_to_csv>
scaler.fit(test_data )
test_file.to_csv('submission.csv',index=False )<load_from_csv>
test_data = scaler.transform(test_data )
train_dir = '.. /input/train/train/'
test_dir = '.. /input/test/test/'
train_df = pd.read_csv('.. /input/train.csv')
 test_images = os.listdir(test_dir)
 test_df = pd.DataFrame(
 list(zip(test_images, [0] * len(test_images))),
columns=['id', 'has_cactus'] 
)
<split>
test_data = pd.DataFrame(test_data, columns=features )
X = train_df.id
y = train_df.has_cactus
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)
 train_gen_df = pd.concat([X_train, y_train], axis=1 ).reset_index(drop=True)
 valid_gen_df = pd.concat([X_test, y_test], axis=1 ).reset_index(drop=True )<prepare_x_and_y>
predict = model.predict_classes(test_data )
<choose_model_class><EOS> 
my_submission = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predict})
 my_submission.to_csv('submission.csv', index=False)
 print(my_submission )