How to Train ML.NET Model using the API

How to Train ML.NET Model using the API

This article will demonstrate the last step of a machine learning pipeline. Today we will see how to train ML.NET Model using the provided API.

ML.NET provides several training algorithms to train for different Machine Learning needs. For each task, there is a component called trainer.

Training process is very important. Combined with the evaluation assessment, we will conclude this tutorial series on introduction to ML.NET and continue investigating these concepts on a deeper level.

Trainers allow us to make sense of the data. Gain a more meaningful and valuable insights on the data set we are providing.

In other words, they learn from the data. Machine Learning algorithms are able to find relationships, develop understanding and make decisions based on the input they are given.

This is why in the last post on how to transform data we said: “Garbage In, Garbage Out”. In fact, the quality and quantity of your training data is as important as the algorithm you are using. It can make or break a Machine Learning project.

Before we start with examples on how to train ML.NET model using the API, let’s first talk about Machine Learning algorithms.

Machine Learning

Why the need for Machine Learning? Especially if we can write a set of instructions to resolve any problem we have.

In mathematics and computer science, an algorithm is defined as a finite sequence of well-defined, computer implementable instructions, typically to solve a class of problems or to perform a computation – Wikipedia.

Why not simply use algorithms?

The problem with algorithms is that they are defined as a set of instructions. And that is fine for solving a problem that follows the exact rules an algorithm defines.

The problem arises when there are simply too many variances in the problem we are trying to solve.

Let’s look at an example.

If we write a fixed set of rules to recognize the following image of a cat

Image of a cat
Cat Image

Can the algorithm recognize this image as well?

Another Cat Image
Cat Image

Most definitely not. Although there is a cat on both images, you would need a new set of rules to recognize cat on the second image. That is because the fur has a different color, and the cat position is different. And don’t forget the background as well as other factors such as image perspective and quality.

Imagine writing a new set of rules to detect all the different combinations that cat can appear on an image. It is simply impossible and impractical.

Patterns

But there are some distinctive patterns. We humans know it is a cat. Because of a common set of features cat species have in common.

Maybe the whiskers, eyes or the shape of the ears, eyes, and mouth reminds us of a cat.

Well, there’s our answer. Machine Learning algorithms work with data so that they can learn what makes a cat – a cat. It tries to learn cat like features just as we humans do.

Because they are able to learn complex patterns from the data, there is no need to write rules. It can deduct its own “rules” from the given input.

As a result, Machine Learning algorithms build a model based on sample data, in order to make predictions or decisions without being explicitly programmed to do so.

Machine Learning Algorithms

There are three broad categories of machine learning algorithms.

Supervised Learning

Supervised Learning algorithm requires an example inputs (training data), and their desired outputs. Think of it as a Y=f(x). The objective of these types of algorithms is to learn a target function (f) that best maps input variable (x) to an output variable (y).

They are called supervised because for each training data point you are providing a desired output. The algorithm will try to make sense of the relationship and map that input as closely as possible to the provided output.

A good example would be Cat Vs Dog Classifier. We provide a dataset which contains labeled images of dogs and cats. Labelled dataset would mean that for each input image, there is a label associated with it.

Then a supervised learning algorithm will try to learn distinct cat and dog features, so that it will be able to differentiate between them.

Unsupervised Learning

Unsupervised Learning algorithm doesn’t need labels. The dataset only contains inputs. These types of algorithms are trying to find structure in the data, like grouping or clustering. They uncover commonalities or patterns if you will, from the data itself.

Some use cases for unsupervised learning include understanding of different customer groups also known as customer segmentation, clustering DNA patterns in genetics. Recommender System which involves grouping together similar users or similar behavior. Credit card companies uses this type of algorithms to detect credit card frauds by using Anomaly Detection as a strategy, and the list goes on.

Reinforcement Learning

Reinforcement Learning algorithms acquire information about stimuli, actions, and context that predict positive outcomes. It modifies behavior when a reward occurs, or if the outcome is better than what we were expecting.

Reinforcement Learning is like teaching your dog to do tricks. You provide the treats as a reward when the dog does what it is meant to.

It is a group of Machine Learning algorithms that enables an agent to learn in environment using feedback from its own actions and experiences. In other words, the goal is to make a sequence of decisions that would maximize the reward.

ML.NET Trainers

ML.NET offers wide variety of trainers. For each Machine Learning task ML.NET provides a component that executes the training algorithm.

Each trainer can be located in their respective types. For example, the Binary Classification trainers are located under the MLContext instance BinaryClassification property.

mlContext.BinaryClassification.Trainers

