Machine Learning using ML.NET - SkillBakery Studios


Post Top Ad

Post Top Ad

Friday, May 8, 2020

Machine Learning using ML.NET

Machine Learning using ML.NET

Machine Learning

Machine learning is basically about making machines learn like humans. Humans are known to be the most intelligent creatures and machine intelligence i.e. artificial intelligence is intelligence demonstrated by machines.
For that, we have to feed machines lots of data in terms of speech, natural language, images and patterns to make a machine learn by themselves.
A machine can learn in thousands of dimensions much more precisely than humans. To make it easy for all the .NET developers Microsoft introduces a preview of ML.Net which we call Machine Learning.Net. This framework is a cross-platform open-source machine learning framework and it makes .Net developers to easily develop their machine learning applications.
We can use C# and F# to develop ML.Net applications. It can be run on Windows, Mac OS, and Linux operating systems.

In this blog, you will see and learn about product review based on sentiment analysis
Before you start you need the following as prerequisites

What is Sentiment Analysis?
Sentiment Analysis is the expression of emotions, opinions, and attitude of humans towards any subject (for e.g. products, person, places) It's a subjective expression and these expressions can be classified as positive, negative and neutral.

Solution for a product review using ML.NET you have to follow these steps

  • Creating Application
  • Build ML Model
  • Choosing your scenario
  • Adding Dataset
  • Train the Model
  • Evaluate
  • Generate Code
  • Consume

Creating your Application you have to go through the following steps

Open Visual Studio and select Create New Project

Choose C# as Console Application (Console app .Net Core) as a template.

Rename project name to myMLApp

Make sure to keep solution and project file in the same directory

Create the project.

Building ML Model:

Model builder is a simple User Interface to build, train and customize machine learning models in .Net Applications

Select myMLApp in Solution Explorer and right-click to ADD > Machine Learning.

Image Reference:

This will open the ML.Net Model Builder in a new window to guide you through building machine learning models for different scenarios.

Choosing Your Scenario

To generate the model we have to choose any scenario. Here, I am selecting sentiment analysis for customers review

Image reference:

Adding Data to your Model

In Model Builder you can add data from any of your local data files with text or you can connect to your SQL server

To add data you have to first select FILE from the drop-down list.

Select Sentiment from the Column to Predict (Label).

The label is the column which we are predicting and in this case, it is the first column of the dataset which is Sentiment.

The columns which are used to help predict the label are called Features.

Select columns as Input Column (Features) which are as Sentiment Text or customer review.

Image Reference:

Next Step are to Train the Model

Training Our Model

Now you have to train your Model with the dataset.
Now you have to select a time to train your model the larger the data the longer the training time.
Select Start training to start the training process.

Image Reference:

After this, we will move on to evaluate our Model

Evaluate the Model

After training, the model builder will select the best model for your scenario, you can evaluate and check how many models have been explored and you can also try different models in can check with the best performing algorithm and many evaluation matrices for all the top models.

Image Reference:

Next steps are now to generate the code

Generate Code

Model Builder automatically adds ML Model and projects for training and consuming. In the solution explorer, we can see the code files generated by ML Builder.

Image Reference:

myMLAppML.Model is a .NET Standard class library that contains ModelInput.cs ModelOutput.cs for input/output classes for model training and consumption.
ConsumeModel.cs is a class that contains method for model consumption. (trained serialized ML model).

Consume your Model

Now Model Builder will generate a trained model and code for you to use in your .Net applications
Only you have to replace this program.cs code in your myMLApp

Using System;
using MyMLAppML.Model;

namespace myMLApp
class Program
static void Main(string[] args)
// Add input data
var input = new ModelInput();
input.SentimentText = "That is rude.";

// Load model and predict output of sample data
ModelOutput result = ConsumeModel.Predict(input);
Console.WriteLine($"Text: {input.SentimentText}\nIs Toxic: {result.Prediction}");

Run myMLApp. You should see the following output, predicting whether the input statement is toxic (true) or non-toxic (false).

Image Reference:

For other such scenarios of building your Machine Learning Model with ML.NET kindly visit

No comments:

Post a Comment

Post Top Ad