Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET

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 

  1. age - age in years
  2. bp - blood pressure in mm/Hg
  3. sg - specific gravity - (1.005,1.010,1.015,1.020,1.025)
  4. al - albumin - (0,1,2,3,4,5)
  5. su - sugar - (0,1,2,3,4,5)
  6. rbc - red blood cells - (normal,abnormal)
  7. pc - pus cell - (normal,abnormal)
  8. pcc - pus cell clumps - (normal,abnormal)
  9. ba - bacteria - (present,notpresent)
  10. bgr - blood glucose random - mgs/dl
  11. bu - blood urea in mgs/dl
  12. sc - serum creatinine mgs/dl
  13. sod - sodium mEq/L
  14. pot - potassium mEq/L
  15. hemo - hemoglobin in gms
  16. pcv - packed cell volume
  17. wc - white blood cell count in cells/cumm
  18. rc - red blood cell count in million/cmm
  19. htn - hypertension - (yes, no)
  20. dm - diabetes mellitus - (yes, no)
  21. cad - coronary artery disease - (yes, no)
  22. appet - appetite - (good, poor)
  23. pe - pedal edema - (yes, no)
  24. ane - anemia - (yes, no)
  25. 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.  
Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET
Prerequisites
  1. Visual Studio 2017 15.9.12 or later (I'm using VS2019) 
  2. ML.NET Model Builder
  3. 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)
Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET
  • Enter the project name and click on the Create button.
Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET
  • Select ASP.NET Web Application (Model View Controller).
Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET
  • 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.
Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET
  • 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.
Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET
Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET
  • 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. 
Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET
  • 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.
Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET
  •  As you can see in solution explorer, the ML.NET Machine Learning model has been created.
Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET
  • 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.
  1. using Microsoft.AspNetCore.Mvc;  
  2. using Microsoft.Extensions.Logging;  
  3. using Chronic_Kidney_Disease_PredictionML.Model;  
  4.   
  5. namespace Chronic_Kidney_Disease_Prediction.Controllers  
  6. {  
  7.     public class KidneyDiseasePredictionController : Controller  
  8.     {  
  9.         private readonly ILogger<KidneyDiseasePredictionController> _logger;  
  10.   
  11.         public KidneyDiseasePredictionController(ILogger<KidneyDiseasePredictionController> logger)  
  12.         {  
  13.             _logger = logger;  
  14.         }  
  15.   
  16.         public IActionResult Index()  
  17.         {  
  18.             return View();  
  19.         }  
  20.         [HttpPost]  
  21.         public IActionResult Index(ModelInput input)  
  22.         {  
  23.             ModelOutput prediction=ConsumeModel.Predict(input);  
  24.             ViewBag.Result = prediction;  
  25.             return View();  
  26.         }  
  27.     }  
  28. }  
 Add a new view named Index in Views>KidneyDiseasePrediction Folder and copy the snippet code.
  1. @model Chronic_Kidney_Disease_PredictionML.Model.ModelInput  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Predict";  
  5. }  
  6.     <div class="row">  
  7.         <form asp-action="Index">  
  8.             <div class="row">  
  9.                 <div class="col-md-4">  
  10.                     <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
  11.                     <div class="form-group">  
  12.                         <label asp-for="Age" class="control-label"></label>  
  13.                         <input asp-for="Age" class="form-control" />  
  14.                         <span asp-validation-for="Age" class="text-danger"></span>  
  15.                     </div>  
  16.                     <div class="form-group">  
  17.                         <label asp-for="Bp" class="control-label"></label>  
  18.                         <input asp-for="Bp" class="form-control" />  
  19.                         <span asp-validation-for="Bp" class="text-danger"></span>  
  20.                     </div>  
  21.                     <div class="form-group">  
  22.                         <label asp-for="Sg" class="control-label"></label>  
  23.                         <input asp-for="Sg" class="form-control" />  
  24.                         <span asp-validation-for="Sg" class="text-danger"></span>  
  25.                     </div>  
  26.                     <div class="form-group">  
  27.                         <label asp-for="Al" class="control-label"></label>  
  28.                         <input asp-for="Al" class="form-control" />  
  29.                         <span asp-validation-for="Al" class="text-danger"></span>  
  30.                     </div>  
  31.                     <div class="form-group">  
  32.                         <label asp-for="Su" class="control-label"></label>  
  33.                         <input asp-for="Su" class="form-control" />  
  34.                         <span asp-validation-for="Su" class="text-danger"></span>  
  35.                     </div>  
  36.                     <div class="form-group">  
  37.                         <label asp-for="Rbc" class="control-label"></label>  
  38.                         <input asp-for="Rbc" class="form-control" />  
  39.                         <span asp-validation-for="Rbc" class="text-danger"></span>  
  40.                     </div>  
  41.                     <div class="form-group">  
  42.                         <label asp-for="Pc" class="control-label"></label>  
  43.                         <input asp-for="Pc" class="form-control" />  
  44.                         <span asp-validation-for="Pc" class="text-danger"></span>  
  45.                     </div>  
  46.                 </div>  
  47.                 <div class="col-md-4">  
  48.                     <div class="form-group">  
  49.                         <label asp-for="Pcc" class="control-label"></label>  
  50.                         <input asp-for="Pcc" class="form-control" />  
  51.                         <span asp-validation-for="Pcc" class="text-danger"></span>  
  52.                     </div>  
  53.                     <div class="form-group">  
  54.                         <label asp-for="Ba" class="control-label"></label>  
  55.                         <input asp-for="Ba" class="form-control" />  
  56.                         <span asp-validation-for="Ba" class="text-danger"></span>  
  57.                     </div>  
  58.                     <div class="form-group">  
  59.                         <label asp-for="Bgr" class="control-label"></label>  
  60.                         <input asp-for="Bgr" class="form-control" />  
  61.                         <span asp-validation-for="Bgr" class="text-danger"></span>  
  62.                     </div>  
  63.                     <div class="form-group">  
  64.                         <label asp-for="Bu" class="control-label"></label>  
  65.                         <input asp-for="Bu" class="form-control" />  
  66.                         <span asp-validation-for="Bu" class="text-danger"></span>  
  67.                     </div>  
  68.                     <div class="form-group">  
  69.                         <label asp-for="Sc" class="control-label"></label>  
  70.                         <input asp-for="Sc" class="form-control" />  
  71.                         <span asp-validation-for="Sc" class="text-danger"></span>  
  72.                     </div>  
  73.                     <div class="form-group">  
  74.                         <label asp-for="Sod" class="control-label"></label>  
  75.                         <input asp-for="Sod" class="form-control" />  
  76.                         <span asp-validation-for="Sod" class="text-danger"></span>  
  77.                     </div>  
  78.                     <div class="form-group">  
  79.                         <label asp-for="Pot" class="control-label"></label>  
  80.                         <input asp-for="Pot" class="form-control" />  
  81.                         <span asp-validation-for="Pot" class="text-danger"></span>  
  82.                     </div>  
  83.                 </div>  
  84.                 <div class="col-md-4">  
  85.                     <div class="form-group">  
  86.                         <label asp-for="Hemo" class="control-label"></label>  
  87.                         <input asp-for="Hemo" class="form-control" />  
  88.                         <span asp-validation-for="Hemo" class="text-danger"></span>  
  89.                     </div>  
  90.                     <div class="form-group">  
  91.                         <label asp-for="Pcv" class="control-label"></label>  
  92.                         <input asp-for="Pcv" class="form-control" />  
  93.                         <span asp-validation-for="Pcv" class="text-danger"></span>  
  94.                     </div>  
  95.                     <div class="form-group">  
  96.                         <label asp-for="Wbcc" class="control-label"></label>  
  97.                         <input asp-for="Wbcc" class="form-control" />  
  98.                         <span asp-validation-for="Wbcc" class="text-danger"></span>  
  99.                     </div>  
  100.                     <div class="form-group">  
  101.                         <label asp-for="Rbcc" class="control-label"></label>  
  102.                         <input asp-for="Rbcc" class="form-control" />  
  103.                         <span asp-validation-for="Rbcc" class="text-danger"></span>  
  104.                     </div>  
  105.                     <div class="form-group form-check">  
  106.                         <label class="form-check-label">  
  107.                             <input class="form-check-input" asp-for="Htn" /> @Html.DisplayNameFor(model => model.Htn)  
  108.                         </label>  
  109.                     </div>  
  110.                     <div class="form-group form-check">  
  111.                         <label class="form-check-label">  
  112.                             <input class="form-check-input" asp-for="Dm" /> @Html.DisplayNameFor(model => model.Dm)  
  113.                         </label>  
  114.                     </div>  
  115.                     <div class="form-group form-check">  
  116.                         <label class="form-check-label">  
  117.                             <input class="form-check-input" asp-for="Cad" /> @Html.DisplayNameFor(model => model.Cad)  
  118.                         </label>  
  119.                     </div>  
  120.                     <div class="form-group">  
  121.                         <label asp-for="Appet" class="control-label"></label>  
  122.                         <input asp-for="Appet" class="form-control" />  
  123.                         <span asp-validation-for="Appet" class="text-danger"></span>  
  124.                     </div>  
  125.                     <div class="form-group form-check">  
  126.                         <label class="form-check-label">  
  127.                             <input class="form-check-input" asp-for="Pe" /> @Html.DisplayNameFor(model => model.Pe)  
  128.                         </label>  
  129.                     </div>  
  130.                     <div class="form-group form-check">  
  131.                         <label class="form-check-label">  
  132.                             <input class="form-check-input" asp-for="Ane" /> @Html.DisplayNameFor(model => model.Ane)  
  133.                         </label>  
  134.                     </div>  
  135.                 </div>  
  136.             </div>  
  137.             <div class="form-group">  
  138.                 <input type="submit" value="Predict" class="btn btn-primary" />  
  139.             </div>  
  140.         </form>  
  141.         @if (ViewBag.Result != null)  
  142.         {  
  143.             <div class="offset-md-1 col-md-3">  
  144.                 <label>Have Chronic Kidney Disease:</label>  
  145.                 <p>@ViewBag.Result.Prediction</p>  
  146.                 <label>Score:</label>  
  147.                 <p>@ViewBag.Result.Score</p>  
  148.             </div>  
  149.         }  
  150.   
  151.     </div>
  152. @section Scripts {  
  153.     @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}  
  154. }  
  • 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.
  1. public class ConsumeModel  
  2.     {  
  3.         // For more info on consuming ML.NET models, visit https://aka.ms/model-builder-consume  
  4.         // Method for consuming model in your app  
  5.         public static ModelOutput Predict(ModelInput input)  
  6.         {  
  7.   
  8.             // Create new MLContext  
  9.             MLContext mlContext = new MLContext();  
  10.   
  11.             // Load model & create prediction engine  
  12.             string modelPath = AppDomain.CurrentDomain.BaseDirectory + "MLModel.zip";  
  13.             ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);  
  14.             var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);  
  15.   
  16.             // Use model to make prediction on input data  
  17.             ModelOutput result = predEngine.Predict(input);  
  18.             return result;  
  19.         }  
  20.     }  

Demo

 
Chronic Kidney Disease Prediction In ASP.NET Core Using ML.NET
 

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
For more details about ML.NET Model Builder please visit Model Builder guide. For more details about ML.NET framework please visit Developer Docs


Similar Articles
Finchship
We Provide Web, Desktop and Mobile Apps Solution