Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this tutorial, you learn how to use ML.NET Model Builder to build a regression model to predict prices. The .NET console app that you develop in this tutorial predicts taxi fares based on historical New York taxi fare data.
The Model Builder price prediction template can be used for any scenario requiring a numerical prediction value. Example scenarios include: house price prediction, demand prediction, and sales forecasting.
For a list of prerequisites and installation instructions, visit the Model Builder installation guide.
Create a C# Console Application called "TaxiFarePrediction". Make sure Place solution and project in the same directory is unchecked.
Create a directory named Data in your project to store the data set files.
The data set used to train and evaluate the machine learning model is originally from the NYC TLC Taxi Trip data set.
To download the data set, navigate to the taxi-fare-train.csv download link.
When the page loads, right-click anywhere on the page and select Save as.
Use the Save As Dialog to save the file in the Data folder you created at the previous step.
In Solution Explorer, right-click the taxi-fare-train.csv file and select Properties. Under Advanced, change the value of Copy to Output Directory to Copy if newer.
Each row in the taxi-fare-train.csv
data set contains details of trips made by a taxi.
Open the taxi-fare-train.csv data set
The provided data set contains the following columns:
The label
is the column you want to predict. When performing a regression task, the goal is to predict a numerical value. In this price prediction scenario, the cost of a taxi ride is being predicted. Therefore, the fare_amount is the label. The identified features
are the inputs you give the model to predict the label
. In this case, the rest of the columns with the exception of trip_time_in_secs are used as features or inputs to predict the fare amount.
When first adding Model Builder to the solution it will prompt you to create an mbconfig
file. The mbconfig
file keeps track of everything you do in Model Builder to allow you to reopen the session.
mbconfig
project TaxiFarePrediction, and click the Add button.To train your model, you need to select from the list of available machine learning scenarios provided by Model Builder. In this case, the scenario is Value prediction
.
Model Builder can run the training on different environments depending on the scenario that was selected.
Local (CPU)
item is selected, and click the Next step button.Model Builder accepts data from two sources, a SQL Server database or a local file in csv or tsv format.
The machine learning task used to train the price prediction model in this tutorial is regression. During the model training process, Model Builder trains separate models using different regression algorithms and settings to find the best performing model for your dataset.
The time required for the model to train is proportionate to the amount of data. Model Builder automatically selects a default value for Time to train (seconds) based on the size of your data source.
Throughout the training process, progress data is displayed in the Training results
section of the train step.
Once training is complete the mbconfig
file will have the generated model called TaxiFarePrediction.zip
after training and two C# files with it:
Click the Next step button to navigate to the evaluate step.
The result of the training step will be one model which had the best performance. In the evaluate step of the Model Builder tool, in the Best model section, will contain the algorithm used by the best performing model in the Model entry along with metrics for that model in RSquared.
Additionally, in the Output window of Visual Studio, there will be a summary table containing top models and their metrics.
This section will also allow you to test your model by performing a single prediction. It will offer text boxes to fill in values and you can click the Predict button to get a prediction from the best model. By default this will be filled in by a random row in your dataset.
If you're not satisfied with your accuracy metrics, some easy ways to try and improve model accuracy are to increase the amount of time to train the model or use more data. Otherwise, click Next step to navigate to the consume step.
This step will have project templates that you can use to consume the model. This step is optional and you can choose the method that best suits your needs on how to serve the model.
When adding a console app to your solution, you will be prompted to name the project.
Name the console project TaxiFare_Console.
Click Add to solution to add the project to your current solution.
Run the application.
The output generated by the program should look similar to the snippet below:
Predicted Fare: 15.020833
When adding a web API to your solution, you will be prompted to name the project.
Name the Web API project TaxiFare_API.
Click Add to solution* to add the project to your current solution.
Run the application.
Open PowerShell and enter the following code where PORT is the port your application is listening on.
$body = @{
Vendor_id="CMT"
Rate_code=1.0
Passenger_count=1.0
Trip_distance=3.8
Payment_type="CRD"
}
Invoke-RestMethod "https://localhost:<PORT>/predict" -Method Post -Body ($body | ConvertTo-Json) -ContentType "application/json"
If successful, the output should look similar to the text below:
score
-----
15.020833
To learn more about topics mentioned in this tutorial, visit the following resources:
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Train a machine learning model for predictive maintenance by using ML.NET Model Builder - Training
In this module, you'll learn how to use ML.NET Model Builder to train and consume a machine learning model for predictive maintenance.
Certification
Microsoft Certified: Azure Data Scientist Associate - Certifications
Manage data ingestion and preparation, model training and deployment, and machine learning solution monitoring with Python, Azure Machine Learning and MLflow.