Skip to main content

← All projects

Guided project · Predict a yes/no

Titanic Survival

The classic first Kaggle competition: who survived the Titanic, and why a one-line rule is hard to beat.

In 1912 the Titanic sank and 1,502 of 2,224 people on board died. Survival was not random: who you were and where you stood on the ship mattered. You will predict survival from the passenger list and find out how much a model adds beyond a single common-sense rule.

The data

Titanic passengers (891 people)

Bundled from the open datasciencedojo/datasets repository, which redistributes the public Titanic passenger list used in the Kaggle tutorial competition.

Open the source

Use the Kaggle version

The competition's train.csv has the same columns. Download it there and upload it below to run every step on your own copy.

Titanic: Machine Learning from Disaster on Kaggle (free Kaggle account needed to download)

Work offline

Every step below runs in your browser. Prefer your own computer? Download the notebook and the data file.

Download the notebook · Download titanic.csv

The brief

Goal

Predict Survived (1 = lived, 0 = died) for the passengers in the hidden test set.

How you are scored

Accuracy: the share of passengers classified correctly (this is the Kaggle metric). Compare it with the 'women survive' rule.

Rules of the game

  • Keep survivors and non-survivors in the same share in every pile (stratified split).
  • Learn the age fill value from train only.
  • Open the test passengers once.

What you hand in

A file with PassengerId and your predicted Survived (0 or 1), exactly as Kaggle asks.

Data dictionary

ColumnWhat it means
PassengerIdA running number. Not a feature.
SurvivedThe target: 1 survived, 0 died.
PclassTicket class: 1 (first), 2 or 3 (third). A rough stand-in for wealth.
NameFull name. Hidden inside it is a title such as Mr, Mrs, Miss or Master.
Sexmale or female.
AgeAge in years. About 20% missing.
SibSp / ParchSiblings or spouses aboard, and parents or children aboard.
Ticket / FareTicket code and price paid.
CabinCabin code. 77% missing, and the gaps mean something.
EmbarkedPort: C (Cherbourg), Q (Queenstown) or S (Southampton).

Before you begin

Target
What you predict: Survived, 1 or 0.
Feature
A column you use to predict, such as Sex or Pclass.
Accuracy
The share of passengers you get right. Fine here because survivors are not rare (38%).
Baseline
The simplest sensible guess. A model must beat it to matter.
Feature engineering
Building a more useful column from raw ones, like pulling the title out of a name.
Overfitting
Learning the quirks of the training passengers instead of the pattern. Shows up as a good training score and a worse validation score.

Work through it, one step at a time

Each step is a short program you can run, read and edit. Press Run, look at what it prints, then open “What to notice”. The first run in a page loads Python and the data, which takes a few seconds.

Download titanic.csv-style data from Kaggle and choose it here. It stays in your browser and is never uploaded anywhere.

  1. Step 1 of 10

    Meet the passengers

    One row per passenger, with the answer in Survived. Check how common each answer is before anything else.

    Live Python

    Step 1: Meet the passengers

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    What to notice

    891 passengers, 12 columns, and 38.4% survived. If you always guessed 'died' you would be right about 62% of the time, so a model has to do better than that.

  2. Step 2 of 10

    Find what is missing, and what the gaps mean

    Age, Cabin and Embarked have holes. Before filling them, ask whether being missing tells you something.

    Live Python

    Step 2: Find what is missing, and what the gaps mean

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    What to notice

    20% of ages and 77% of cabins are missing. Passengers with a known cabin survived 66.7% of the time against 30.0% without one. Cabin was recorded mostly for first class, so 'missing' is a clue about wealth, not random noise.

    Aha. A gap is information. Dropping or filling it without looking can throw the clue away.

  3. Step 3 of 10

    Look at who survived

    Group averages show the pattern a model will learn. Look at one factor at a time, then two together.

    Live Python

    Step 3: Look at who survived

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    What to notice

    Women survived 74.2% of the time, men 18.9%. First class survived 63.0%, third class 24.2%. Together they tell the story: first-class women survived 96.8% of the time, third-class men only 13.5%.

  4. Step 4 of 10

    Split fairly, then set two baselines

    Split into train, validation and test with the same share of survivors in each. Then write the dumbest rules and score them.

    Live Python

    Step 4: Split fairly, then set two baselines

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    What to notice

    'Nobody survives' scores 0.618. 'Women survive, men do not' scores 0.798, one line of code and nearly 80%. Every model has to beat 0.798 to earn its complexity.

  5. Step 5 of 10

    Build better columns

    Turn raw columns into ones a model can use: a 0/1 for sex, family size, the title hidden in the name, and a flag for 'age was missing'.

    Live Python

    Step 5: Build better columns

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    What to notice

    14 columns, built from 7. The median age 29 is learned from train only. 'Master' (a young boy) and 'Mrs' will turn out to matter.

    Aha. The name column looks useless, yet its title carries age and status. Good features come from asking what a column means.

  6. Step 6 of 10

    Train a model you can read

    Logistic regression gives each feature a weight. Positive pushes toward survival, negative away.

    Live Python

    Step 6: Train a model you can read

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    What to notice

    Validation accuracy is 0.837. The largest weights are female (+0.96) and Pclass (-0.93), then family_size and Age. The model learned what the group averages showed, which is reassuring.

  7. Step 7 of 10

    Try a random forest

    A random forest averages many decision trees. Compare it fairly with the logistic model on the same validation rows.

    Live Python

    Step 7: Try a random forest

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    What to notice

    The forest scores 0.854 against 0.837. A modest gain on only 178 passengers is within noise: with so few rows, one or two passengers change the score by a full percentage point.

  8. Step 8 of 10

    Find where it is wrong

    Slice accuracy by group, and check which kind of mistake is more common.

    Live Python

    Step 8: Find where it is wrong

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    What to notice

    Accuracy is similar for women (85.9%) and men (85.1%). Second class is easiest (94.4%) and third class hardest (82.8%). About 62% of the mistakes are survivors the model said would die.

    Aha. Overall accuracy hides which mistakes you make. Here, most are people who beat the odds.

  9. Step 9 of 10

    Open the test set once

    Choose the forest, refit on train plus validation, and score the 179 test passengers one time. Compare with the one-line rule.

    Live Python

    Step 9: Open the test set once

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    What to notice

    Test accuracy is 0.821 against 0.777 for the women-survive rule. The model beats the rule by about four points, and the gap is smaller than what the validation numbers suggested. That shrinkage is normal and is exactly why the test set exists.

  10. Step 10 of 10

    Write your submission file

    Kaggle wants PassengerId and Survived, nothing else. Build exactly that and download it.

    Live Python

    Step 10: Write your submission file

    Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.

    Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.

    What to notice

    A 179-row file of 0s and 1s. On Kaggle itself you would train on all of train.csv and predict the competition's own test.csv.

Say the limits out loud

A result is only useful with its limits attached. Include these when you present it.

  • This is a small, historical data set. A model that works here says little about modern risk.
  • 'Women and children first' shows up in the data, so the model partly encodes who was given priority, not who was fittest.
  • With only 891 passengers, small score differences are noise. Do not chase them.
  • Prediction of survival is not an explanation. Do not infer that being in third class caused death.

Try next

  • Try max_depth values from 2 to 8 in the forest and watch validation and training accuracy.
  • Add a column for 'travelling alone' and see if it helps.
  • Upload Kaggle's train.csv and compare your results with the numbers here.