ML.Net Tutorial 2: Building a Machine Learning Model for Classification

Building Machine Learning Model With ML.Net in Visual Studio
This is the Tutorial 2 ML.Net series of tutorials. These tutorials would help you understand and build machine learning models in .Net very easily if you are already a .Net developer.

If you are not a .Net Developer, you could check my tutorial in Machine Learning with TensorFlow using Python.

You could also learn Difference Between Machine Learning and Deep Learning. This tutorial follows from Tutorial 1  where you downloaded your dataset, setup the Visual studio solution and added the ML.Net package to the solution
Take the following steps to create your Machine Learning Model.

Understand the DataSet

Open the iris-data file and view the data. Note that each record is made up of five fields. The first four fields are the features while the last field is the class the iris belongs.
This means that based on the value of the features(SepalLength, SepalWidth, PetalLenght, PetalWidth) we could predict whether the iris belongs to one of the three classes(Iris-setosa, Iris-versicolor, Iris-virginica)
This scenario is a typical classification problem.

Step 1: Class Classes that map to the Dataset

In this step, you need to create the two classes that represents your dataset. The first class represents the features and the second class represents the class.
The codes for the two classes are given below. You will write this code in the Program.cs file.


//STEP 1: Define your data structure. That is clases to hold the data
public class IrisData
  public float SepalLength;
  public float SepalWidth;

  public float PetalLength;

  public float PetalWidth;

  public string Label;
Listing 1.0: IrisData class maps to the features of the dataset
The second class shown below just has one attribute that maps the class name (the last field in the dataset)


    //IrisPrediction is the result returned prediction by the model
    public class IrisPrediction
        public string PredictedLabels;
Listing 1.1: Class to hold the prediction

Step 2: Create a Pipeline and Load the Data

What is a Pipeline? A pipeline is a set of classes in ML.Net that allows you to load external data, transform it to format suitable for training and train the model.
Use the code below to load the Iris data set.


      var pipeline = new LearningPipeline();
      string dataPath = "iris-data.txt";
      pipeline.Add(new TextLoader(dataPath).CreateFrom<IrisData>(separator: ','));
Listing 1.3: Create a pipeline and load your data.

Step 3: Transform the Data

Data needs to be transformed from string data to numeric data since training can only be carried out on numeric data. So the column labels are assigned numbers. This is achieved by using a Dictionarizer object
Next the we need to use a ColumnConcatenator to merge all the features into one single string as shown in Listing 1.4.
     pipeline.Add(new Dictionarizer("Label"));
     pipeline.Add(new ColumnConcatenator("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"));
Listing 1.4: Transform the data

Step 4: Add a Learning Algorithm

Here we add a learning algorithm that can train a classification model. This algorithm is the StochasticDualCoordinateClassifier object.
Then we convert back the numeric label into the original text using PredictedLabelColumnOriginalValueConverter object.
     pipeline.Add(new StochasticDualCoordinateAscentClassifier());
     pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" });
Listing 1.5: Add a learning algorithm and covert back

Step 5: Train the Model

In this step, we simple call the Train() method of the pipeline object to train our model. This is achieved by a single line of code shown in Listing 1.6.


     var model = pipeline.Train<IrisData, IrisPrediction>();
Listing 1.6: Train the model

Step 6: Use the Mode to Make Prediction

Now we can now test the model by passing new set of features and then allow the model to predict the class it belongs.
The code is given in Listing 1.7, but you can change the values to see what result you get.


var prediction = model.Predict(new IrisData()
     SepalLength = 0.3f,
     SepalWidth = 0.6f,
     PetalLength = 1.2f,
     PetalWidth = 1.1f
Console.WriteLine($"Pridicted flower class is : {prediction.PredictedLabels}");
Listing 1.7: Making prediction with the trained model
The output of this is shown in Figure 1.0


Figure 1.0: Final  Output


Final Notes


If you have come this far then be sure you now know the basics of How to Build a Machine Learning Mode using ML.Net.
This is really interesting for .net developers as you don’t need to learn how to use other tools like TensorFlow in Jupyter notebook and the Python Language.
We continues with Tutorial 3.
Subscribe to the Kindson The Genius Video Channel so you get notified when new lessons are made.