Loading the Titanic Dataset
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, updated Aug 17 2026
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
We begin by importing all required libraries and loading the Seaborn Titanic dataset, which provides cleaned but rich information: passenger demographics, ticket class, fares, and survival outcome.
Setup the virtual environment
# Create a virtual environment called '.venv'python3 -m venv .venv# Activate it (Linux/macOS)source .venv/bin/activate# or activate it (Windows CMD).venv\Scripts\activate# or activate it (Windows PowerShell).\.venv\Scripts\Activate.ps1# Your shell prompt should change to indicate the environment is active# e.g., (.venv) $Install all the libraries
pip install numpy pandas seaborn matplotlib scipy scikit-learn tensorflowImport all the libraries needed and load the dataset:
import numpy as npimport pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltfrom scipy import statsfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import OneHotEncoder, StandardScalerfrom sklearn.compose import ColumnTransformerfrom sklearn.pipeline import Pipelinefrom sklearn.metrics import ( accuracy_score, classification_report, confusion_matrix, roc_auc_score, RocCurveDisplay, roc_curve,)from sklearn.linear_model import LogisticRegressionfrom sklearn.ensemble import RandomForestClassifierimport tensorflow as tffrom tensorflow.keras import layers, models, callbackssns.set(style="whitegrid")RANDOM_STATE = 42# Load datasettitanic = sns.load_dataset("titanic")print(titanic.head())The dataset includes both numeric and categorical columns, with survived being our prediction target.