ML.NET: Heart disease prediction
Tags: machinelearning, softwaredevelopment, softwareengineering
Published: 2019-04-11 12:55:31

---
title: 'ML.NET: Heart disease prediction'
published: true
description: "Microsoft announced ML.NET last May, and as an advocate user of the\_.NET framework with experience in Machine Learning, I knew that I’d…"
date: '2019-04-11T12:55:31.166Z'
tags: machinelearning,softwaredevelopment,softwareengineering
canonical_url: https://medium.com/@dvirsegal/ml-net-heart-disease-prediction-6fd73a274eec
---

![[Heart Attack Stroke Disease — Free image on Pixabay](https://pixabay.com/illustrations/heart-attack-stroke-heart-disease-3177360/)](https://cdn-images-1.medium.com/max/2560/1*CEwJ9Ai7LEytGq-tPmMafg.jpeg)

<span>
<em>Originally published at <a href="https://medium.com/@dvirsegal/ml-net-heart-disease-prediction-6fd73a274eec" style="color:#1395b8">Medium</a> on <time datetime="2019-04-11T12:55:31.166Z" title="Thursday, April 11, 2019, 12:55:31 PM">Apr 11, 2019</time>
</em>
</span>

Microsoft announced ML.NET last May, and as an advocate user of the .NET framework with experience in Machine Learning, I knew that I’d have to give it a try knowingly that Python various frameworks (such as [scikit-learn](https://scikit-learn.org/stable/)) rule this domain.

ML.NET is a free, cross-platform, open source machine learning framework explicitly made for .NET developers. The preview release includes learners to handle binary classification, multi-class classification, and regression tasks. Additional ML tasks like a recommendation system, clustering, anomaly detection, ranking models, and deep learning architectures have been added.

In this blog-post, my purpose is to play along with ML.NET capabilities and eventually demonstrating its ease of use. My goal is to predict a heart disease for a given patient based on an opensource dataset.

![ML.NET logo by Wikimedia Commons](https://cdn-images-1.medium.com/max/800/0*YWGKpgPBnuFtBqSk.png)

#### Getting started

Assuming Visual Studio 2017 is installed on your system, open a new console application (.NET core) project:

![](https://cdn-images-1.medium.com/max/800/1*bn1lfUteirJ5NZETRDEVbQ.png)

Afterward, go to the ‘**Tools**’ menu and choose ‘**Manage NuGet Packages for Solution…**’:

![](https://cdn-images-1.medium.com/max/800/1*4kkhpH7IyVTS38j4XjwVyA.png)

There, browse for ‘[Microsoft.ML](https://www.nuget.org/packages/Microsoft.ML/)’ and choose the latest stable version (at the time of writing these lines, the stable version is 0.11.0) to install it for the current solution. As a side note, if you prefer using the package manager console, just run the following command:

> Install-Package Microsoft.ML -Version 0.11.0

Now that all prerequisites are in place, we can start doing some machine learning magic.

#### The Heart Disease Dataset

For the binary classification task, I used the Heart Disease UCI dataset from [Kaggle](https://www.kaggle.com/ronitf/heart-disease-uci) datasets; It contains 14 columns and 303 records.

![Heart Disease Dataset Columns (screenshot is taken from Kaggle)](https://cdn-images-1.medium.com/max/800/1*nQblVebCZ_IlBm5xzcmAmA.png)
Heart Disease Dataset Columns (screenshot is taken from Kaggle)

[**Heart Disease UCI**](https://www.kaggle.com/ronitf/heart-disease-uci)

I’ve manually split the CSV file into two files, smaller one for the test data and a larger one for the training data. Below, the test data CSV as an example.

#####Test Data CSV

{% gist https://gist.github.com/dvirsegal/c6bd3ad78ac7b0a867dc5d2285effea3 %}

ML.NET provides mapping attributes to model the dataset structure. It means each header is mapped to a _LoadColumn_ attribute as follows:

{% gist https://gist.github.com/dvirsegal/033f29c7ef78695cf2547dd98a97e588 %}

#### **Training the model**

Training a model based on a given dataset requires to define a context as an entry point or as defined by ML.NET API:

> The [MLContext](https://docs.microsoft.com/en-us/dotnet/api/microsoft.ml.mlcontext?view=ml-dotnet) is a starting point for all ML.NET operations. It is instantiated by the user, provides mechanisms for logging and entry points for training, prediction, model operations, etc.

Afterward, the training and test datasets are loaded using the context, based on the mapped structure.

{% gist https://gist.github.com/dvirsegal/cc17737d4ad07db9abda0e021f1a7769 %}

Transform the data and add a learning algorithm for this task numeric values are assigned to text because only numbers can be processed during model training. In this case, the problem I try to predict is a type of Binary Classification (two classes, has and hasn’t diseased). The selected model is based on Decision Trees because they can be easily interpreted (by humans) as rules, have excellent [performance](http://www.cs.cornell.edu/~caruana/ctp/ct.papers/caruana.icml06.pdf) and don’t require any assumptions on the data.

{% gist https://gist.github.com/dvirsegal/10439bfc2ecfafd596ff18d989778965 %}

Eventually, the model is trained by using the _fit_ method:

{% gist https://gist.github.com/dvirsegal/349d9e8914eb3bc58bb3ca0a4e924035 %}

We’ve trained our model with a few steps, as simple as that.

BTW, the framework provides capabilities for saving your model. According to the tutorial it is recommended to save it as a ZIP file.

#### Testing

Given the trained model, prediction can be made.   
For that purpose, a sample _class_ containing a list of patients is given as input to the model for prediction.

{% gist https://gist.github.com/dvirsegal/f276182fd0979ca9887f3fbd3adbcb8e %}

The result prediction structure shall be defined too, as follows:

{% gist https://gist.github.com/dvirsegal/d11b8f789029dd11156a5e8a20dcdaf7 %}

In the below gist, I load the model from disk, create a prediction engine based on the resulting structure (defined above) and using the engine I predict the probability for heart disease on the list of patients.

{% gist https://gist.github.com/dvirsegal/dc5d1c01bb11fc82560c8f2a2a5a4ab5 %}

Which returns the following (Prediction Output):

{% gist https://gist.github.com/dvirsegal/ac88a783c4d0b62d06e9c5a706923daf %}

In terms of the result accuracy, the code hits 78.95% accuracy, below are more evaluation parameters generated by the model’s Evaluate method:

{% gist https://gist.github.com/dvirsegal/8b1e8145955a84cb37e5cddcbffac716 %}


Overall, ML.NET has it all, flexible, robust and supported by a big company that provides the engineering vision behind it. I recommend you to try it too, and for sure I’ll use it again soon.

This blog-post was written based on the [Heart disease Classification](https://github.com/dotnet/machinelearning-samples/tree/master/samples/csharp/getting-started/BinaryClassification_HeartDiseasePrediction) coding sample with some modification; more ML.NET Samples can be found at:

[**Machine Learning Samples** Samples for ML.NET, an open source and cross-platform machine learning framework](https://github.com/dotnet/machinelearning-samples/blob/master/README.md)