Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
%matplotlib inline
warnings.filterwarnings('ignore' )
model.fit(
 get_training_dataset(do_aug=DO_AUG), steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
callbacks=[es_acc, epoch_cb, lr_schedule], verbose=1
)<compute_test_metric>
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 train.describe(include="all" )
h = model.history
plt_acc(h)
 plt_loss(h )<feature_engineering>
ids = test['PassengerId']
POST_TRAINING_TIME_START = datetime.now()<predict_on_test>
Cat_Features = ['Sex', 'Pclass', 'SibSp', 'Parch', 'Embarked']
def test(tta=None):
test_ds = get_test_dataset(ordered=True, tta=tta)
 print(f'Computing predictions for TTA {tta}...')
 test_images_ds = test_ds.map(lambda iw, filename: [iw])
 model_pred = model.predict(test_images_ds)
 return model_pred<categorify>
print(pd.isnull(train ).sum() )
model_pred = test(tta=None)
 model_pred_tta_0 = test(tta=0)
 model_pred_tta_1 = test(tta=1)
 model_pred_tta_2 = test(tta=2 )<prepare_output>
all_data = pd.concat([train.drop(columns='Survived'), test], ignore_index=True)
 print(all_data.shape )
pred_plain = np.argmax(model_pred, axis=-1)
 test_ds = get_test_dataset(ordered=True)
 test_ids_ds = test_ds.map(lambda iw, filename: filename ).unbatch()
test_ids = next(iter(test_ids_ds.batch(pred_plain.shape[0])) ).numpy().astype('U')
 df_submission = pd.DataFrame({'filename': test_ids, 'category': pred_plain})
 df_submission = df_submission.drop_duplicates()
df_submission['category'] = df_submission['category'].apply(lambda c: str(c ).zfill(2))
df_submission<save_to_csv>
for feature in ['PassengerId','Cabin', 'Ticket']:
all_data.drop(feature, axis = 1, inplace=True)
 all_data.head()
df_submission.to_csv('submission.csv', index=False)
 !head submission.csv<feature_engineering>
all_data['Embarked'].fillna(all_data['Embarked'].mode() [0], inplace = True)
 all_data['Fare'].fillna(all_data['Fare'].median() , inplace = True)
pred_tta_0 = np.mean(np.array([model_pred, model_pred_tta_0]), axis=0)
 pred_tta_0 = np.argmax(pred_tta_0, axis=-1)
 test_ds = get_test_dataset(ordered=True)
 test_ids_ds = test_ds.map(lambda iw, filename: filename ).unbatch()
test_ids = next(iter(test_ids_ds.batch(pred_tta_0.shape[0])) ).numpy().astype('U')
 df_submission_tta_0 = pd.DataFrame({'filename': test_ids, 'category': pred_tta_0})
 df_submission_tta_0 = df_submission_tta_0.drop_duplicates()
df_submission_tta_0['category'] = df_submission_tta_0['category'].apply(lambda c: str(c ).zfill(2))
df_submission_tta_0<save_to_csv>
all_data['Title'] = all_data.Name.str.extract('([A-Za-z]+)\.', expand=False)
 all_data['Title'].value_counts()
df_submission_tta_0.to_csv('submission_tta_0.csv', index=False)
 !head submission_tta_0.csv<feature_engineering>
frequent_titles = all_data['Title'].value_counts() [:5].index.tolist()
frequent_titles
pred_tta_all = np.mean(np.array([model_pred, model_pred_tta_0, model_pred_tta_1, model_pred_tta_2]), axis=0)
 pred_tta_all = np.argmax(pred_tta_all, axis=-1)
 test_ds = get_test_dataset(ordered=True)
 test_ids_ds = test_ds.map(lambda iw, filename: filename ).unbatch()
test_ids = next(iter(test_ids_ds.batch(pred_tta_all.shape[0])) ).numpy().astype('U')
 df_submission_tta_all = pd.DataFrame({'filename': test_ids, 'category': pred_tta_all})
 df_submission_tta_all = df_submission.drop_duplicates()
