Train your data model using Azure AI model and predict results using C#

 

Hi friends, would like to talk today about the immense capabilities of Azure AI models, over the years how has it evolved and how could you leverage the same with C#, without writing much of a code (and of course very little understanding of AI).

Before we begin, let us consider some basics:

What is meant by REGRESSION in AI?

Regression is a machine learning algorithm technique that uses input features to predict continuous numerical outputs. It's a type of supervised learning, meaning it's trained on a labeled dataset with known output values.

Let me give you an example:

Suppose I have data about your company, as to how has it been performing w.r.t. the profit it has been making over years. 

Here is the data:


 When plotted it gives a scatter like this:


From here we can trace out a line that can cover the most of the points, and based on that we can easily find out the profit value, in another 10 years down the line.


Such a plot is typically marked with the equation: y=mx+c, where y is the outcome, x is the input data, m is the gradient and C is the initial value. 

This is a typical concept of regression, which we would cover in most of our model training algos, by 'labelling' data, and feeding datasets into models, over and over again, and thereby making the program understand the trend and 'predict' the future.

 A slightly different algorithm is what we know as 'Classification', whereby depending on the incoming data, we can class or 'brand' an outcome:


Here is a typical example of training your model to predict if an incoming mail is 'Spam' or 'HAM' message, by training the model with the mail attributes types: sender, subject, embdeded URL, photos, etc. All this could be done by machine learning techniques like Deep forest and Random forest algorithms.

Now guess what: all these is done by Azure model studio, without you to need a single line of code and predicting the models precisely.

And yes: it is free. Test your data, locally -- absolutely free, before you promote it to higher levels of testing and deployments.

So here you go:

I downloaded the following data file from imdb site: https://www.kaggle.com/datasets, that gives me viewers' reactions to various movies and rating (1 or 0, with 1 meaning a postive review, 0 meaning a bad review). It contains the reactions from the input data. We would be using the same data in our Azure model training and would predict an input data.

We are assuming for now that we are going to use Visual Studio 2022, although we can use Visual Studio Code as well.

Step 1:

In your Visual Studio installer, make sure that you have already installed the following component:



In your Visual Studio 2022, begin with creating a new project area, and manage nugget packages:



Search for the above package and install the same. 

Once done, in your project area, right click and you would see the following extension appearing:



Step-2

Choose the above and create model file with a suitable name. I created the same:


Uh, oh -- don't worry about the name (it was just a exampleπŸ˜…πŸ˜…)

The following prompt opens: 



Where it shows a list of templates which we can use the model for.
Go to the bottom and select the highlighted model -- this one is for predicting sentiments:

Click on 'Local' button to continue. The following screen comes:

Click on Next to continue.In the next step select the file which you previously downloaded (alternately you can choose database as well, as the option suggests):


This will load with a first few lines of records and allows you to select the input and output columns:

The above example shows, we have selected Column-0 as the column to be predicted, and column 1 as the  input column (it contains the sentiment inputs).
Click on 'Next step' to continue.
Click on 'Train' to start the training:
Click on 'Advanced training options' and you could see the range of different predictive models you can leverage here:

We are selecting Macro-accuracy: which means as the result is closer to 1, the more is the accuracy.
Depending on your data, the program would go through several errands of training through the training models (known as Epochs) and would show you the below prompt in the output window:

And finally it will be done, indicating that the training is complete:
As a 'Next step', you could evluate the data:
I have given a test data to see how is the program predictive accuracy. The results are absolutely correct, showing 1 as 99% probability with 0 as 1%..
The window also shows the model it has used and Macro-accuracy score it has got. 

Step-3

How to leverage the above through code:


You can create a console application and include the following files to begin with:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using MLModelSalary_ConsoleApp_PredictMovieReview;


Where MLModelSalary_ConsoleApp contains my trained model reference:


You can make the user to input a string, as the movie name and then accept your review of the movie:

Console.WriteLine("Which movie would you like to review?");
string movieName = Console.ReadLine();
Console.WriteLine("How did you find the movie?");
string review = Console.ReadLine();

Now you can tuck in the review in the model, in a variable called sampleData:
MLModelSalary.ModelInput sampleData = new MLModelSalary.ModelInput()
{
    Col0 = review
};

And then we are predicting the outcome from the above data, by calling our predicitve algo:
var sortedScoresWithLabel = MLModelSalary.PredictAllLabels(sampleData);
Andf then analysing the result:
 foreach (var score in sortedScoresWithLabel)
 {
     Console.WriteLine($"{score.Key,-40}{score.Value,-20}");
 }
With each outcome being scouped out of a key-value pair relatrionship:
 
Let us run the code now. Note the inputs from me:

And here comes the analysis of my sentiment:


Indicating that it's a pretty positive input from me, with a high score of 0.998 (nearer to 1, more is the accuracy).

Whew!!! So much for tdoay -- hope you liked the blog: you can also train your own data and leverage the result in your own code, and it's absolutely free to test.
That's all for now -- will come back with another cool hack on Azure AI and machine learning, soon. Much love and namaste, like always πŸ’“πŸ’“πŸ’“




Comments

Popular posts from this blog

X++ : mistakes which developers commit the most

Make your menu items visible on main menu, conditionally,, using this cool feature of D365FO

Speed up your execution performance by using SysGlobalCaches