← Back to Projects

Titanic Survival Prediction API

A machine learning API that predicts passenger survival on the Titanic using a Random Forest classifier, packaged in Docker and published on Docker Hub so anyone can run it with a single command

Python Docker FastAPI Scikit-learn Pandas

Project Overview

Business Context: Taking a trained model off my own computer and turning it into a service that other people and applications can actually use.

In this project I trained a machine learning model from scratch, containerized it, and published it to Docker Hub. The model is trained inside a Jupyter Docker container, then served from a separate, lighter Docker container using FastAPI.

Anyone with Docker can pull the image and get survival predictions for a Titanic passenger through a simple API, without installing Python or any libraries.

What This Demonstrates

Learning Challenge

  • Docker was completely new to me: images, containers, Dockerfiles, volumes, and port mapping
  • Keeping a training environment and a serving environment separate
  • Building the project with current versions instead of the 2023 versions in the course instructions

Problem-Solving Process

  1. Training Container: Built a custom Jupyter image with my ML libraries and mounted my project folder into it
  2. Model Training: Cleaned the data, created new features, and trained a Random Forest classifier
  3. Serving Container: Wrapped the saved model in a FastAPI app and packaged it into a slim Python image
  4. Testing and Sharing: Tested the API locally, then tagged it and pushed it to Docker Hub
  5. Debugging: Found and fixed several things in the instructions that broke on newer versions, like missing values that silently stopped getting filled in with pandas 3

Professional Outcome

  • A complete ML deployment pipeline, from raw CSV to a public Docker image
  • An API that checks its input and gives a clear error for bad values instead of a wrong prediction
  • Updated the whole project from the 2023 course versions to current versions and fixed what broke

Tools Utilized

  • VS Code with Claude Code for development
  • Docker and Docker Hub for containers
  • Jupyter Lab (running inside Docker) for training
  • Git/GitHub for version control

API Endpoints

GET /

Health check that shows whether the model loaded and its accuracy.

POST /predict

Send one passenger's details and get back survived or not, the survival probability, and a confidence level.

POST /predict-batch

Send a list of passengers and get a prediction for each one.

GET /model-info

Model type, training date, accuracy, and the features it uses.

Dataset

The model is trained on Kaggle's Titanic train.csv (891 passengers). The API takes 7 inputs:

  • Pclass - Passenger class (1, 2, 3)
  • Sex - male or female
  • Age - Age in years
  • SibSp - Siblings/spouses aboard
  • Parch - Parents/children aboard
  • Fare - Ticket price
  • Embarked - Port: Cherbourg, Queenstown, or Southampton

How It Works

  1. Training Container - Jupyter Lab image with pandas, scikit-learn, and the plotting libraries, with my project folder mounted as a volume
  2. Data Cleaning - Fill in missing ages, fares, and ports
  3. Feature Engineering - Family size, traveling alone, title from the name, age group, and fare group
  4. Model Training - Random Forest with 20% of passengers held out for testing
  5. Saving the Model - Model, encoders, and metadata saved with joblib and copied to the serving folder
  6. Serving Container - Slim Python image running FastAPI, with a health check and a non-root user
  7. Publishing - Tagged as v1.0 and latest, and pushed to Docker Hub

Key Results

  • Accuracy: about 82% (0.8156) on 179 test passengers
  • Live demo: the same Docker image runs on my server, so anyone can try the model from the demo page
  • One-command setup: docker run -p 8000:8000 vfoster2docker/titanic-survival-api
  • Input checking: bad values like "Male" with a capital M or class 4 get a clear error instead of a wrong prediction
  • Up to date: Python 3.13, pandas 3.0, scikit-learn 1.9, and the current Jupyter images

Note: The full training run with charts is available in the notebook.

Skills Demonstrated

  • Writing Dockerfiles for training and serving environments
  • Docker volumes, port mapping, and container management
  • Data cleaning and feature engineering
  • Training and evaluating a classification model
  • Saving and loading models with joblib
  • Building a REST API with FastAPI and Pydantic
  • Publishing images to Docker Hub with version tags
  • Debugging version problems and documenting changes

Technologies Used

Python 3.13 Docker Docker Hub Pandas Scikit-learn Matplotlib Seaborn FastAPI Pydantic Uvicorn Jupyter Lab