df_submission_tta_all['category'] = df_submission_tta_all['category'].apply(lambda c: str(c ).zfill(2))
df_submission_tta_all<save_to_csv>
all_data['Title'] = all_data['Title'].apply(lambda x: x if x in frequent_titles else 'Other')
df_submission_tta_all.to_csv('submission_tta_all.csv', index=False)
 !head submission_tta_all.csv<load_pretrained>
median_ages = {}
for title in frequent_titles:
median_ages[title] = all_data.loc[all_data['Title'] == title]['Age'].median()
median_ages['Other'] = all_data['Age'].median()
all_data.loc[all_data['Age'].isnull() , 'Age'] = all_data[all_data['Age'].isnull() ]['Title'].map(median_ages )
model.save('model.h5')
 model.save_weights('model_weights.h5' )<train_model>
num_bins = 5
feature = 'Fare'
labels = [feature + '_' + str(i + 1)for i in range(num_bins)]
all_data['FareBin'] = pd.qcut(all_data['Fare'], num_bins, labels )
print(f'Post training time : {(datetime.now() - POST_TRAINING_TIME_START ).total_seconds() } seconds' )<define_variables>
used_features = ['Age', 'Fare','Name']
for feature in used_features:
all_data.drop(feature, axis = 1, inplace=True )
numeric_features_train = list(train.select_dtypes(exclude =
['object', 'category'] ).columns)
 numeric_features_test = list(test.select_dtypes(exclude =
['object', 'category'] ).columns)
 print("numeric_features_train = ", numeric_features_train)
 print("Len = ", len(numeric_features_train))
print("
numeric_features_test = ", numeric_features_test)
 print("Len = ", len(numeric_features_test))<count_missing_values>
all_data = pd.get_dummies(all_data)
 all_data.head()
print(train.isnull().any())
 print(test.isnull().any() )<drop_column>
target = train['Survived']
train = all_data.iloc[: len(train)]
test = all_data.iloc[len(train):]
train.shape
SP = train.pop('SalePrice' )<split>
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, random_state = 0)
 MLA_columns = ['MLA Name', 'MLA Train Accuracy Mean', 'MLA Validation Accuracy Mean', 'MLA Validation Accuracy STD']
MLA_compare = pd.DataFrame(columns = MLA_columns)
 estimators = []
for row_index, alg in enumerate(MLA):
alg_name = alg.__class__.__name__
MLA_compare.loc[row_index, 'MLA Name'] = alg_name
print(alg.__class__.__name__)
 cv_results = model_selection.cross_validate(alg, train, target, cv=cv_split, return_estimator=True, return_train_score=True)
 estimators += list(cv_results['estimator'])
 MLA_compare.loc[row_index, 'MLA Train Accuracy Mean'] = cv_results['train_score'].mean()
MLA_compare.loc[row_index, 'MLA Validation Accuracy Mean'] = cv_results['test_score'].mean()
MLA_compare.loc[row_index, 'MLA Validation Accuracy STD'] = cv_results['test_score'].std()
MLA_compare.sort_values(by = ['MLA Validation Accuracy Mean'], ascending = False, inplace = True)
 MLA_compare
def getTheta(r):
X_train, X_test , y_train, y_test = train_test_split(train_X, SP,
test_size = 0.2,
random_state = r)
 theta = np.dot(np.linalg.pinv(X_train), y_train)
 print("Training Error =",rmse(predictTarget(X_train, theta), y_train))
print("Validation Error = ", rmse(predictTarget(X_test, theta), y_test))
return theta<predict_on_test>
def hard_voting(estimators, X):
Y = np.zeros([X.shape[0], len(estimators)], dtype=int)
 for i, estimator in enumerate(estimators):
Y[:, i] = estimator.predict(X)
 y = np.zeros(X.shape[0], dtype=int)
 for i in range(X.shape[0]):
y[i] = np.argmax(np.bincount(Y[i,:]))
return y
y_predict = hard_voting(estimators, train)
 accuracy_score(target, y_predict)
<predict_on_test><EOS> 
predictions = hard_voting(estimators, test)
 output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission_voting_cross_validation.csv', index=False )