Neural Network (Keras)
End-to-End Machine Learning: Titanic Survival Prediction
1 min read
This section is 1 min read, full guide is 12 min read
Published Nov 18 2025
10
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
KerasMachine LearningMatplotlibNumPyPandasPythonscikit-learnSciPySeabornTensorFlow
Neural networks require all data to be numeric, so we reuse the fitted preprocessor:
preprocessor.fit(X_train)
X_train_t = preprocessor.transform(X_train)
X_test_t = preprocessor.transform(X_test)
if hasattr(X_train_t, "toarray"):
X_train_t = X_train_t.toarray()
X_test_t = X_test_t.toarray()
Copy to Clipboard
Define a feedforward network:
model = models.Sequential([
layers.Input(shape=(X_train_t.shape[1],)),
layers.Dense(64, activation="relu"),
layers.Dropout(0.3),
layers.Dense(32, activation="relu"),
layers.Dropout(0.3),
layers.Dense(1, activation="sigmoid")
])
model.compile(
optimizer=tf.keras.optimizers.Adam(1e-3),
loss="binary_crossentropy",
metrics=["accuracy"]
)
Copy to Clipboard
Train:
early = callbacks.EarlyStopping(patience=5, restore_best_weights=True)
model.fit(
X_train_t, y_train,
validation_split=0.2,
epochs=50,
batch_size=32,
callbacks=[early],
verbose=1
)
Copy to Clipboard
Evaluate:
prob_keras = model.predict(X_test_t).ravel()
pred_keras = (prob_keras >= 0.5).astype(int)
print("Keras Accuracy:", accuracy_score(y_test, pred_keras))
print("Keras ROC-AUC:", roc_auc_score(y_test, prob_keras))
Copy to Clipboard
Output:
Keras Accuracy: 0.8100558659217877
Keras ROC-AUC: 0.8592885375494071
Copy to Clipboard














