The rise of the machines has been foretold for many a year on the big screen, in books, and in folklore. Movies such as Terminator, The Matrix, and The Fifth Element; books such as 2001: A Space Odyssey by Arthur C. Clarke come to mind.
Every day, machine learning becomes easier and easier, especially when Microsoft gives us nice tools to play with. In today’s article, you will learn about Machine Learning and how you can make use of ML.NET in your .NET apps for machine learning purposes.
Machine Learning
Machine learning is the scientific study of algorithms and models that computers use to perform tasks without using explicit instructions, relying solely instead on patterns and inference. Machine learning algorithms build models based on training data, to make decisions without being explicitly programmed to do so.
Machine learning methods include:
- Supervised machine learning algorithms: Apply what has been learned to new data by using labels to predict future events.
- Unsupervised machine learning algorithms: Used when the training information used is not classified nor not labeled.
- Semi-supervised machine learning algorithms: Use both labeled and unlabelled data for training.
- Reinforcement machine learning algorithms: Interact with their environment by producing actions.
ML.NET
ML.NET is a free software machine learning library for C#, VB.NET, and F#. ML.NET includes transforms for feature engineering such as n-gram creation. ML.NET also includes transforms for learners to handle binary classification, multi-class classification, and regression tasks. ML.NET also includes anomaly detection and recommendation systems and deep learning.
ML.NET Model Builder
ML.NET Builder is a UI tool from Microsoft that allows developers to train and build machine learning models in their applications. Model Builder supports AutoML. AutoML explores different machine learning algorithms automatically to assist in finding the correct algorithm that fits the current scenario.
Download ML.NET Model Builder from the Visual Studio Marketplace, as shown in Figure 1.
Figure 1: Download ML.NET Model Builder
After the download, run the Install (VSIX) file.
That’s it!
Now, how do we use it?
Well, create a new .NET Core App (for example, a Console Application). You can see this in Figure 2.
Figure 2: Console .NET Core App
After the project has loaded, right-click it in the Solution Explorer.
Select Add.
Select Machine Learning. This opens ML.NET Model Builder (see Figure 3).
Figure 3: Machine Learning Menu
Choose the Scenario. In this case, choose Price Prediction, as pictured in Figure 4.
Figure 4: Scenario
As you shall see, with all the scenarios, a file is needed. This file needs to be in either CSV or TSV values. Each file must also contain at least 100 records for training to be done properly. For the purposes of this article and this exercise, you can download a file named taxi-fare-train.csv.
It contains the following columns:
vendor_id,rate_code,passenger_count,trip_time_in_secs,trip_distance,payment_type,fare_amount
Choose the file that you have just downloaded, and choose the fare_amount as the column to predict (see Figure 5).
Figure 5: Data
Select the Train tab, and then select the Start training button, shown in Figure 6. The algorithm details will be shown as it progresses through the test.
Figure 6: Train
Select Evaluate to double check the outputs (see Figure 7).
Figure 7: Evaluate
Finally, click the Code tab, and then click Add Projects.
The Model gets added to the project, and code, as shown below, is generated. This code needs to be referenced from your project.
// Add ML.NET namespaces using Microsoft.ML; public void ConsumeModel() { // Load the model MLContext mlContext = new MLContext(); ITransformer mlModel = mlContext.Model.Load("MLModel.zip", out var modelInputSchema); var predEngine = mlContext.Model.CreatePredictionEngine <ModelInput, ModelOutput>(mlModel); // Use the code below to add input data var input = new ModelInput(); // Input // Try model on sample data ModelOutput result = predEngine.Predict(input); }
The final project can be seen in Solution Explorer, as shown in Figure 8.
Figure 8: Solution Explorer
Conclusion
Teaching machines becomes easier and easier, especially with the help of frameworks such as Microsoft’s ML.NET Framework. In a future article, I will delve a bit deeper into ML.NET, but, until then, cheers!