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.
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.ML;
- using PricePredictionML.Model.DataModels;
-
- namespace Price_Prediction.Controllers
- {
- public class PredictionController : Controller
- {
- public IActionResult Price(ModelInput input)
- {
-
- MLContext mlContext = new MLContext();
-
- ITransformer mlModel = mlContext.Model.Load(@"..\Price PredictionML.Model\MLModel.zip", out var modelInputSchema);
- var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
-
-
- ModelOutput result = predEngine.Predict(input);
-
- ViewBag.Price = result.Score;
- return View(input);
- }
- }
- }
Add a new view named Price in Prediction Folder and copy the snippet code.
- @model PricePredictionML.Model.DataModels.ModelInput
-
- @{
- ViewData["Title"] = "Price Prediction Using ML.NET";
- }
-
- <h2>Price Prediction using ML.NET</h2>
-
- <hr />
- <div class="row">
- <div class="col-md-4">
- <form asp-action="Price">
- <div asp-validation-summary="ModelOnly" class="text-danger"></div>
-
- <div class="form-group">
- <label asp-for="Passenger_count" class="control-label"></label>
- <input asp-for="Passenger_count" type="number" class="form-control" />
- <span asp-validation-for="Passenger_count" class="text-danger"></span>
- </div>
- <div class="form-group">
- <label asp-for="Trip_time_in_secs" class="control-label"></label>
- <input asp-for="Trip_time_in_secs" class="form-control" />
- <span asp-validation-for="Trip_time_in_secs" class="text-danger"></span>
- </div>
- <div class="form-group">
- <label asp-for="Trip_distance" class="control-label"></label>
- <input asp-for="Trip_distance" class="form-control" />
- <span asp-validation-for="Trip_distance" class="text-danger"></span>
- </div>
- <div class="form-group">
- <label asp-for="Payment_type" class="control-label"></label>
- <select asp-for="Payment_type" class="form-control">
- <option value="CSH">Cash</option>
- <option value="CRD">Credit</option>
- </select>
- <span asp-validation-for="Payment_type" class="text-danger"></span>
- </div>
- <div class="form-group">
- <input type="submit" value="Check" class="btn btn-default" />
- </div>
- </form>
- </div>
- <div class="col-md-offset-2 col-md-4">
- @if(ViewBag.Price!=null)
- {
- <h1>Result</h1>
- <h2>Fare Price:@ViewBag.Price</h2>
- }
- </div>
- </div>
-
- @section Scripts {
- @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
- }
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