ML.NET is brand new, you can anticipate some thrills along the way.

ML.NET Tutorial - Get started in 10 minutes

  1. Install the .NET SDK

    To start building .NET apps you just need to download and install the .NET SDK (Software Development Kit).

  2. Create your app

    Open a new command prompt and run the following commands:

    dotnet new console -o myApp
    cd myApp

    The dotnet command creates a new application of type console for you. The -o parameter creates a directory named myApp where your app is stored, and populates it with the required files. The cd myApp command puts you into the newly created app directory.

  3. Install ML.NET package

    To use ML.NET, you need to install the Microsoft.ML package. In your command prompt, run the following command:

    dotnet add package Microsoft.ML --version 0.9.0
  4. Download the data set

    Your machine learning app will predict the type of iris flower (setosa, versicolor, or virginica) based on four features: petal length, petal width, sepal length, and sepal width

    Open the UCI Machine Learning Repository: Iris Data Set, copy and paste the data into a text editor (e.g. Notepad), and save it as iris-data.txt in the myApp directory.

    When you paste the data it will look like the following. Each row represents a different sample of an iris flower. From left to right, the columns represent: sepal length, sepal width, petal length, petal width, and type of iris flower.


    Using Visual Studio?

    If you're following along in Visual Studio, you'll need to configure iris-data.txt to be copied to the output directory.

    In Visual Studio, open the properties window for iris-data.txt and set 'Copy To Output Directory' to 'Copy always'.
  5. Write some code

    Open Program.cs in any text editor and replace all of the 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
            // input for prediction operations
            // - First 4 properties are inputs/features used to predict the label
            // - Label is what you are predicting, and is only set when training
            public class IrisData
                public float SepalLength;
                public float SepalWidth;
                public float PetalLength;
                public float PetalWidth;
                public string Label;
            // IrisPrediction is the result returned from prediction operations
            public class IrisPrediction
                public string PredictedLabels;
            static void Main(string[] args)
                // STEP 2: Create a ML.NET environment  
                var mlContext = new MLContext();
                // If working in Visual Studio, make sure the 'Copy to Output Directory'
                // property of 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: Transform your data and add a learner
                // Assign numeric values to text in the "Label" column, because only
                // numbers can be processed during model training.
                // Add a learning algorithm to the pipeline. e.g.(What type of iris is this?)
                // Convert the Label back into original text (after converting to 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"))
                // STEP 4: Train your model based on the data set  
                var model = pipeline.Fit(trainingDataView);
                // STEP 5: Use your model to make a prediction
                // You can change these numbers to test different predictions
                var 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}");
  6. Run your app

    In your command prompt, run the following command:

    dotnet run

    The last line of output is the predicted type of iris flower. You can change the values passed to the Predict function to see predictions based on different measurements.

    Congratulations, you've built your first machine learning model with ML.NET!

  7. Keep learning

    Now that you've got the basics, you can keep learning with our ML.NET tutorials.

    ML.NET tutorials