Applying Andrew Ng’s 1st Deep Neural Network to the Titanic Survival dataset

Jan Zawadzki
Towards Data Science
4 min readNov 14, 2017

--

You’re blown away by Andrew Ng’s first Coursera course on Deep Learning, maybe even binged it in a week. We all were! But control your eager fingers from jumping to the second course before applying your newly gained knowledge to a new dataset.

Utilize Andrew Ng’s Deep Learning course to predict Titanic Survival rates

This post covers the application of a neural network to the Titanic Survival dataset from kaggle. It helps the reader deepen their understanding of neural networks instead of simply executing carefully arranged code from Andrew Ng. The Titanic Survival dataset really just serves as an example. Please feel free to use any other binary classification dataset you like!

The code is in the Github repo here. Now let’s dive into the fun.

  1. Download the “Deep Neural Network Application” and “dnn_utils_v2.py” files from the Coursera hub and save them locally
  • The Github repo does not contain the code provided by deeplearning.ai. Please enroll in course 1 from the Coursera Deep Learning Specialization. The required material is in the programming assignments from week 4.
Download the “Deep Neural network Application v3” notebook from the Coursera hub and click on “Open”
Download the dnn_app_utils_v2.py file

2. Download the Titanic Survival dataset from kaggle and save it in the same location as your notebooks in the “datasets” folder

  • For convenience, I included the data sets in the Github repo already

3. Open the “Deep Neural Network Application” notebook

  • You can safely delete all other cells but the import and L-Layer_model cells
  • Run both cells

4. Load the Titanic Survival dataset

5. Preprocess the dataset

  • Impute passenger age based on class — idea courtesy of Jose Portilla’s highly recommended Udemy course “Python for Data Science and Machine Learning Bootcamp”, a subsection of Logistic Regression
  • Extract passenger titles from names — I followed Manuel’s great and simple post about extracting titles from names, thanks for sharing!
  • Drop the columns passengerId, cabin, and name
  • Dummy encode the categorical variables sex, embark and title
  • Scale age and fare — feature scaling helps the gradient descent algorithm converge faster, refer to the scikit-learn documentation or the Github repo

The preprocessed data now looks like this:

The training set contains 11 features, ready for the neural net to evaluate it

6. Apply your neural network

Get X and y variables and transpose them to fit the neural network architecture.

Choose first layer dimension according to your number of features. In this case, the first dimension is 11. Pick as many hidden layers as you wish afterward.

The first try yields 85% training and 82% test accuracy!

Preprocess the test data. Generate predictions through forward propagation with X and the parameters from training the neural network.

Last step: Generate predictions on the test data

Et voilá! You generated predictions, saved them as a csv file and you are now ready to submit the file to kaggle. This prediction will land you in the top 30% of the participants.

Submitting the predictions file lands you in the upper third and helps you get used to participating in kaggle competitions

Congratulations, you have applied a neural network to your own data set!

Now I encourage you to play with the number of iterations and layers in the network. Can you improve the score? What did you do differently?

Is applying a neural network to the Titanic Survival dataset overkill? Probably.

Is the network regularized and optimized? Not yet. After completing Course 2 we might have the tools handy to tune our neural network and improve our score.

Key takeaways:

  • Download the “Applied Deep Learning” and “dnn_utils_v2” jupyter notebooks from the Coursera hub and run them in your local environment
  • Preprocess your data accordingly
  • Transpose your X and y variables so that you have a “features by examples” training matrix
  • Adjust the first layer dimension to match your number of features
  • Train the neural network and save the generated parameters
  • Generate predictions on the test set through forward propagation on your test data and the previously saved neural network parameters

Thank you for reading the post. Feel free to reach out through LinkedIn, post questions in the comments or follow me to stay up to date on my learnings along the journey towards the earning Coursera Deep Learning Specialization.

--

--

CTO & Co-MD CertifAI | Focusing on reliable AI engineering to spearhead the next wave of AI adoption