Predict Your Chances Of Surviving The Titanic Disaster With C# And ML.NET Machine Learning
By: Mark Farragher
Tags: Artificial Intelligence, Machine Learning, Deep Learning, Data Science, Programming

It’s a CSV file with 12 columns of information:

The passenger identifier

The label column containing ‘1’ if the passenger survived and ‘0’ if the passenger perished

The class of travel (1–3)

The name of the passenger

The gender of the passenger (‘male’ or ‘female’)

The age of the passenger, or ‘0’ if the age is unknown

The number of siblings and/or spouses aboard

The number of parents and/or children aboard

The ticket number

The fare paid

The cabin number

The port in which the passenger embarked

The second column is the label: 0 means the passenger perished, and 1 means the passenger survived. All other columns are input data from the passenger manifest.

I will build a binary classification machine learning model that reads in all columns, and then makes a prediction for each passenger if he or she survived.

Let’s get started. Here’s how to set up a new console project in NET Core:

$ dotnet new console -o Titanic

$ cd Titanic

Next, I need to install NuGet packages:

$ dotnet add package Microsoft.ML

$ dotnet add package BetterConsoleTables

Microsoft.ML is the main ML.NET library, and BetterConsoleTables is a library that lets me output nice-looking tables to the console.

Now I’m ready to start coding. I will start by loading the training data in memory.

I will modify the Program.cs file like this:

This code uses the CreateTextLoader method to create a CSV data loader. The TextLoader.Options class describes how to load each field. Then I call the text loader’s Load method twice to load the train- and test data in memory.

Note how I’m loading the age column as a string and not a number. I’m doing this because ML.NET expects missing data in CSV files to appear as a ‘?’. Unfortunately my Titanic file uses a ‘0’ to indicate an unknown age.

So the first thing I need to do is replace all ‘0’ age occurrences with ‘?’.

I’ll need to declare two helper classes for this:

I’ll use the FromAge and ToAge classes in the next step to transform the data.

Machine learning models in ML.NET are built with pipelines, which are sequences of data-loading, transformation, and learning components.

I’m not interested in the Name, Cabin, and Ticket columns. The data in those columns will not help the machine learning model to predict if the passenger survived or perished. So I’ll drop them from my dataset with the DropColumns component.

Then I’ll add a CustomMapping component which will convert my unknown age values to ‘?’ strings:

Now ML.NET is happy with the age values. I’ll convert the string ages to numeric values and instruct ML.NET to replace missing values with the mean age over the entire dataset:

The Sex and Embarked columns are enumerations of string values. My machine learning model can deal with that, but I need to one-hot encode them first:

And finally, I’ll concatenate all remaining columns into a single feature column (a requirement for ML.NET training) and attach a Fast Tree Learner to train the model:

The FastTreeBinaryClassificationTrainer is a very nice training algorithm that uses gradient boosting, a machine learning technique for classification problems.

Now let’s take a look inside the ML.NET training pipeline. What are the final data columns? What does the actual data look like?

I’m going to add a helper method to the Program class that can write the contents of the pipeline to the console. Check this out:

This code uses the BetterConsoleTables library to write the pipeline to the console in a nice format. And there’s some extra code to display sparse- and dense vectors in a compact format so they don’t mess up the layout of the table.

With this method in place, add the following line to your Main method:

Run the code. Here’s what you’ll see: