🏙 Machine Learning Engineer Nanodegree   🌀   Home Page      

Introduction and Foundations

📑  P0: Titanic Survival Exploration

In 1912, the ship RMS Titanic struck an iceberg on its maiden voyage and sank, resulting in the deaths of most of its passengers and crew.
In this introductory project, we will explore a subset of the RMS Titanic passenger manifest to determine which features best predict whether someone survived or did not survive.

Getting Started

Resources

🕸 Intro to Data Science. Udacity   🕸 Statistics in Python. Scipy Lecture Notes   🕸 A Visual Introduction to Machine Learning. Part 1   🕸 The scikit-learn metrics

Code Tools




To begin working with the RMS Titanic passenger data, we'll first need to import the functionality we need, and load our data into a pandas DataFrame.
Run the code cell below to load our data and display the first few entries (passengers) for examination using the .head() function.


From a sample of the RMS Titanic data, we can see the various features present for each passenger on the ship:
Survived: Outcome of survival (0 = No; 1 = Yes)
Pclass: Socio-economic class (1 = Upper class; 2 = Middle class; 3 = Lower class)
Name: Name of passenger
Sex: Sex of the passenger
Age: Age of the passenger (Some entries contain NaN)
SibSp: Number of siblings and spouses of the passenger aboard
Parch: Number of parents and children of the passenger aboard
Ticket: Ticket number of the passenger
Fare: Fare paid by the passenger
Cabin: Cabin number of the passenger (Some entries contain NaN)
Embarked: Port of embarkation of the passenger (C = Cherbourg; Q = Queenstown; S = Southampton)
Since we're interested in the outcome of survival for each passenger or crew member,
we can remove the Survived feature from this dataset and store it as its own separate variable outcomes.
We will use these outcomes as our prediction targets.

The very same sample of the RMS Titanic data now shows the Survived feature removed from the DataFrame.
Note that the passenger data and the outcomes of survival are now paired. That means for any passenger data.loc[i], they have the survival outcomes[i].
To measure the performance of our predictions, we need a metric to score our predictions against the true outcomes of survival - the function accuracy_score().
Since we are interested in how accurate our predictions are, we will calculate the proportion of passengers where our prediction of their survival is correct.
Think: Out of the first five passengers, if we predict that all of them survived, what would you expect the accuracy of our predictions to be?

Making Predictions

If we were asked to make a prediction about any passenger aboard the RMS Titanic whom we knew nothing about,
then the best prediction we could make would be that they did not survive.
This is because we can assume that a majority of the passengers (more than 50%) did not survive the ship sinking.
The predictions_0() function below will always predict that a passenger did not survive.

Question 1

Using the RMS Titanic data, how accurate would a prediction be that none of the passengers survived?

Answer 1


Let's take a look at whether the feature Sex has any indication of survival rates among passengers using the survival_stats() function.
This function is defined in the visualizations.py. Python script included with this project.
The first two parameters passed to the function are the RMS Titanic data and passenger survival outcomes, respectively.
The third parameter indicates which feature we want to plot survival statistics across.

Examining the survival statistics, a large majority of males did not survive the ship sinking.
However, a majority of females did survive the ship sinking.
Let's build on our previous prediction: if a passenger was female, then we will predict that they survived.
Otherwise, we will predict the passenger did not survive.

Question 2

How accurate would a prediction be that all female passengers survived and the remaining passengers did not survive (prediction_1())?

Answer 2


Using just the Sex feature for each passenger, we are able to increase the accuracy of our predictions by a significant margin.
Now, let's consider using an additional feature to see if we can further improve our predictions.
For example, consider all of the male passengers aboard the RMS Titanic: can we find a subset of those passengers that had a higher rate of survival?
Let's start by looking at the Age of each male, by again using the survival_stats() function.
This time, we'll use the fourth parameter to filter out the data so that only passengers with the Sex 'male' will be included.

Examining the survival statistics, the majority of males younger than 10 survived the ship sinking, whereas most males age 10 or older did not survive the ship sinking.
Let's continue to build on our previous prediction: if a passenger was female, then we will predict they survive.
If a passenger was male and younger than 10, then we will also predict they survive. Otherwise, we will predict they do not survive.

