To build Machine learning apps, I’ve been learning Python and R, And learning a new language sometimes takes time and effort (Of course Python is easy).

But I’m very happy with the fact that I can also create Machine Learning applications using my .Net Skills. Only extra effort I’ve to do is Install an extension to Visual Studio and a Nuget package. Let’s see step by step.

What we need :
Visual Studio 2017 15.6 or later

What we’ll build :
We’d be creating a hello world app of Machine Learning that’s Sentiment Analysis.

Installing Extension :
Download and Install the Extension “ML.NET Model Builder“.

Let’s Start :
Once we’re done with the extension. Lets go and create a Console App(.Net Core) named myMLApp.

So as above is a simple console solution. Now we make use of the extension. When we Right click the project and say Add We see an Option of Machine Learning.

Ok so lets add it. So after adding we get the below model builder.

We’d choose Sentiment Analysis (Binary classification) for Now. Now on the next step we’ll be asked to add data. The best thing is it also has option to add Data from SQL Server.

But we’d choose file and upload the file downloaded from here. The data looks like below.

SentimentSentimentText
1 ==RUDE== Dude, you are rude upload that carl picture back, or else.
1 == OK! == IM GOING TO VANDALIZE WILD ONES WIKI THEN!!!

Select “Sentiment” under Column to Predict (Label). Post uploading the file we also see the preview of data.

Now its time to train our model. So we go to the next tab “Train”. This step is simple you select the time duration you want to train your model for and the model builder will choose the best possible algorithm based on the accuracy, I thinks that’s the best part of it.

We can see that the best accuracy for the data we uploaded is 82.35%. No We’d also like to evaluate. In below we can see the why the particular algorithm was chosen, we could also increase the time and see for more options. But we continue with this for now.

Now comes the best part the model builder will also generate code for us. So we just move to next tab “Code” and there we just click “Add Projects”. This has added 2 new projects in My solution as below.

For Now we can run the myMLAppML.ConsoleApp to try the model. In the above screen we also have the sample code to consume the model. So Lets consume the model.
To consume the model would do the following.

1. From myMLApp we would add refernce of “myMLAppML.Model”.

2. Install nuget package Microsoft.ML to myMLApp.

3. In myMLAppML.Model for the file MLModel.zip set the property “Copy to Output” to “Always”.

4. Now we would change the Program.cs Code for myMLApp to below code

namespace myMLApp
{
class Program
{
static void Main(string[] args)
{
ConsumeModel();
}

public static void ConsumeModel()
{
// Load the model
MLContext mlContext = new MLContext();
ITransformer mlModel = mlContext.Model.Load("MLModel.zip", out var modelInputSchema);
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
// Use the code below to add input data
var input = new ModelInput();
Console.WriteLine("Type your sentiment :");
input.SentimentText = Console.ReadLine();
// Try model on sample data
ModelOutput result = predEngine.Predict(input);
Console.WriteLine("Result=" + result.Prediction);
Console.ReadKey();
}
}
}

Now we can run our project “myMLApp”.

Ok so here we are done. I have tried to make it simple to understand you could also have a look at below pages of micrsoft to learn more about it. Enjoy Machine Learning in .Net.

https://dotnet.microsoft.com/learn/machinelearning-ai/ml-dotnet-get-started-tutorial/intro

https://dotnet.microsoft.com/apps/machinelearning-ai/ml-dotnet

https://github.com/dotnet/machinelearning

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*