One missing age.
Does the median change?
Work through the numbers yourself. Start with three passengers from the Titanic training set, see what happens when you remove one age, and write up the method so someone else can repeat it.
Download practice dataLeave one age blank
Change only the age of passenger 2. Watch the sorted values, the number of known ages and the median as you switch between the two conditions.
Original records
train.csv| Passenger ID | Class | Age |
|---|---|---|
| 1 | Third | 22 years |
| 2 | First | 38 years |
| 3 | Third | 26 years |
Recalculated result
3 known ages26 years
Median of the known ages
22, 26, 38Three ages in order. The middle value is 26.
Data from the Kaggle Titanic training set. Passenger 2 was recorded as 38 years old. We hide that value here so you can compare results.
Can someone else
reproduce your result?
Describe your sample and your method so a classmate can repeat the calculation. These results apply to the three passengers in this exercise.
Check your lab notes
- Sample
- Use the first three training rows, keeping passengers 1, 2 and 3.
- Method
- The original ages are 22, 38 and 26, in that order. For comparison, leave 38 blank. Skip the missing entry when calculating the median.
- Median with all ages
- 26 years
- 3 known ages22, 26, 38
- Median with one age missing
- 24 years
- 2 known ages(22 + 26) ÷ 2
Try it in a spreadsheet. Open the CSV and enter =MEDIAN(C2:C4) in an empty cell. The result is 26. Make a copy of the sheet and clear C3. Leave it blank and recalculate to get 24.
From three passengers
to twelve
This time, the age was already missing in the training data. Read the first twelve Titanic passengers in ID order. Whose age is missing? What else do you still know about that person?
- 0122
- 0238
- 0326
- 0435
- 0535
- 06Missing
- 0754
- 082
- 0927
- 1014
- 114
- 1258
Passenger 6 travelled in third class. This row counts when you tally passengers. Only the other eleven have ages you can sort for a median. Keep the row so you can estimate this passenger’s age next.
Download the 12-passenger CSVOne missing age,
two estimates
Suppose your model needs an age for every row. To fill passenger 6's blank, would you use everyone with a known age, or just the people who travelled in the same class?
Add two columns for your estimates and keep the original Age column intact. In a spreadsheet, take the median of C2:C13 to check the first method. To check the second, copy the six known third-class ages into a fresh range and take their median.
Median across all passengers
Put the eleven known ages in order. The sixth value is 27. This estimate draws on everyone in the sample who has a recorded age.
27 yearsMedian within the same class
The known third-class ages are 2, 4, 22, 26, 27 and 35. Average the two middle values: (22 + 26) ÷ 2.
24 yearsHide a known age
and test both methods
We do not know passenger 6's age. Instead, hide the age of someone whose age was recorded. Recalculate both estimates using everyone else, then reveal the original. Hide one age at a time and restore it before trying another.
Choose a passenger to check
Passenger 2 · First class
The recorded age is 38. Without it, the overall median is 26.5, an error of 11.5 years. In first class, the remaining ages are 35, 54 and 58. Their median is 54, an error of 16 years.
Passenger 8 · Third class
This passenger was just 2. The overall estimate is 31, off by 29 years. The third-class estimate is 26, off by 24. Both methods overestimate the child's age.
Passenger 9 · Third class
The recorded age is 27. The two middle values among the ten other known ages are 26 and 35. The overall estimate is 30.5, off by 3.5 years. The third-class estimate is 22, off by 5 years.
Keep a notebook you can rerun
Download the lab and extract both files to the same folder. In Jupyter, open age-check.ipynb and run the cells in order. The notebook uses only Python's standard library and prints the results of the three tests above.
Change the passenger ID in the test to 3 and run it again. Which estimate came closer? How many same-class records did it use? With just twelve passengers, it is easy to trace why changing one person can change the answer.
Download notebook and data