Wednesday, June 13, 2018

Get started with ML.NET in 10 minutes

Get started with ML.NET in 10 minutes


  1. I
    nstall 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 myAppwhere 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.2.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.
    5.1,3.5,1.4,0.2,Iris-setosa
    4.9,3.0,1.4,0.2,Iris-setosa
    ...
    4.7,3.2,1.3,0.2,Iris-setosa

    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;

  6. using Microsoft.ML.Data;
    using Microsoft.ML.Runtime.Api;
    using Microsoft.ML.Trainers;
    using Microsoft.ML.Transforms;
    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
            {
                [Column("0")]
                public float SepalLength;

                [Column("1")]
                public float SepalWidth;

                [Column("2")]
                public float PetalLength;

                [Column("3")]
                public float PetalWidth;

                [Column("4")]
                [ColumnName("Label")]
                public string Label;
            }

            // IrisPrediction is the result returned from prediction operations
            public class IrisPrediction
            {
                [ColumnName("PredictedLabel")]
                public string PredictedLabels;
            }

            static void Main(string[] args)
            {
                // STEP 2: Create a pipeline and load your data
                var pipeline = new LearningPipeline();

                // If working in Visual Studio, make sure the 'Copy to Output Directory'
                // property of iris-data.txt is set to 'Copy always'
                string dataPath = "iris-data.txt";
                pipeline.Add(new TextLoader(dataPath).CreateFrom<IrisData>(separator: ','));

                // STEP 3: Transform your data
                // Assign numeric values to text in the "Label" column, because only
                // numbers can be processed during model training
                pipeline.Add(new Dictionarizer("Label"));

                // Puts all features into a vector
                pipeline.Add(new ColumnConcatenator("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"));

                // STEP 4: Add learner
                // Add a learning algorithm to the pipeline.
                // This is a classification scenario (What type of iris is this?)
                pipeline.Add(new StochasticDualCoordinateAscentClassifier());

                // Convert the Label back into original text (after converting to number in step 3)
                pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" });

                // STEP 5: Train your model based on the data set
                var model = pipeline.Train<IrisData, IrisPrediction>();

                // STEP 6: Use your model to make a prediction
                // You can change these numbers to test different predictions
                var prediction = model.Predict(new IrisData()
                {
                    SepalLength = 3.3f,
                    SepalWidth = 1.6f,
                    PetalLength = 0.2f,
                    PetalWidth = 5.1f,
                });

                Console.WriteLine($"Predicted flower type is: {prediction.PredictedLabels}");
            }
        }


  1. In your command prompt, run the following command:

No comments:

Post a Comment

ChatGPT and Intelligent Document Processing!!

 ChatGPT and Intelligent Document Processing!! Question: How chatgpt can helpful in IDP? Answer: As an AI language model, ChatGPT can be he...