Home .NET A guide to ML.NET – the first application in 10 minutes

A guide to ML.NET – the first application in 10 minutes

by admin

Last year we introduced ML.NET, a cross-platform and open-source machine learning system for .NET developers.In that time it has evolved a lot and gone through many versions.Today we’re sharing a guide on how to create your first ml.net application in 10 minutes.
A guide to ML.NET - the first application in 10 minutes
* This guide is in English
** Below is a Windows tutorial. But exactly the same thing can be done on MacOS / Linux

Install .NET SDK

To start creating .NET applications, you just need to download and install the .NET SDK (Software Development Kit).
A guide to ML.NET - the first application in 10 minutes

Create your own application

Open a command prompt and run the following commands :

dotnetnewconsole-omyAppcd myApp

Command dotnet will create for you new application of the type console Parameter -o creates directories with the name myApp where your application is stored and fills it with the necessary files. The command cd myAppwill return you to the created application directory.

Install the ML.NET package

To use ML.NET, you must install the Microsoft.ML package. At the command line, run the following command :

dotnet add package Microsoft.ML --version 0.9.0

Download DB

Our demonstrative machine learning application will predict the type of iris flower (setosa, versicolor, or virginica) based on four characteristics: petal length, petal width, tepal length, and tepal width.
Open the machine learning repository UCI : Iris dataset, copy and paste the data into a text editor (like Notepad) and save it as iris-data.txt in the directory myApp
When you add the data, it will look like this: each row represents a different iris flower pattern. From left to right, the columns represent: tepal length, tepal width, petal length, petal width, and iris flower type.

5.1, 3.5, 1.4, 0.2, Iris-setosa4.9, 3.0, 1.4, 0.2, Iris-setosa4.7, 3.2, 1.3, 0.2, Iris-setosa...

Do you use Visual Studio?

If you are using Visual Studio, you need to configure iris-data.txt to copy to the output directory.
A guide to ML.NET - the first application in 10 minutes

Let’s dig in a little.

Open Program.cs in any text editor and replace all code with the following :

using Microsoft.ML;using Microsoft.ML.Data;using System;namespace myApp{class Program{// Step 1: Define your data structures// IrisData is used to provide training data and// as an introduction to predictive operations.// - The first 4 properties are the input data/functions used to predict the label// - Label is what you predict and is only set during trainingpublic class IrisData{[LoadColumn(0)]public float SepalLength;[LoadColumn(1)]public float SepalWidth;[LoadColumn(2)]public float PetalLength;[LoadColumn(3)]public float PetalWidth;[LoadColumn(4)]public string Label;}// IrisPrediction is the result returned from the prediction operationspublic class IrisPrediction{[ColumnName("PredictedLabel")public string PredictedLabels;}static void Main(string[] args){// Step 2: Create ML.NET environmentvar mlContext = new MLContext();// If you work in Visual Studio, make sure that the 'Copy to Output Directory' parameter// iris-data.txt is set to 'Copy always'.var reader = mlContext.Data.CreateTextReader<IrisData> (separatorChar: ', ', hasHeader: true);IDataView trainingDataView = reader.Read("iris-data.txt");// Step 3: Convert your data and add a learner// Assign numeric values to the text in the "label" column, because only// numbers can be processed during model training.// Add a learning algorithm to the pipeline. For example (What type of iris is this?)// Convert the label back to the original text (after converting it to a number in step 3)var pipeline = mlContext.Transforms.Conversion.MapValueToKey("Label").Append(mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")).Append(mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumn: "Label", featureColumn: "Features")).Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));// Step 4: Train the model on this datasetvar model = pipeline.Fit(trainingDataView);// Step 5: use the model to predict// You can change these numbers to test different predictionsvar prediction = model.CreatePredictionEngine<IrisData, IrisPrediction> (mlContext).Predict(new IrisData(){SepalLength = 3.3f, SepalWidth = 1.6f, PetalLength = 0.2f, PetalWidth = 5.1f, });Console.WriteLine($"Predicted flower type is: {prediction.PredictedLabels}");}}}

Run your application

At the command line, run the following command :

dotnet run

The last line of output is the predicted type of the iris flower. You can change the values passed to the function Predict to see predictions based on different measurements.
Congratulations, you created your first machine learning model with ML.NET!

Keep up the good work

Now that you’ve got the basics, you can continue learning with our ML.NET tutorials.
A guide to ML.NET - the first application in 10 minutes

You may also like