February 10, 2025

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

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.

  1. Understand the Dataset
  2. Step 1: Define the Data Structure
  3. Step 2: Create a Pipeline and Load the Data
  4. Step 3: Transform the Data
  5. Step 4: Add a Learning Algorithm
  6. Step 5: Train the Model
  7. Step 6: Use the Mode to Make Prediction
  8. Final Notes

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
        {   
            [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;
        }

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
        {
            [ColumnName("PredictedLabel")]
            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}");
        Console.Read();

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.

0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Creem siteuri
Creem siteuri
4 years ago

An impressive share! I have just forwarded this onto a colleague who has been doing
a little research on this. And he in fact ordered
me breakfast due to the fact that I discovered it for him…
lol. So allow me to reword this…. Thanks for the meal!!
But yeah, thanks for spending some time to talk about
this subject here on your internet site.

Paul Van Blom
Paul Van Blom
7 months ago

I invested with a Crypto broker without proper research to know what I was hoarding my hard-earned money into, only to find out it was a hoax and sham. I invested over $375,000 USD estimated to be 5.6 BTC. I was unable to make any withdrawals out of my initial deposits let alone the gains they claimed I had earned even after meeting the bogus fees and charges they were always requested. Fortunately, I got to know about the Lost Recovery Masters, a recovery cryptocurrency expert that is well known and has the best tech skills in Bitcoin recovery programmers through research and positive reviews l saw on Google. After a couple of hours I consulted with them through their website https:/ /lostrecoverymasters. com Within the next 24hours all my funds were recovered including my profits. Thank you Lost Recovery Masters.