On this blog we will constantly use different components to train ML.NET model. At the moment the important thing to remember is that a trainer is added as the last step in the machine learning pipeline.

So far, we have gone through tutorials on how to load data, transform it and finally we are at the end. Pass it to an algorithm that takes in a data view to train ML.NET model. Once trained we can use this model to predict future values.

One more important thing to note is that each learning algorithm is different. It takes different parameters as inputs called hyperparameters. Don’t worry too much about it for now because I will demonstrate some very simple examples.

The important part is to know where to find the ML.NET trainers and how to append them to your machine learning pipeline. In future post we will discuss different algorithms, how they work and their respective hyperparameters.

Train ML.NET Model on Regression Problem

Our first example will be to solve a regression problem.

Regression is a statistical method which attempts to determine the strength of the relationship between one dependent variable (Y) and a series of other independent variables (X1, X2, …)

A good small regression problem that we will try to solve is a bike rental scenario.

Bike Rental Scenario

Imagine you are a bike rental shop owner, and you need to know how many bikes you need to have ready at the shop. You need this kind of application because at any given time some of the bikes need to be repaired. As a result, if it is a slow day, you may send more bikes for maintenance or repair. But, if the application predicts a busy day at the shop, then you might need more bikes ready than usual.

In this scenario the number of bikes in the shop is represented by the Y variable. This variable is dependent on other variables such as temperature, season, windspeed and so on.

It is expected that a busy day in the shop is when the weather is good, it is not a workday in the summer season. When it rains or when it is cold very few bikes are being rent out.

Before setting up the model, we need data.

The Dataset

The dataset we are going to use is called: Bike Sharing Data Set and can be download from the following link.

UCI Machine Learning Repository: Bike Sharing Dataset Data Set

The Dataset is accompanied with two files. Bike rentals per day and per hour. We will use the data generated daily.

It is a CSV file so it should be easy to load. The important thing to note is that we will not attempt to solve this problem completely. The idea is to demonstrate how a complete machine learning pipeline is built.

Because of that we will predict the number of bike rents per day on the following input variables:

  • Season (1: spring, 2: summer, 3: fall, 4: winter)
  • Holiday (1: it is a holiday, 0: it is not a holiday)
  • Weekday
  • Working Day
  • Weather Situation
    • Clear, Few clouds, Partly cloudy
    • Mist + Cloudy, Mist + Broken Clouds, Mist + Few Clouds, Mist
    • Light Snow, Light Rain + Thunderstorm + Scattered Clouds, Light Rain + Scattered Clouds
    • Heavy rain + Ice pallets + Thunderstorm + Mist, Snow + Fog
  • Temperature
  • Humidity
  • Windspeed

And we will try to predict the total number of bike rentals (Casual and Registered)

So, let’s get started.

Bike Rental Shop Code Walkthrough

Everything in ML.NET starts with creating an instance from MLContext class.

var mlContext = new MLContext();

Next, we need to load the data. If you are not sure how to load data in ML.NET you can check the following tutorial.

var data = mlContext.Data.LoadFromTextFile<InputModel>("day.csv", separatorChar: ',', hasHeader: true);

The input model is defined as:

public class InputModel
{
     [ColumnName("Season"), LoadColumn(2)]
     public float Season { get; set; }
     [ColumnName("Year"), LoadColumn(3)]
     public float Year { get; set; }
     [ColumnName("Month"), LoadColumn(4)]
     public float Month { get; set; }
     [ColumnName("Holiday"), LoadColumn(5)]        
     public float Holiday { get; set; }
     [ColumnName("WeekDay"), LoadColumn(6)]
     public float WeekDay { get; set; }
     [ColumnName("WorkDay"), LoadColumn(7)]
     public float WorkingDay { get; set; }
     [ColumnName("Weather"), LoadColumn(8)]
     public float WeatherSituation { get; set; }
     [ColumnName("Temperature"), LoadColumn(9)]
     public float Temperature { get; set; }
     [ColumnName("Humidity"), LoadColumn(11)]
     public float Humidity { get; set; }
     [ColumnName("WindSpeed"), LoadColumn(12)]
     public float WindSpeed { get; set; }
     [ColumnName("Count"), LoadColumn(15)]
     public float TotalBikeRentalCount { get; set; }
}

Now let’s build the pipeline

var pipeline = mlContext.Transforms.Concatenate("Features", new string[] { "Season","Year", "Month", "Holiday",  "WeekDay", "WorkDay", "Weather", "Temperature", "Humidity", "WindSpeed" })
.Append(mlContext.Regression.Trainers.FastTree("Count", "Features"));  

There is one transformation which concatenates the input values into a single vector with length of 10. If you are not sure what data transformation is, then refresh your knowledge on the following tutorial.

