Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<save_to_csv>
warnings.filterwarnings('ignore')
 df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )
test_img = os.listdir('.. /input/test/test')
 test_df = pd.DataFrame(test_img, columns=['id'])
 test_preds = pd.DataFrame({'imgs': test_df.id.values, 'preds': runner.callbacks[0].predictions["logits"]})
 test_preds.columns = ['id', 'has_cactus']
test_preds.to_csv('sub.csv', index=False)
 test_preds.head()<save_to_csv>
y = df_train['Survived']
test_index = df_test['PassengerId']
combine = [df_train, df_test]
runner1.infer(
 model=model,
loaders=test_loader,
callbacks=[InferCallback() ], 
)
 test_preds['has_cactus'] = runner1.callbacks[0].predictions["logits"]
test_preds.to_csv('sub1.csv', index=False )<set_options>
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int)
 freq_port = df_train.Embarked.dropna().mode() [0]
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)
 for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 print("Values in Sex:", df_train['Sex'].unique())
 print("Values in Embarked:", df_train['Embarked'].unique())
 print("Values in Title:", sorted(df_train['Title'].unique()))
%matplotlib inline 
<split>
df_train = df_train.drop(['FareBand'], axis=1)
 combine = [df_train, df_test]
train_df = pd.read_csv('.. /input/train.csv')
 train_df.head()
x_train,x_test,y_train,y_test = train_test_split(
 train_df['id'],
train_df['has_cactus'],
test_size = 0.2,
random_state = 3 )<data_type_conversions>
for dataset in combine:
dataset.loc[(dataset.Age.isnull()), 'Age'] = dataset.Age.median()
X_train = []
for images in tqdm(x_train):
img = plt.imread('.. /input/train/train/' + images)
 X_train.append(img)
 X_test = []
for images in tqdm(x_test):
img = plt.imread('.. /input/train/train/' + images)
 X_test.append(img)
 X_train = np.array(X_train)
 X_test = np.array(X_test)
 X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_train = X_train/255
X_test = X_test/255<train_model>
df_train['AgeBand'] = pd.cut(df_train['Age'], 5)
 df_train[['AgeBand', 'Survived']].groupby(['AgeBand'], as_index=False ).mean().sort_values(by='AgeBand', ascending=True )
augmentations = ImageDataGenerator(
 vertical_flip=True,
horizontal_flip=True,
zoom_range=0.1)
 augmentations.fit(X_train )<choose_model_class>
df_train = df_train.drop(['AgeBand'], axis=1)
 combine = [df_train, df_test]
df_train.head()
best_model_weights = './base.model'
checkpoint = ModelCheckpoint(
 best_model_weights,
monitor='val_loss',
verbose=1,
save_best_only=True,
mode='min',
save_weights_only=False,
period=1 
)
 earlystop = EarlyStopping(
 monitor='val_loss',
min_delta=0.001,
patience=10,
verbose=1,
mode='auto' 
)
 tensorboard = TensorBoard(
 log_dir = './logs',
histogram_freq=0,
batch_size=16,
write_graph=True,
write_grads=True,
write_images=False, 
)
 csvlogger = CSVLogger(
 filename= "training_csv.log",
separator = ",",
append = False 
)
 reduce = ReduceLROnPlateau(
 monitor='val_loss',
factor=0.5,
patience=10,
verbose=1,
mode='auto',
cooldown=1 
)
 callbacks = [checkpoint,tensorboard,csvlogger,reduce]<train_model>
for dataset in combine:
dataset['Age*Class'] = dataset.Age * dataset.Pclass
df_train.loc[:, ['Age*Class', 'Age', 'Pclass']].head(5 )
opt = SGD(lr=1e-4,momentum=0.99)
 opt1 = Adam(lr=1e-3)
 model.compile(
 loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'] 
)
 history = model.fit_generator(
 augmentations.flow(X_train,y_train,batch_size = 16),
steps_per_epoch=150,
validation_steps=150,
validation_data=(X_test,y_test),
epochs = 50,
verbose = 1,
callbacks=callbacks,
)<load_pretrained>
df_train = df_train.drop(['Name', 'Ticket', 'Cabin'], axis=1)
 df_test = df_test.drop(['Name', 'Ticket', 'Cabin'], axis=1)
 combine = [df_train, df_test]
