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
Car Evaluation In ASP.NET Core Using ML.NET
WhatsApp
Habibul Rehman
5y
15.6k
0
13
300
Article
CarEvaluation.zip
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.
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)
After Project has been selected, enter your Project Name.
Select Asp.Net Core template which you want to use, I'm using Web Application MVC.
After the project has been created, we will start to build our model. Right-click on Project > Add > Machine Learning,
After the project has been created, we will start to build our model. Right-click on Project > Add > Machine Learning,
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,
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. In code tab window you can see the code snippet that can be used to consume the ML model into ASP.NET MVC project.
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.
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.
using
Microsoft.AspNetCore.Mvc;
using
Microsoft.Extensions.Logging;
using
Car_EvaluationML.Model;
namespace
Car_Evaluation.Controllers
{
public
class
CarEvaluationController : Controller
{
private
readonly
ILogger<CarEvaluationController> _logger;
public
CarEvaluationController(ILogger<CarEvaluationController> logger)
{
_logger = logger;
}
public
ActionResult Index()
{
return
View();
}
[HttpPost]
public
ActionResult Index(ModelInput input)
{
ModelOutput prediction=ConsumeModel.Predict(input);
ViewBag.Prediction = prediction;
return
View();
}
}
}
Add a new view named Index in CarEvaluation Folder and copy the snippet code.
@model Car_EvaluationML.Model.ModelInput
@{
ViewData[
"Title"
] =
"Evaluate"
;
}
<h4>Evaluate</h4>
<hr />
<div
class
=
"row"
>
<div
class
=
"col-md-4"
>
<form asp-action=
"Index"
>
<div asp-validation-summary=
"ModelOnly"
class
=
"text-danger"
></div>
<div
class
=
"form-group"
>
<label asp-
for
=
"Buying"
class
=
"control-label"
></label>
<select asp-
for
=
"Buying"
class
=
"form-control"
>
<option value=
"vhigh"
>VHigh</option>
<option value=
"high"
>High</option>
<option value=
"med"
>Medium</option>
<option value=
"low"
>Low</option>
</select>
<span asp-validation-
for
=
"Buying"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group"
>
<label asp-
for
=
"Maint"
class
=
"control-label"
></label>
<select asp-
for
=
"Maint"
class
=
"form-control"
>
<option value=
"vhigh"
>VHigh</option>
<option value=
"high"
>High</option>
<option value=
"med"
>Medium</option>
<option value=
"low"
>Low</option>
</select>
<span asp-validation-
for
=
"Maint"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group"
>
<label asp-
for
=
"Doors"
class
=
"control-label"
></label>
<select asp-
for
=
"Doors"
class
=
"form-control"
>
<option value=
"2"
>Two</option>
<option value=
"3"
>Three</option>
<option value=
"4"
>Four</option>
<option value=
"5more"
>Five or More</option>
</select>
<span asp-validation-
for
=
"Doors"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group"
>
<label asp-
for
=
"Persons"
class
=
"control-label"
></label>
<select asp-
for
=
"Persons"
class
=
"form-control"
>
<option value=
"2"
>Two</option>
<option value=
"4"
>Four</option>
<option value=
"more"
>More</option>
</select>
<span asp-validation-
for
=
"Persons"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group"
>
<label asp-
for
=
"Bug_boot"
class
=
"control-label"
></label>
<select asp-
for
=
"Bug_boot"
class
=
"form-control"
>
<option value=
"small"
>Small</option>
<option value=
"med"
>Medium</option>
<option value=
"big"
>Big</option>
</select>
<span asp-validation-
for
=
"Bug_boot"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group"
>
<label asp-
for
=
"Safety"
class
=
"control-label"
></label>
<select asp-
for
=
"Safety"
class
=
"form-control"
>
<option value=
"low"
>Low</option>
<option value=
"med"
>Medium</option>
<option value=
"high"
>High</option>
</select>
<span asp-validation-
for
=
"Safety"
class
=
"text-danger"
></span>
</div>
<div
class
=
"form-group"
>
<input type=
"submit"
value=
"Evaluate"
class
=
"btn btn-primary"
/>
</div>
</form>
</div>
<div
class
=
"offset-2 col-md-4"
>
<div
class
=
"form-group"
>
@
if
(ViewBag.Prediction !=
null
)
{
<label>Class:</label>
<h6>@ViewBag.Prediction.Prediction</h6>
<label>Score:</label>
<h6>
@
foreach
(var item
in
ViewBag.Prediction.Score
as
float
[])
{
<span>@item ,</span>
}
</h6>
}
</div>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync(
"_ValidationScriptsPartial"
);}
}
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.
public
static
ModelOutput Predict(ModelInput input)
{
// Create new MLContext
MLContext mlContext =
new
MLContext();
// Load model & create prediction engine
string
modelPath = AppDomain.CurrentDomain.BaseDirectory +
"MLModel.zip"
;
ITransformer mlModel = mlContext.Model.Load(modelPath,
out
var modelInputSchema);
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
// Use model to make prediction on input data
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 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
.
ASP.NET Core MVC
Machine Learning
ML.NET
ML.NET Model Builder
Visual Studio
Up Next
Ebook Download
View all
Machine Learning for Future Engineers
Read by 718 people
Download Now!
Learn
View all
Finchship
We Provide Web, Desktop and Mobile Apps Solution
Membership not found