How to transform data in ML.NET

Because this is a regression problem, we will find the appropriate algorithm under Regression.Trainers.

ML.NET Linear Regression Trainer

The best suited trainer for this type of setup is the FastTree algorithm. All you have to know right now is that is part of the Linear Regression family of algorithms.

Also, you will notice that not all columns are included. This is because some of the features are positively correlated. For example, temp and atemp are highly positively correlated to each other. Statement like that means that they both are carrying the same information. As a result, I decided not to use atemp column.

Columns like total_count, casual, and registered are also highly positively correlated to each other. So, we are going to ignore casual and registered as well.

The last couple of paragraphs are just explaining why some of the columns are missing in my code. Right now, don’t worry about how I decided not to include them. We’ll get there. For now, I just wanted to offer a simple explanation and that’s it.

You may also ask why I chose FastTree algorithm. Well in my opinion this works best for this scenario. Please feel free to experiment with other trainers as well, and see what kind of results you get.

Remember this post is about ML.NET trainers and how to train ML.NET model. The important thing to remember here is that trainers are added as the last component to a machine learning pipeline. Also, the trainer you are going to choose depends on the machine learning task you are trying to solve.

That’s it.

We will have a dedicated tutorial on which data transformation we will apply and why. Data exploration also plays a huge role in the process as well. But we will visit these subjects in another post.

Next, we simply call the Fit method.

var model = pipeline.Fit(data);

And to get the predictions we execute the Transform method.

var predictions = model.Transform(data);

ML.NET Model Evaluation

Once you learn how to train ML.NET model, you also need to learn how to evaluate the results. Evaluation is a more elaborate process that what I will present here but let’s see a simple way to do just that.

var metrics = mlContext.Regression.Evaluate(predictions, "Count", "Score");
Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:#.##}");
Console.WriteLine($"RSquared Score:          {metrics.RSquared:0.##}");

These metrics show the error our ML.NET Model makes. The comparison is between what we expect to get and what the model outputs. As you can see, we are comparing the Count column from the dataset with the predicted values in the Score column.

This is not a proper evaluation. We will talk about it in a dedicated post, and we will also explain the metrics that were used.

This post is already too long, so let’s end it here.

Conclusion

This article demonstrates how to train ML.NET model using the API. The framework provides various trainers for different types of Machine Learning scenarios.

What is important to note is that the trainer is appended at the end of the pipeline. Also remember that you must choose the appropriate trainer for the specific machine learning task you are trying to solve.

We saw how to use FastTree algorithm to solve a Linear Regression problem. You could also try SDCA to solve the bike rental problem.

There are also unsupervised trainers that ML.NET offers, like KMeansTrainer.

In the family of Binary Classification Trainers, you can find FastTreeBinaryTrainer and LinearSvmTrainer.

There are many other training components that I have not mentioned. We will get to them eventually with different types of hands-on projects.

Since, now we know how to train ML.NET Model we can start working on more complex scenarios and dive deeper into ML.NET.

Previous Post: How to Transform Data in ML.NET

Next Post: How to Preprocess Data in ML.NET

The source code for this tutorial can be downloaded from the following link:

