# Price Prediction In ASP.NET Core Using ML.NET

## Problem

This problem is centered around predicting the fare of a taxi trip in New York City. At first glance, it may seem to depend simply on the distance traveled. However, taxi vendors in New York charge varying amounts for other factors such as additional passengers, paying with a credit card instead of cash and so on. This prediction can be used in the application for taxi providers to give users and drivers an estimate on ride fares.

To solve this problem, we will build an ML model that takes as inputs,
• vendor ID
• rate code
• passenger count
• trip time
• trip distance
• payment type
and predicts the fare of the ride.

## ML task - Regression

The generalized problem of regression is to predict some continuous value for given parameters, for example,
• predict a house price based on the number of rooms, location, year built, etc.
• predict car fuel consumption based on fuel type and car parameters.
• predict a time estimate for fixing an issue based on issue attributes.
The common feature for all those examples is that the parameter we want to predict can take any numeric value in a certain range. In other words, this value is represented by `integer` or `float`/`double`, not by `enum` or `boolean` types.

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 taxi fares.

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 add data from the file as well as using SQL Server Database. I will use wikiDetoxAnnotatedRows data to train my model. You can download it from here. After data source selection 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.

•  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, the machine learning model for price prediction has been created. Now, first, we need to add the reference Price PredictionML.Model project into our Price Prediction Web project and also add ML.NET package from NuGet.

Create a new empty controller named Prediction and copy below the snippet code.
1. using Microsoft.AspNetCore.Mvc;
2. using Microsoft.ML;
3. using PricePredictionML.Model.DataModels;
4.
5. namespace Price_Prediction.Controllers
6. {
7.     public class PredictionController : Controller
8.     {
9.         public IActionResult Price(ModelInput input)
10.         {
11.             // Load the model
12.             MLContext mlContext = new MLContext();
13.             // Create predection engine related to the loaded train model
14.             ITransformer mlModel = mlContext.Model.Load(@"..\Price PredictionML.Model\MLModel.zip"out var modelInputSchema);
15.             var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
16.
17.             // Try model on sample data to predict fair price
18.             ModelOutput result = predEngine.Predict(input);
19.
20.             ViewBag.Price = result.Score;
21.             return View(input);
22.         }
23.     }
24. }
Add a new view named Price in Prediction Folder and copy the snippet code.
1. @model PricePredictionML.Model.DataModels.ModelInput
2.
3. @{
4.     ViewData["Title"] = "Price Prediction Using ML.NET";
5. }
6.
7. <h2>Price Prediction using ML.NET</h2>
8.
9. <hr />
10. <div class="row">
11.     <div class="col-md-4">
12.         <form asp-action="Price">
13.             <div asp-validation-summary="ModelOnly" class="text-danger"></div>
14.
15.             <div class="form-group">
16.                 <label asp-for="Passenger_count" class="control-label"></label>
17.                 <input asp-for="Passenger_count" type="number" class="form-control" />
18.                 <span asp-validation-for="Passenger_count" class="text-danger"></span>
19.             </div>
20.             <div class="form-group">
21.                 <label asp-for="Trip_time_in_secs" class="control-label"></label>
22.                 <input asp-for="Trip_time_in_secs" class="form-control" />
23.                 <span asp-validation-for="Trip_time_in_secs" class="text-danger"></span>
24.             </div>
25.             <div class="form-group">
26.                 <label asp-for="Trip_distance" class="control-label"></label>
27.                 <input asp-for="Trip_distance" class="form-control" />
28.                 <span asp-validation-for="Trip_distance" class="text-danger"></span>
29.             </div>
30.             <div class="form-group">
31.                 <label asp-for="Payment_type" class="control-label"></label>
32.                 <select asp-for="Payment_type" class="form-control">
33.                     <option value="CSH">Cash</option>
34.                     <option value="CRD">Credit</option>
35.                 </select>
36.                 <span asp-validation-for="Payment_type" class="text-danger"></span>
37.             </div>
38.             <div class="form-group">
39.                 <input type="submit" value="Check" class="btn btn-default" />
40.             </div>
41.         </form>
42.     </div>
43.     <div class="col-md-offset-2 col-md-4">
44.         @if(ViewBag.Price!=null)
45.         {
46.             <h1>Result</h1>
47.             <h2>Fare Price:@ViewBag.Price</h2>
48.         }
49.     </div>
50. </div>
51.
52. @section Scripts {
53.     @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
54. }
Demo

## Conclusion

So we have completed our basic ASP.NET Core MVC solution which consumes Price PredictionML.NET model to predict taxi fair prices.

Source Code

You can also access the source code from Github.

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.