Problem
This problem is centered around Cardiovascular Heart Disease Prediction, i.e., whether a person is suffering from cardiovascular disease or not. It is important that cardiologists are able to recognize cardiovascular disease in patients.
The input data set consists of factual information, results of medical examinations, and the information given by the patient.
Dataset
The datasets contain 70,000 records of patient data.
Data description
There are 3 types of input features,
- Objective: factual information;
- Examination: results of medical examination;
- Subjective: information given by the patient.
Features
- Age | Objective Feature | age | int (days)
- Height | Objective Feature | height | int (cm) |
- Weight | Objective Feature | weight | float (kg) |
- Gender | Objective Feature | gender | categorical code |
- Systolic blood pressure | Examination Feature | ap_hi | int |
- Diastolic blood pressure | Examination Feature | ap_lo | int |
- Cholesterol | Examination Feature | cholesterol | 1: normal, 2: above normal, 3: well above normal |
- Glucose | Examination Feature | gluc | 1: normal, 2: above normal, 3: well above normal |
- Smoking | Subjective Feature | smoke | binary |
- Alcohol intake | Subjective Feature | alco | binary |
- Physical activity | Subjective Feature | active | binary |
- Presence or absence of cardiovascular disease | Target Variable | cardio | binary |
All of the dataset values were collected at the moment of medical examination.
Solution
Prerequisites
- Visual Studio (I'm using VS2019)
- ML.NET Model Builder
- ASP.NET Core (I'm using 2.2)
- Cardiovascular Disease Dataset
Let's start,
- Open Visual Studio and create a new project, and select ASP.NET Core.
- Enter the project name and click on the "Create" button.
- Select ASP.NET Web Application (Model View Controller).
- Our ASP.NET Core MVC project template has been created.
- So first of all, we will build, train and evaluate our model using ML.NET Model Builder.
- Right-Click on the project and Select Add>Machine Learning.
- ML.NET Model Builder window will be shown. Now, select a custom scenario because we are going to build our model from our Cardiovascular Disease dataset.
- We will use the Cardiovascular Disease data set file in .csv format. Select the dataset from your local directories where you place the .csv file then select the column that will be used to the prediction label.
- After training dataset selection, select the Machine learning task as binary-classification because this model will tell us only if the patient has a disease or not.
- Select the time to train the model. Click on start training.
- ML.NET Model builder will start training our Machine Learning Model. I've selected only 5 minutes (300 seconds). You can choose the duration for training as you want.
- Now our model has been trained successfully. Click on the Evaluate button to evaluate the new Cardiovascular Disease Detection MLModel.
- The Evaluate Window shows the performance of the best and worst algorithms on this dataset.
- Now, click on the code button to consume this model into our ASP.NET Core project. When the code window shows up, click on "Add Project". This will include two new build projects into our Visual Studio solution.
- Our ML Model project has been added to our Visual Studio solution. Now, we will use the shown ML.NET Model builder window to consume this model.
- So we have built, trained, and evaluated our model successfully. Now, it is time to integrate this and consume in our ASP.NET Core MVC project.
- Here is how our complete solution looks.
- Now, install Microsoft.ML in ASP.NET Core MVC project. Right-click on Dependencies and Select Manage Nuget Package and install Microsoft.ML or Open Nuget Package Manager console and use this command Install-Package Microsoft.ML.
- Now we will create our User Interface to interact with Cardiovascular Disease Detection Model. So, first of all, create a new controller name CardiovascularDisease and insert the following snippet.
- using Microsoft.AspNetCore.Mvc;
- using Cardiovascular_Disease_DetectionML.Model;
-
- namespace Cardiovascular_Disease_Detection.Controllers
- {
- public class CardiovascularDiseaseController : Controller
- {
- [HttpGet]
- public IActionResult Predict()
- {
- return View();
- }
- [HttpPost]
- public IActionResult Predict(ModelInput input)
- {
- var prediction = ConsumeModel.Predict(input);
- ViewBag.Result = prediction;
- return View();
- }
- }
- }
- Now we need a view that will be used to post input data from user and show output result to the user. Create a new view inside Views>CardiovascularDisease folder named Predict and paste the following snippet.
Now finally build your project and run.
Demo
Conclusion
So in this article, we learned how to build, train, evaluate and consume the Cardiovascular Disease Detection model using ML.NET Model builder and consume the resultant model into ASP.NET Core MVC application. Here is the overview.
- Setup a Prerequisite environment.
- Download the Cardiovascular Disease Detection dataset.
- Create ASP.NET Core MVC project template.
- Start adding a machine learning model for our project.
- Select Machine Learning scenario.
- Select the dataset file and predicted column label.
- Set training tasks and time to train the model.
- Train model.
- Evaluate model.
- Add model to ASP.NET Core Project.
- Create a User interface.
- Consume Cardiovascular Disease Detection model into ASP.NET Core MVC project.
- Finally, build and run the project and test the application using the first record from the dataset file.
Note
In this article, we have used ML.NET Model builder to build our Cardiovascular Disease Detection machine learning model.