Question 3

How accurate would a prediction be that all female passengers and all male passengers younger than 10 survived (prediction_2())?

Answer 3


Adding the feature Age as a condition in conjunction with Sex improves the accuracy by a small margin more than with simply using the feature Sex alone.
Now we can try to find a series of features and conditions to split the data on to obtain an outcome prediction accuracy of at least 80%.
This may require multiple features and multiple levels of conditional statements to succeed.
We can use the same feature multiple times with different conditions.
There are some experiments and the function prediction_final() as a result:



Question 4

Describe the steps you took to implement the final prediction model so that it got an accuracy of at least 80%.
What features did you look at? Were certain features more informative than others?
Which conditions did you use to split the survival outcomes into the data? How accurate are your predictions?

Answer 4

The final set of features Sex, Age, SibSp and Pclass are the most informative on my opinion.
As we noted the percentage of survivors of passengers is much higher among women than among men, and it was used in our predictions.
Next, I proceed from the assumption that because of humanitarian reasons people rescue children and elders at first.
Unfortunately, this was only valid for the passengers of the first and second classes in this dataset.
And the latest clarification, which overcomes the border of 80% in prediction accuracy:
if a family has more than three children, absolutely all the family may not be survived in catastrophic situations and in an atmosphere of panic.

Scikit-learn Metrics

There is a set of metrics in the scikit-learn package for evaluation the quality of predictions.

The evaluation terminology:

$accuracy \ = \ \frac{number \ of \ people \ that \ are \ correctly \ predicted \ as \ survived \ or \ non−survived}{number \ of \ all \ people \ in \ the \ dataset}$

$recall \ = \ \frac{number \ of \ people \ that \ are \ predicted \ as \ survived \ and \ they \ are \ actually \ survived}{number \ of \ people \ that \ are \ actually \ survived}$

$precision \ = \ \frac{number \ of \ people \ that \ are \ predicted \ as \ survived \ and \ they \ are \ actually \ survived}{number \ of \ people \ that \ are \ predicted \ as \ survived}$

It's easy to see that we could be enough confident in this prediction. I think this model can be improved and it's possible to find a way for that.

Question 5

Think of a real-world scenario where supervised learning could be applied. What would be the outcome variable that you are trying to predict?
Name two features about the data used in this scenario that might be helpful for making the predictions.

Answer 5

There are several natural ideas for applying supervised learning.
I.
For every catastrophic situation, find out the exact sequence of steps and technical facilities which maximally decrease the damage.
On the basis of the identified trends, it is possible to develop and check in practice clear guidelines to save lives and restore economic activities (for example, during and after the floods).
Applying the scientific methods, in this case, means thousands of lives and quick recovering the economics.
The useful features can evaluate disasters (areas, time period), damage (lost human lives, economic indicators) and recovery process (speed, effectiveness).
As a target, we can use the damage_in_general measured in any financial units.
II.
The same techniques could be useful in the process of creating self-learning facilities of virtual reality in order to bring the process
of their development to the real counterparts, predict it and make corrections in time. Here the set of concrete features is very individual and depends on the object.
For example, it can be growth, flowering, etc. for the plant and its imitation.
In this case, the outcomes can be number indicators which evaluate how well the development of virtual objects is correlated to the real processes.

Conclusion

In this project, we use a manual implementation of a simple machine learning model:
the decision tree splits a set of data into smaller and smaller groups (called nodes), by one feature at a time.
Our predictions become more accurate if each of the resulting subsets is more homogeneous (contain similar labels) than before.

The decision tree algorithm is just one of many models that come from supervised learning,
i.e. learning a model from labeled training data to make predictions about unseen or future data in a set of samples the desired outputs are already known.

Overvaluation of meaning and application of machine learning is unlikely to succeed. And the particular supervised method has a special importance
because of the possibility of a permanent correlation of the predictions with the result of real actions.

For Additional Code Experiments