Problem
This problem is centered around predicting whether a person is suffering from
Chronic Kidney Disease or not. It is very important for doctors to diagnose Chronic Kidney Disease in patients so that proper treatment can be started.
The input
dataset of Chronic Kidney Disease contains 25 attributes having 400 instances of patient data which can be collected nearly in 2 months of period.
Dataset
The dataset is originally sourced from Dr P. Soundarapandian M.D, D.M (Senior Consultant Nephrologist), Apollo Hospitals,
India on July 2015.
Attribute Information
- age - age in years
- bp - blood pressure in mm/Hg
- sg - specific gravity - (1.005,1.010,1.015,1.020,1.025)
- al - albumin - (0,1,2,3,4,5)
- su - sugar - (0,1,2,3,4,5)
- rbc - red blood cells - (normal,abnormal)
- pc - pus cell - (normal,abnormal)
- pcc - pus cell clumps - (normal,abnormal)
- ba - bacteria - (present,notpresent)
- bgr - blood glucose random - mgs/dl
- bu - blood urea in mgs/dl
- sc - serum creatinine mgs/dl
- sod - sodium mEq/L
- pot - potassium mEq/L
- hemo - hemoglobin in gms
- pcv - packed cell volume
- wc - white blood cell count in cells/cumm
- rc - red blood cell count in million/cmm
- htn - hypertension - (yes, no)
- dm - diabetes mellitus - (yes, no)
- cad - coronary artery disease - (yes, no)
- appet - appetite - (good, poor)
- pe - pedal edema - (yes, no)
- ane - anemia - (yes, no)
- class - class - (ckd, notckd)
I've downloaded this datset from
UCI Machine Learning Repository. Originally the dataset file had Attribute Relation File Format but I've converted this into Comma Seprated Value file to use with
Microsoft ML.NET. Plese use this preprocessed dataset file to avoid any issues while building ML model
Kidney Disease Dataset because any empty or null value may create problems.
Solution
To solve this problem, first, we will build an ML model. Then we will train the model on existing data, evaluate how good it is, and lastly, we'll consume the model to predict our evaluation.
Prerequisites
- Visual Studio 2017 15.9.12 or later (I'm using VS2019)
- ML.NET Model Builder
- Chronic Kidney Disease dataset in CSV file with no empty or null values.
Let's start,
- Download and Install ML.NET Model Builder tool from Visual Studio Marketplace. Click Here
- After ML.NET Model Builder installation open your Visual Studio (in my case I'm using VS2019)
- Enter the project name and click on the Create button.
- Select ASP.NET Web Application (Model View Controller).
- Our Asp.NET Core MVC Web Application has been created successfully. Now we are going to 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 custom scenario because we are going to build our model from our Chronic Kidney Disease dataset.
- After data source selection, enter the time for which you want to train the model and click on Start training button, and the model training will be started for the entered time.
- After the model has been successfully trained click on the evaluation button.
- In evaluating tab you can see the output details of model builder showing the best algorithm and other performance measures of the ML model.
- We can also predict the Car evaluation in ML.NET Model builder. Add your data and click on the Predict button to see the predicted evaluation from the trained ML model.
- You can see the new build machine learning model evaluation. Click on code button to consume the model into your project.
- As you can see in solution explorer, the ML.NET Machine Learning model has been created.
- Now we need to create a new controller named "KidneyDiseasePrediction" and paste the below snippet code.
We will use the static Predict method of ConsumeModel class which will use the KidneyDisease machine learning model and return the predicted output.
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- using Chronic_Kidney_Disease_PredictionML.Model;
-
- namespace Chronic_Kidney_Disease_Prediction.Controllers
- {
- public class KidneyDiseasePredictionController : Controller
- {
- private readonly ILogger<KidneyDiseasePredictionController> _logger;
-
- public KidneyDiseasePredictionController(ILogger<KidneyDiseasePredictionController> logger)
- {
- _logger = logger;
- }
-
- public IActionResult Index()
- {
- return View();
- }
- [HttpPost]
- public IActionResult Index(ModelInput input)
- {
- ModelOutput prediction=ConsumeModel.Predict(input);
- ViewBag.Result = prediction;
- return View();
- }
- }
- }
Add a new view named Index in Views>KidneyDiseasePrediction Folder and copy the snippet code.
- Let's explore what's inside ConsumeModel.Predict method.
- First, it's creating MLContext and then creating and loading prediction engine or trained model from MLModel.zip file.
Then using the prediction engine to predict Kidney Disease Prediction on given input data.
- public class ConsumeModel
- {
-
-
- public static ModelOutput Predict(ModelInput input)
- {
-
-
- MLContext mlContext = new MLContext();
-
-
- string modelPath = AppDomain.CurrentDomain.BaseDirectory + "MLModel.zip";
- ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);
- var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
-
-
- ModelOutput result = predEngine.Predict(input);
- return result;
- }
- }
Demo
Summary
So in this article, we have learned how to build, train, evaluate and consume machine learning model using ML.NET.
We have solved this problem in the following below given steps,
- Install ML.NET Model Builder extension.
- Create ASP.NET Core MVC application using Visual Studio 2019
- Download Chronic Kidney Disease dataset and fill null or empty values with the default values for training our Machine Learning model. Please download this dataset for preprocessed data Kidney Disease dataset.
- Use ML.NET Model builder to build, train, evaluate and consume ML.NET ML model.
- Integrate ML.NET Machine Learning model into ASP.NET Core MVC application.
- Design Front End to make interaction with the user.
Source Code
You can also access the source code from my
Github repo.
Note