Documente Academic
Documente Profesional
Documente Cultură
Microsoft is pouring all their effort into ML.NET right now. This is
going to be their go-to solution for all machine learning in NET going
forward.
And it’s super easy to use. Watch this, I’m going to build an app that can
predict taxi fares in New York.
The first thing I need is a data file with thousands of New York taxi
rides. The NYC Taxi & Limousine Commission provides yearly TLC Trip
Record Data files which have exactly what I need.
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 1/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 2/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 3/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
I’m using the awesome Rainbow CSV plugin for Visual Studio Code
which is highlighting my CSV data file with these nice colors.
The plugin can also run simple RBQL queries directly on the file:
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 4/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 5/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
The final column in the data file has the taxi fare I’m trying to predict.
• Number of passengers
• Trip time
• Trip distance
I’ll build a machine learning model in C# that will use these columns as
input, and use them to accurately predict the taxi fare for every trip.
NET Core is really cool. It’s the multi-platform version of the NET
framework and it runs flawlessly on Windows, OS/X, and Linux.
I’m using the 3.0 preview on my Mac right now and haven’t touched my
Windows 10 virtual machine in days.
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 6/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
Now I’m ready to add some classes. I’ll need one to hold a taxi trip, and
one to hold my model’s predictions.
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 7/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
1 /// <summary>
2 /// The TaxiTrip class represents a single taxi trip.
3 /// </summary>
4 public class TaxiTrip
5 {
6 [Column("0")] public string VendorId;
7 [Column("1")] public string RateCode;
8 [Column("2")] public float PassengerCount;
9 [Column("3")] public float TripTime;
10 [Column("4")] public float TripDistance;
11 [Column("5")] public string PaymentType;
12 [Column("6")] public float FareAmount;
13 }
14
The TaxiTrip class holds one single taxi trip. Note how each field is
adorned with a Column attribute that tell the CSV data loading code
which column to import data from.
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 8/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
1 /// <summary>
2 /// The program class.
3 /// </summary>
4 class Program
5 {
6 // file paths to data files
7 static readonly string trainingDataPath = Path.Combine(
8 static readonly string testDataPath = Path.Combine(Envi
9
10 /// <summary>
11 /// The main application entry point.
12 /// </summary>
13 /// <param name="args">The command line arguments.</par
14 static void Main(string[] args)
15 {
16 // create the machine learning context
17 var mlContext = new MLContext();
18
19 // set up the text loader
20 var textLoader = mlContext.Data.CreateTextLoader(
21 new TextLoader.Arguments()
22 {
23 Separators = new[] { ',' },
24 HasHeader = true,
25 Column = new[]
26 {
27 new TextLoader.Column("VendorId", DataK
28 new TextLoader.Column("RateCode", DataK
29 new TextLoader.Column("PassengerCount",
30 T tL d C l ("T i Ti " D t K
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 9/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
This code sets up a TextLoader to load the CSV data into memory. Note
that all column data types are what you’d expect, except RateCode.
This column holds a numeric value from 0 to 6, but I’m loading it as a
text field.
• 1 = standard
• 2 = JFK
• 3 = Newark
• 4 = Nassau
• 5 = negotiated
• 6 = group
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 10/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
With the TextLoader all set up, a single call to Read() is sufficient to
load the entire data file in memory.
Machine learning models in ML.NET are built with pipelines, which are
sequences of data-loading, transformation, and learning components.
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 11/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 12/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
With the pipeline fully assembled, I can train the model with a call to
Fit().
This code uses the TextLoader class to load another taxi trip data file
for testing. And with a single call to Transform(…) I can set up
predictions for every single trip in the file.
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 13/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
• Rms: this is the root mean square error or RMSE value. It’s the go-
to metric in the field of machine learning to evaluate models and
rate their accuracy. RMSE represents the length of a vector in n-
dimensional space, made up of the error in each individual
prediction.
• L2: this is the mean square prediction error, or MSE value. Note
that RMSE and MSE are related: RMSE is just the square root of
MSE.
I’m going to take a taxi trip for 3.75 miles and the trip will take me 19
minutes. I’ll be the only passenger and I’ll pay by credit card.
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 14/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
I know from the data file that this trip is supposed to cost $15.50. How
accurate will the model prediction be?
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 15/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 16/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
The output is a bit small, so here’s the app again running in a zsh shell:
I get an RMSE value of 2.06 and an L1 value of 0.42. This means that
my predictions are on average only 42 cents off.
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 17/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 18/19
29/03/2019 Use C# and ML.NET Machine Learning To Predict Taxi Fares In New York
https://medium.com/machinelearningadvantage/use-c-and-ml-net-machine-learning-to-predict-taxi-fares-in-new-york-519546f52591 19/19