Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Cardiovascular Disease Detection In ASP.NET Core Using ML.NET
WhatsApp
Habibul Rehman
5y
11.8k
0
5
100
Article
CardiovascularDiseaseDetection.zip
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 Cardiovascular Disease Dataset training data is based on a public
dataset available at Kaggle
by
Svetlana Ulianova
.
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.
@model Cardiovascular_Disease_DetectionML.Model.ModelInput
@{
ViewData[
"Title"
] =
"Cardiovascular Diseasese Prediction"
;
}
<h2>Cardiovascular Disease Detection
in
ASP.NET Core Using ML.NET</h2>
<hr />
@
if
(ViewBag.Result !=
null
)
{
<div
class
=
"row"
>
<div
class
=
"col-md-6"
>
<h4>Prediction:@ViewBag.Result.Prediction</h4>
</div>
<div
class
=
"col-md-6"
>
<h4>Score:@ViewBag.Result.Score</h4>
</div>
</div>
<hr />
}
<div
class
=
"row"
>
<div
class
=
"col-md-12"
>
<form asp-action=
"Predict"
>
<div asp-validation-summary=
"ModelOnly"
class
=
"text-danger"
></div>
<div
class
=
"row"
>
<div
class
=
"form-group col-md-4"
>
<label asp-
for
=
"Age"
class
=
"control-label"
></label>
<input asp-
for
=
"Age"
class
=
"form-control"
/>
<span asp-validation-
for
=
"Age"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-4"
>
<label asp-
for
=
"Gender"
class
=
"control-label"
></label>
<select asp-
for
=
"Gender"
class
=
"form-control"
>
<option value=
"1"
>Woman</option>
<option value=
"2"
>Man</option>
</select>
<span asp-validation-
for
=
"Gender"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-4"
>
<label asp-
for
=
"Height"
class
=
"control-label"
></label>
<input asp-
for
=
"Height"
class
=
"form-control"
/>
<span asp-validation-
for
=
"Height"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-4"
>
<label asp-
for
=
"Weight"
class
=
"control-label"
></label>
<input asp-
for
=
"Weight"
class
=
"form-control"
/>
<span asp-validation-
for
=
"Weight"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-4"
>
<label asp-
for
=
"Ap_hi"
class
=
"control-label"
></label>
<input asp-
for
=
"Ap_hi"
class
=
"form-control"
/>
<span asp-validation-
for
=
"Ap_hi"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-4"
>
<label asp-
for
=
"Ap_lo"
class
=
"control-label"
></label>
<input asp-
for
=
"Ap_lo"
class
=
"form-control"
/>
<span asp-validation-
for
=
"Ap_lo"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-4"
>
<label asp-
for
=
"Cholesterol"
class
=
"control-label"
></label>
<select asp-
for
=
"Cholesterol"
class
=
"form-control"
>
<option value=
"1"
>Normal</option>
<option value=
"2"
>Above Normal</option>
<option value=
"3"
>Well Above Normal</option>
</select>
<span asp-validation-
for
=
"Cholesterol"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-4"
>
<label asp-
for
=
"Gluc"
class
=
"control-label"
></label>
<select asp-
for
=
"Gluc"
class
=
"form-control"
>
<option value=
"1"
>Normal</option>
<option value=
"2"
>Above Normal</option>
<option value=
"3"
>Well Above Normal</option>
</select>
<span asp-validation-
for
=
"Gluc"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-4"
>
<label asp-
for
=
"Smoke"
class
=
"control-label"
></label>
<select asp-
for
=
"Smoke"
class
=
"form-control"
>
<option value=
"1"
>Smoke</option>
<option value=
"0"
>Not Smoke</option>
</select>
<span asp-validation-
for
=
"Smoke"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-4"
>
<label asp-
for
=
"Alco"
class
=
"control-label"
></label>
<select asp-
for
=
"Alco"
class
=
"form-control"
>
<option value=
"1"
>Intake</option>
<option value=
"0"
>Not Intake</option>
</select>
<span asp-validation-
for
=
"Alco"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group col-md-4"
>
<label asp-
for
=
"Active"
class
=
"control-label"
></label>
<select asp-
for
=
"Active"
class
=
"form-control"
>
<option value=
"1"
>True</option>
<option value=
"0"
>False</option>
</select>
<span asp-validation-
for
=
"Active"
class
=
"text-danger"
></span>
</div>
</div>
<div
class
=
"form-group text-right"
>
<input type=
"submit"
value=
"Predict"
class
=
"btn btn-primary"
/>
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync(
"_ValidationScriptsPartial"
);}
}
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.
You can also access the complete project source code from my GitHub repository
habib-developer/Cardiovascular-Disease-Detection
.
For more information about the training dataset please visit
kaggle Cardiovascular Disease dataset.
ASP.NET Core
Machine Learning
ML.NET
Visual Studio
Up Next
Ebook Download
View all
Yatharth Machine Learning
Read by 335 people
Download Now!
Learn
View all
Finchship
We Provide Web, Desktop and Mobile Apps Solution
Membership not found