Car Evaluation In ASP.NET Core Using ML.NET

Problem

 
This problem is centred around making the Car evaluation and predicting the Car condition.
 
To solve this problem, we will build an ML model that takes as inputs,
  • buying: vhigh, high, med, low.
  • maint: vhigh, high, med, low.
  • doors: 2, 3, 4, 5more.
  • persons: 2, 4, more.
  • lug_boot: small, med, big.
  • safety: low, med, high.
and predicts the evaluation of the Car.
 
Prerequisites
  • Visual Studio 2019 or 2017
  • ML.NET Model Builder Extension for Visual Studio

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 Car evaluation. 
Car Evaluation In ASP.NET Core Using ML.NET
So 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)
Car Evaluation In ASP.NET Core Using ML.NET
  • After Project has been selected, enter your Project Name.
Car Evaluation In ASP.NET Core Using ML.NET
  • Select Asp.Net Core template which you want to use, I'm using Web Application MVC.  
Car Evaluation In ASP.NET Core Using ML.NET
  • After the project has been created, we will start to build our model. Right-click on Project > Add > Machine Learning, 
Car Evaluation In ASP.NET Core Using ML.NET
  • After the project has been created, we will start to build our model. Right-click on Project > Add > Machine Learning,
Car Evaluation In ASP.NET Core Using ML.NET
  • After scenario selection, we will select the data set that will be used to train our model. You may add data from the file as well as using SQL Server Database. I will use car.csv data to train my model. Originally I've downloaded this dataset from UCI Machine Learning Repo Car Evaluation Dataset. You can download it from here. Select class column for prediction label and select input columns, after that click on Train link,
Car Evaluation 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. 
Car Evaluation 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.
Car Evaluation In ASP.NET Core Using ML.NET
  • You can see the new build machine learning model evaluation. Click on code button to consume the model into your project.
Car Evaluation In ASP.NET Core Using ML.NET
  • As you can see in solution explorer, the ML.NET Machine Learning model has been created. In code tab window you can see the code snippet that can be used to consume the ML model into ASP.NET MVC project.
Car Evaluation In ASP.NET Core Using ML.NET
  • Now, the machine learning model for Car evaluation has been built. Now, first, we need to add the reference Car EvaluationML.Model project into our Car Evaluation Web project. Right-click on Car Evaluation Web project and select add > reference.
  • Select Car EvaluationML.Model project and click ok.
Car Evaluation In ASP.NET Core Using ML.NET
  • Now we need to create a new controller named "CarEvaluation" and paste the below snippet code.
We will use the static Predict method of ConsumeModel class which will use the CarEvaluation machine learning model and return the predicted output.
  1. using Microsoft.AspNetCore.Mvc;  
  2. using Microsoft.Extensions.Logging;  
  3. using Car_EvaluationML.Model;  
  4.   
  5. namespace Car_Evaluation.Controllers  
  6. {  
  7.     public class CarEvaluationController : Controller  
  8.     {  
  9.         private readonly ILogger<CarEvaluationController> _logger;  
  10.   
  11.         public CarEvaluationController(ILogger<CarEvaluationController> logger)  
  12.         {  
  13.             _logger = logger;  
  14.         }  
  15.         public ActionResult Index()  
  16.         {  
  17.             return View();  
  18.         }  
  19.         [HttpPost]  
  20.         public ActionResult Index(ModelInput input)  
  21.         {  
  22.             ModelOutput prediction=ConsumeModel.Predict(input);  
  23.             ViewBag.Prediction = prediction;  
  24.             return View();  
  25.         }  
  26.     }  
  27. }  
