Kaggle competition: Titanic - Machine Learning from Disaster
cases = train.ConfirmedCases
fatalities = train.Fatalities
del train['ConfirmedCases']
del train['Fatalities']<categorify>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
lb = LabelEncoder()
train['Country_Region'] = lb.fit_transform(train['Country_Region'])
 test['Country_Region'] = lb.transform(test['Country_Region'] )<normalization>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
scaler = MinMaxScaler()
x_train = scaler.fit_transform(train.values)
 x_test = scaler.transform(test.values )<train_model>
def generateBaselineOutputNobodySurvives(df):
output = pd.DataFrame({'PassengerId': df.PassengerId, 'Survived': np.full(( len(df)) , 0)})
 return output
output_train = generateBaselineOutputNobodySurvives(train_data)
 output_test = generateBaselineOutputNobodySurvives(test_data )
rf = XGBRegressor(n_estimators = 2500 , random_state = 0 , max_depth = 27)
 rf.fit(x_train,cases )<predict_on_test>
print("MSE baseline: ", mean_squared_error(train_data["Survived"], output_train["Survived"]))
print("LogLoss baseline", log_loss(train_data["Survived"], output_train["Survived"]))
cases_pred = rf.predict(x_test)
 cases_pred<feature_engineering>
output_test.to_csv('baseline_submission.csv', index=False)
 print("Your baseline submission was successfully saved!" )
cases_pred = np.around(cases_pred,decimals = 0)
 cases_pred<concatenate>
train_data_copy = train_data.copy()
test_data_copy = test_data.copy()
median_age = train_data["Age"].median()
train_data["Age"] = train_data["Age"].replace(np.nan, median_age)
 train_data["Embarked"] = train_data["Embarked"].replace(np.nan, 'S')
 for feature in ['PassengerId', 'Name','Cabin', 'Ticket']:
train_data.drop(feature, axis=1, inplace=True)
 for feature in ['Fare', 'Pclass', 'SibSp', 'Parch', 'Age']:
train_data[feature] = stats.zscore(train_data[feature])
 for feature in ['Sex', 'Embarked']:
one_hot = pd.get_dummies(train_data[feature])
 train_data = train_data.drop(feature,axis = 1)
 train_data = train_data.join(one_hot )
x_train_cas = []
for i in range(len(x_train)) :
x = list(x_train[i])
 x.append(cases[i])
 x_train_cas.append(x)
 x_train_cas[0]<prepare_x_and_y>
Y_train = pd.DataFrame(train_data["Survived"] ).to_numpy().flatten()
train_data.drop("Survived", axis=1, inplace=True)
 X_train = train_data.to_numpy()
x_train_cas = np.array(x_train_cas )<train_model>
def build_model(input_shape, learning_rate=0.01):

 tf.keras.backend.clear_session()
np.random.seed(0)
 tf.compat.v1.set_random_seed(0)
 model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=input_shape))
model.add(keras.layers.Dense(
 units=512,
use_bias=True,
activation="relu", 
))
model.add(keras.layers.Dense(
 units=256,
use_bias=True,
activation="relu", 
))
model.add(keras.layers.Dense(
 units=128,
use_bias=True,
activation="relu", 
))
model.add(keras.layers.Dense(
 units=1,
use_bias=True,
activation="sigmoid" 
))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=[metrics.binary_accuracy])
 return model
rf = XGBRegressor(n_estimators = 2500 , random_state = 0 , max_depth = 27)
 rf.fit(x_train_cas,fatalities )<concatenate>
model = build_model(input_shape=X_train[0].shape, learning_rate=0.01)
 history = model.fit(
 x = X_train,
y = Y_train,
epochs=5,
batch_size=64,
validation_split=0.1,
verbose=1 
)
 history = pd.DataFrame(history.history)
 display(history)
 plot_history(history )
x_test_cas = []
for i in range(len(x_test)) :
x = list(x_test[i])
 x.append(cases_pred[i])
 x_test_cas.append(x)
 x_test_cas[0]<predict_on_test>
train_predictions = model.predict(X_train ).flatten()
thresholds = [0.3,0.49, 0.5, 0.51, 0.52, 0.7]
group_names = ["True Neg", "False Pos", "False Neg" , "True Pos"]
for threshold in thresholds:
train_predictions_copy = np.copy(train_predictions)
 train_predictions_copy[train_predictions < threshold] = 0.0
train_predictions_copy[train_predictions >= threshold] = 1.0
cf_matrix = confusion_matrix(Y_train, train_predictions_copy)
 tn, fp, fn, tp = cf_matrix.ravel()
group_counts = ["{0:0.0f}".format(value)for value in
cf_matrix.flatten() ]
group_percentages = ["{0:.2%}".format(value)for value in
cf_matrix.flatten() /np.sum(cf_matrix)]
labels = [f"{v1}
{v2}
{v3}" for v1, v2, v3 in
zip(group_names,group_counts,group_percentages)]
labels = np.asarray(labels ).reshape(2,2)
 plt.figure()
plt.title(
 "Threshold : " + str(threshold)+ "
"
+ "Accuracy : " + str(( tp+tn)/(tp+tn+fp+fn)) + "
"
+ "Precision : " + str(( tp)/(tp+fp)) + "
"
+ "Recall : " + str(( tp)/(tp+fn)))
 sns.heatmap(cf_matrix, annot=labels, fmt="", cmap='Blues' )
fatalities_pred = rf.predict(x_test_cas)
 fatalities_pred<feature_engineering>
pd.set_option("display.max_rows", None, "display.max_columns", None)
 train_data_copy['Prediction'] = train_predictions
train_data_copy['Difference'] = abs(train_data_copy['Survived'] - train_data_copy['Prediction'])
 train_data_copy = train_data_copy[['Difference', 'Survived','Prediction', 'Pclass', 'Sex', 'Age', 'Fare', 'SibSp', 'Parch', 'Embarked']]
train_data_copy = train_data_copy.sort_values('Difference')
 train_data_copy.head()
fatalities_pred = np.around(fatalities_pred,decimals = 0)
 fatalities_pred<prepare_output>
test_data_copy = test_data.copy()
for feature in ['PassengerId', 'Name','Cabin', 'Ticket']:
test_data.drop(feature, axis=1, inplace=True)
 test_data["Age"] = test_data["Age"].replace(np.nan, median_age)
 test_data["Embarked"] = test_data["Embarked"].replace(np.nan, 'S')
 test_data["Fare"] = test_data["Fare"].replace(np.nan, train_data_copy["Fare"].median())
 for feature in ['Fare', 'Pclass', 'SibSp', 'Parch', 'Age']:
test_data[feature] = stats.zscore(test_data[feature])
 for feature in ['Sex', 'Embarked']:
one_hot = pd.get_dummies(test_data[feature])
 test_data = test_data.drop(feature,axis = 1)
 test_data = test_data.join(one_hot)
 X_test = test_data.to_numpy()
test_data.head()
<save_to_csv><EOS> 
test_predictions = model.predict(X_test ).flatten()
test_predictions_actual_values = np.array([1 if val >= 0.51 else 0 for val in test_predictions])
 output_test_logit = pd.DataFrame({'PassengerId': test_data_copy.PassengerId, 'Survived': test_predictions_actual_values})
 output_test_logit.to_csv('logit_submission.csv', index=False)
 print("Your logit submission was successfully saved!")