From the course: Machine Learning with ML.NET

Build an ML model for predicting taxi fares - .NET Tutorial

From the course: Machine Learning with ML.NET

Build an ML model for predicting taxi fares

- In this video, we will look at how to build a custom machine learning model for predicting taxi fares. What is a price prediction? Price prediction is about predicting a taxi fare or a fair amount based on some input data. For example, how long was the taxi ride? How many customers or riders were in the taxi? And so here's an example of a data set from the New York taxi fare which captures the historical data of all the rides that happened. In this case, let us understand how would price prediction work. Price prediction is an example of a regression machine learning task which can be used to predict a number for example, predicting taxi fare, predicting house prices or anything else. The prediction for taxi fare works as follows. You have some features which go into the training process. And so for example, this features can determine how high or low was the fair amount. And for example, if the distance traveled was very high then most likely the fair is going to be big as well. And then the fair amount is your label or output column or the column that you want to predict. So this is the outcome of calling a machine learning model. Now that we have understood the basics of predicting taxi fares, let's look at how can you build a custom machine learning model for this scenario. This is a console app that I have and I'm going to use this to build a price prediction model. I've already downloaded the model builder visual studio tool. So I can right click my app and say, add machine learning. This will launch model builder. I can choose from a variety of templates. So in this case, since I am predicting taxi fares, I'm going to choose the price prediction template which predicts a numerical value from my historical data set. So I'm going to choose that template. And the second step in the machine learning process is around connecting to your data and loading it. I can choose to load by file or SQL server. In this case, since I've downloaded the New York taxi dataset, which is a CSV file, I can choose file and I can navigate to that data set. Once the data gets loaded, I need to get a preview of what the data looks like. It's showing me the 10 rows of all these rows and about seven columns. And I can then choose the column that I want to predict or the label column. And in this case is the fair amount. And so fair amount becomes my label column. I can use these features and the data to train a model. And by simply saying start training the longer I train the model for the better model I'll get underneath the covers, model builder uses automated machine learning which explores various models with different settings and gives me the best model. So I don't have to worry about fine tuning the model for my specific case. So in this case, once the training completed I got a sort of a quality or accuracy of 94% and the best algorithm was LightGbmRegression. I can go to the evaluate screen and I can understand more in terms of what happened in the system. For example, while the best model was LightGbmRegression the tool also explored other models and it's showing me a summary of the top five models explored along with the accuracy and other characteristics that I can use. So I can use this information to further evaluate or to sort of gain trust in the system that it's not a black box where I got one result, but there were other results as well. If I'm happy with the evaluation criteria, I can then go to the code step which is the last step of your machine learning workflow which is around consuming the model. I can click add projects, which is going to add two projects to my solution, one project, which has the code for how the model was trained in ModelBuilder.CS and a class library which has the actual model that was trained. And I can include it in my application. So let's go ahead and add a reference to the class library project so I can consume it in my app. So I'm going to say, add reference to that class library and say, okay. And I can use this code in my Program.CS to consume the model in my app. I'm going to add a few using statements for the class library I included. And then what this code does over here. It creates a model context, and then it loads the model, creates a prediction engine which I can use to predict a value. And I can give some values around... What was my sort of trip distance? Equals a hundred. What was my trip time in seconds? Which is around thousand. So I can play around with all these values and then call the predict function to predict the fair amount. And I can write it to the console as well. So I can do result.score, which is going to be the prediction value for the fair amount. So I'm going to run this application, looks like a missing a semi-colon here. So that's why I was getting errors. So I'm going to relaunch it. And what's happening is the machine learning model is loading the model. And it's going to give me a score which is what was the fair amount for my process. So what we saw in this video was how to build a machine learning model for predicting taxi fares. In the next video, we will look at how to build a model for recommending movies.

Contents