Add a new view named Index in CarEvaluation Folder and copy the snippet code.
  1. @model Car_EvaluationML.Model.ModelInput  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Evaluate";  
  5. }  
  6.   
  7. <h4>Evaluate</h4>  
  8. <hr />  
  9. <div class="row">  
  10.     <div class="col-md-4">  
  11.         <form asp-action="Index">  
  12.             <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
  13.             <div class="form-group">  
  14.                 <label asp-for="Buying" class="control-label"></label>  
  15.                 <select asp-for="Buying" class="form-control">  
  16.                     <option value="vhigh">VHigh</option>  
  17.                     <option value="high">High</option>  
  18.                     <option value="med">Medium</option>  
  19.                     <option value="low">Low</option>  
  20.                 </select>  
  21.                 <span asp-validation-for="Buying" class="text-danger"></span>  
  22.             </div>  
  23.             <div class="form-group">  
  24.                 <label asp-for="Maint" class="control-label"></label>  
  25.                 <select asp-for="Maint" class="form-control">  
  26.                     <option value="vhigh">VHigh</option>  
  27.                     <option value="high">High</option>  
  28.                     <option value="med">Medium</option>  
  29.                     <option value="low">Low</option>  
  30.                 </select>  
  31.                 <span asp-validation-for="Maint" class="text-danger"></span>  
  32.             </div>  
  33.             <div class="form-group">  
  34.                 <label asp-for="Doors" class="control-label"></label>  
  35.                 <select asp-for="Doors" class="form-control">  
  36.                     <option value="2">Two</option>  
  37.                     <option value="3">Three</option>  
  38.                     <option value="4">Four</option>  
  39.                     <option value="5more">Five or More</option>  
  40.                 </select>  
  41.                 <span asp-validation-for="Doors" class="text-danger"></span>  
  42.             </div>  
  43.             <div class="form-group">  
  44.                 <label asp-for="Persons" class="control-label"></label>  
  45.                 <select asp-for="Persons" class="form-control">  
  46.                     <option value="2">Two</option>  
  47.                     <option value="4">Four</option>  
  48.                     <option value="more">More</option>  
  49.                 </select>  
  50.                 <span asp-validation-for="Persons" class="text-danger"></span>  
  51.             </div>  
  52.             <div class="form-group">  
  53.                 <label asp-for="Bug_boot" class="control-label"></label>  
  54.                 <select asp-for="Bug_boot" class="form-control">  
  55.                     <option value="small">Small</option>  
  56.                     <option value="med">Medium</option>  
  57.                     <option value="big">Big</option>  
  58.                 </select>  
  59.                 <span asp-validation-for="Bug_boot" class="text-danger"></span>  
  60.             </div>  
  61.             <div class="form-group">  
  62.                 <label asp-for="Safety" class="control-label"></label>  
  63.                 <select asp-for="Safety" class="form-control">  
  64.                     <option value="low">Low</option>  
  65.                     <option value="med">Medium</option>  
  66.                     <option value="high">High</option>  
  67.                 </select>  
  68.                 <span asp-validation-for="Safety" class="text-danger"></span>  
  69.             </div>  
  70.             <div class="form-group">  
  71.                 <input type="submit" value="Evaluate" class="btn btn-primary" />  
  72.             </div>  
  73.         </form>  
  74.     </div>  
  75.     <div class="offset-2 col-md-4">  
  76.         <div class="form-group">  
  77.             @if (ViewBag.Prediction != null)  
  78.             {  
  79.                 <label>Class:</label>  
  80.                 <h6>@ViewBag.Prediction.Prediction</h6>  
  81.                 <label>Score:</label>  
  82.         <h6>  
  83.             @foreach (var item in ViewBag.Prediction.Score as float[])  
  84.             {  
  85.             <span>@item ,</span>  
  86.             }  
  87.         </h6>  
  88.             }  
  89.         </div>  
  90.     </div>  
  91. </div>  
  92. @section Scripts {  
  93.     @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}  
  94. }  
  • Let's explore what's inside ConsumeModel.Predict method.
  • First, it's creating MLContext and then creating and loading prediction engine from MLModel.zip file.
Then using prediction engine to predict Car Evaluation on given input data.
  1. public static ModelOutput Predict(ModelInput input)  
  2. {  
  3.   
  4.             // Create new MLContext  
  5.             MLContext mlContext = new MLContext();  
  6.   
  7.             // Load model & create prediction engine  
  8.             string modelPath = AppDomain.CurrentDomain.BaseDirectory + "MLModel.zip";  
  9.             ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);  
  10.             var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);  
  11.   
  12.             // Use model to make prediction on input data  
  13.             ModelOutput result = predEngine.Predict(input);  
  14.             return result;  
  15. }  

Demo

 
Car Evaluation 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 Car Evaluation dataset for training our Machine Learning model.
  • 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