Hello Devz,

Here is a simple tutorial of how to use ML.Net to make a prediction based on regression. ML.Net is an open source and cross-platform machine learning framework. A regression is a statistical method to find the relation between variables.

Let’s use a budget context. We have a list of expenses with a cost and a category. We would like to predict the category based on the cost of an expense.

After you created your new project in Visual Studio, be sure to install ML.Net from NuGet and set your project to be built with X64 (required for ML.Net).

We can start by creating our models. First, a definition of an Expense:

The Column attribute comes from ML.NET and will help the framework to understand the mapping between the model and the columns in the CSV file. This ExpenseData model will represent the data we have: a CSV file looking that we will call “expenses.csv”:

CostCategoryId
1.71
2.21
13002
73
93
1204
1.71
13002
7.53
2.21
13002
93
1304
1.71
31
2.21
13002
1104
1.71
2.21
7.53
1.71
13002
2.21
1224
73
13002
1.71
2.21
2.21
1.71
13002
7.53
1.71
1.71
13002
7.53
13002
1.71
2.21
1.71
31
1.71
13002
7.53

Where a category is defined like this:

And with:

We will create another CSV file called: “expenses_tests.csv”, containing this (for a later usage):

CostCategoryId
1.71
13002
2.21
93
7.53
1304
13002
1.71
31
2.21
7.53
93
13002
1.71
1.71
2.21
31
1204
13002

Which will be the data we will use to test our prediction model.

Now, we can create our CategoryPrediction class:

Where ColumnName is a mandatory attribute helping ML.Net to identify the target of the prediction.

Now, let’s see the whole code:

And the prediction seems correct:

ML.NET - prediction result

ML.NET – prediction result

Happy coding!  😉