18 thoughts on “How to Train ML.NET Model using the API

  1. Reply
    Trending Hairstyles
    March 14, 2024 at 3:53 pm

    Usually I don’t read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been amazed me. Thanks, very nice article.

  2. Reply
    ubaTaeCJ
    March 14, 2024 at 4:11 pm

    1

  3. Reply
    Anonymous
    March 14, 2024 at 4:23 pm

    1

  4. Reply
    Hairstyles
    March 14, 2024 at 7:27 pm

    What抯 Going down i’m new to this, I stumbled upon this I’ve found It positively helpful and it has helped me out loads. I’m hoping to give a contribution & assist different customers like its helped me. Great job.

  5. Reply
    Hairstyles
    March 14, 2024 at 7:39 pm

    Hey There. I found your weblog the usage of msn. That is a really well written article. I will make sure to bookmark it and come back to learn more of your helpful information. Thanks for the post. I will definitely comeback.

  6. Reply
    Haircuts
    March 15, 2024 at 4:46 am

    I learned more interesting things on this weight-loss issue. A single issue is a good nutrition is especially vital when dieting. A huge reduction in bad foods, sugary food, fried foods, sweet foods, beef, and white colored flour products may be necessary. Having wastes harmful bacteria, and wastes may prevent goals for shedding fat. While certain drugs in the short term solve the condition, the awful side effects are usually not worth it, and they also never give more than a momentary solution. This is a known indisputable fact that 95 of fad diets fail. Many thanks sharing your thinking on this web site.

  7. Reply
    KAYSWELL
    March 15, 2024 at 8:30 am

    It’s perfect time to make a few plans for the longer term and it’s time to be happy. I have learn this put up and if I may just I want to counsel you some interesting issues or suggestions. Perhaps you could write subsequent articles regarding this article. I desire to learn more things approximately it!

  8. Reply
    Beauty Fashion
    March 15, 2024 at 1:40 pm

    Thanks for the post. My partner and i have always seen that almost all people are eager to lose weight since they wish to show up slim plus attractive. Nevertheless, they do not constantly realize that there are additional benefits just for losing weight additionally. Doctors declare that overweight people have problems with a variety of ailments that can be directly attributed to the excess weight. The great thing is that people who definitely are overweight plus suffering from various diseases are able to reduce the severity of their illnesses by losing weight. It is easy to see a gradual but noted improvement with health while even a minor amount of weight loss is obtained.

  9. Reply
    Haircuts
    March 15, 2024 at 9:03 pm

    Some tips i have seen in terms of computer memory is that often there are features such as SDRAM, DDR and so forth, that must go with the technical specs of the motherboard. If the pc’s motherboard is kind of current and there are no computer OS issues, updating the ram literally normally takes under 1 hour. It’s one of the easiest laptop upgrade treatments one can imagine. Thanks for discussing your ideas.

  10. Reply
    Haircuts
    March 16, 2024 at 1:48 am

    I抦 impressed, I need to say. Actually not often do I encounter a weblog that抯 both educative and entertaining, and let me inform you, you’ve hit the nail on the head. Your concept is outstanding; the difficulty is one thing that not sufficient individuals are speaking intelligently about. I am very happy that I stumbled throughout this in my seek for something regarding this.

  11. Reply
    KAYSWELL
    March 16, 2024 at 2:46 am

    Thanks for your write-up. I would love to opinion that the first thing you will need to accomplish is determine if you really need credit improvement. To do that you have got to get your hands on a copy of your credit rating. That should never be difficult, since government mandates that you are allowed to have one free of charge copy of your real credit report each year. You just have to check with the right men and women. You can either look into the website for your Federal Trade Commission or even contact one of the main credit agencies specifically.

  12. Reply
    Haircuts
    March 16, 2024 at 12:29 pm

    Valuable information. Lucky me I found your site by accident, and I am shocked why this accident didn’t happened earlier! I bookmarked it.

  13. Reply
    Hottest Hairstyles
    March 16, 2024 at 3:26 pm

    Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but instead of that, this is fantastic blog. A great read. I’ll certainly be back.

  14. Reply
    Haircuts
    March 17, 2024 at 9:44 am

    Hi there! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My website discusses a lot of the same topics as yours and I think we could greatly benefit from each other. If you are interested feel free to send me an e-mail. I look forward to hearing from you! Great blog by the way!

  15. Reply
    Trending Hairstyles
    March 17, 2024 at 6:49 pm

    Magnificent goods from you, man. I have take into accout your stuff previous to and you are simply too magnificent. I actually like what you have got here, certainly like what you are stating and the way in which you assert it. You are making it entertaining and you still take care of to stay it wise. I can not wait to read much more from you. That is really a great website.

  16. Reply
    KAYSWELL
    March 18, 2024 at 1:56 am

    We would also like to convey that most individuals that find themselves without health insurance are normally students, self-employed and those that are not working. More than half with the uninsured are under the age of 35. They do not come to feel they are in need of health insurance as they are young along with healthy. The income is often spent on property, food, plus entertainment. Most people that do go to work either complete or part time are not provided insurance via their work so they move without owing to the rising cost of health insurance in america. Thanks for the strategies you discuss through this blog.

  17. Reply
    Haircuts
    March 18, 2024 at 5:24 am

    I have learned several important things through your post. I’d also like to say that there can be situation where you will get a loan and do not need a cosigner such as a Federal Student Aid Loan. In case you are getting financing through a regular financier then you need to be made ready to have a co-signer ready to enable you to. The lenders will base their own decision over a few issues but the biggest will be your credit ratings. There are some loan merchants that will likewise look at your job history and come to a decision based on that but in most cases it will be based on on your report.

  18. Reply
    Hottest Hairstyles
    March 18, 2024 at 9:46 pm

    I cherished as much as you will obtain performed right here. The cartoon is attractive, your authored material stylish. nevertheless, you command get got an nervousness over that you would like be delivering the following. in poor health indubitably come further before again as exactly the same nearly very incessantly within case you shield this hike.

Leave a Reply

Your email address will not be published. Required fields are marked *