display(combine[0].head())
 display(combine[1].head() )
show_final_history(history)
 model.load_weights(best_model_weights)
 model_json = model.to_json()
with open("model.json","w")as json_file:
json_file.write(model_json)
 model.save("model.h5")
 print("Weights Saved")
 print("JSON Saved" )<compute_train_metric>
df_train_X = combine[0].drop(["Survived", "PassengerId"], axis=1)
 train_y = combine[0]["Survived"]
test_test_X = combine[1].drop("PassengerId", axis=1 ).copy()
train_pred = model.predict(X_train, verbose= 1)
 valid_pred = model.predict(X_test, verbose= 1)
 train_acc = roc_auc_score(np.round(train_pred), y_train)
 valid_acc = roc_auc_score(np.round(valid_pred), y_test)
 confusion_matrix(np.round(valid_pred),y_test )<load_from_csv>
MLA = [
ensemble.AdaBoostClassifier() ,
ensemble.BaggingClassifier() ,
ensemble.ExtraTreesClassifier() ,
ensemble.GradientBoostingClassifier() ,
ensemble.RandomForestClassifier() ,
gaussian_process.GaussianProcessClassifier() ,
linear_model.LogisticRegressionCV() ,
linear_model.PassiveAggressiveClassifier() ,
linear_model.RidgeClassifierCV() ,
linear_model.SGDClassifier() ,
linear_model.Perceptron() ,
naive_bayes.BernoulliNB() ,
naive_bayes.GaussianNB() ,
neighbors.KNeighborsClassifier() ,
svm.SVC(probability=True),
svm.NuSVC(probability=True),
svm.LinearSVC() ,
tree.DecisionTreeClassifier() ,
tree.ExtraTreeClassifier() ,
discriminant_analysis.LinearDiscriminantAnalysis() ,
discriminant_analysis.QuadraticDiscriminantAnalysis() ,
XGBClassifier()
]
cv_split = model_selection.ShuffleSplit(n_splits = 10, test_size =.3, train_size =.6, random_state = 0)
 MLA_columns = ['MLA Name', 'MLA Parameters','MLA Train Accuracy Mean', 'MLA Test Accuracy Mean', 'MLA Test Accuracy 3*STD' ,'MLA Time']
MLA_compare = pd.DataFrame(columns = MLA_columns)
 MLA_predict = {}
row_index = 0
for alg in MLA:
MLA_name = alg.__class__.__name__
MLA_compare.loc[row_index, 'MLA Name'] = MLA_name
MLA_compare.loc[row_index, 'MLA Parameters'] = str(alg.get_params())
 cv_results = model_selection.cross_validate(alg, df_train_X, train_y, cv = cv_split, return_train_score=True)
 MLA_compare.loc[row_index, 'MLA Time'] = cv_results['fit_time'].mean()
MLA_compare.loc[row_index, 'MLA Train Accuracy Mean'] = cv_results['train_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy Mean'] = cv_results['test_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy 3*STD'] = cv_results['test_score'].std() *3
alg.fit(df_train_X, train_y)
 MLA_compare.loc[row_index, 'F1 Score'] = metrics.f1_score(train_y, alg.predict(df_train_X))
MLA_predict[MLA_name] = alg.predict(test_test_X)
 row_index+=1
MLA_compare.sort_values(by = ['F1 Score'], ascending = False, inplace = True )
sample = pd.read_csv('.. /input/sample_submission.csv')
 test = []
for images in tqdm(sample['id']):
img = plt.imread('.. /input/test/test/' + images)
 test.append(img)
 test = np.array(test )<save_to_csv>
best_model = MLA_compare.loc[MLA_compare['F1 Score'].idxmax() ]['MLA Name']
best_model_score = round(MLA_compare.loc[MLA_compare['F1 Score'].idxmax() ]['F1 Score'],3)
 print("Best model:",best_model)
 print("F1 Score:",best_model_score )
<set_options><EOS> 
predictions = MLA_predict[best_model]
predictions = predictions.ravel()
data_to_submit = pd.DataFrame({
'PassengerId': test_index,
'Survived': predictions
})
 data_to_submit.to_csv("results.csv", index=False )