Over a million developers have joined DZone.

First Look of ML.NET: Microsoft Machine Learning Framework for .Net

DZone 's Guide to

First Look of ML.NET: Microsoft Machine Learning Framework for .Net

Read this article in order to take a look at a NuGet package for Machine Learning. This tutorial teaches how to add the NuGet package in your .Net applications.

· AI Zone ·
Free Resource


Finally a NuGet Package for Machine Learning

I always wanted to have a NuGet package that could be plugged with a .Net application by which we can create Machine learning applications.

Microsoft has announced the Open source and Cross-platform Machine learning framework ML.NET.

ML.NET First Version

ML.NET is just a baby, but it has already shown the capability of becoming a giant.

With its first version, we can perform Machine Learning tasks like classification, regression, etc. Have a look here for some basic information for these ML algorithms.

Along with some basic algorithms, we can train the model and predict using models, along with other basic Machine Learning tasks.

ML.NET can be extended to work with ML libraries like TensorFlow, Accord.NET, CNTK, etc.

A Big Picture


As you can see above, the framework can be extended to work with third-party libraries and it has some awesome libraries as well. Training and consumption both are present for many popular ML tasks like regression and classification.

Apart from this, ML.NET also supports core data types, extensible pipelines, high-performance math, data structures for heterogeneous data, tooling support, and more.

Install ML.NET NuGet Package

Let us see how to add this NuGet package in your .Net applications.

Important notes before starting:

First, make sure that you have installed .NET Core 2.0 or later. ML.NET also works on the .NET Framework. Note that ML.NET currently must run in a 64-bit process.

Once you have all of these installed, open your Visual Studio 2017 -> Create New Project -> Select Core Web application:


Select empty for now:


Once the application is created, let us add required Microsoft.ML NuGet package.

Search with “Microsoft.ML” in NuGet Package Manager and click on Install:


Once the package is installed, let us add some sample code.

Training the Model

We will first see the steps to train our model.

Creating Pipeline

We will create a LearningPipeline which will encapsulate the data loading, data processing/featurization, and learning algorithm:

1 varpipeline = newLearningPipeline();

Loading Data

Imagine we have a big file with all the data we require and we want to apply ML algorithms to that data. Let us get the data from that big file.

We get the path as below:

1 2 3 varSentimentDataPath = "pathToFile"; stringdataPath = GetDataPath(SentimentDataPath);

Once we have the path, use TextLoader to load the data from training file:

pipeline.Add(new TextLoader<SentimentData>(dataPath, useHeader: true, separator: “tab“));

Data is loaded now.

Create the Features

Next step is to convert the data columns to the feature. For that, TextFeaturizer is used:

pipeline.Add(new TextFeaturizer(“Features”, “SentimentText”));

We have completed all preprocessing steps.

Apply Algorithm

Next step is to apply different algorithms.

We will apply FastTreeBinaryClassifier here:

1 pipeline.Add(newFastTreeBinaryClassifier() { NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2 });

FastTreeBinaryClassifier is a decision tree learner we will use in this pipeline.

Convert Labels Back to the Original Value

Next step is to convert the predicted labels back to the original value. We will use PredictedLabelColumnOriginalValueConverter for this:

1 pipeline.Add(newPredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel"});

Train the Model

We have added everything required to our pipeline. The next, and important, step is to train your pipeline, which basically loads the data and trains the featurizer and learner:

var model = pipeline.Train<SentimentData, SentimentPrediction>();

The training phase is completed. The next step is to predict the data using this training model.

Predict From the Trained Model

Below is the code for prediction:

1 2 3 4 5 6 7 8 SentimentData data = newSentimentData{SentimentText = "Today is a great day!"};SentimentPrediction prediction = model.Predict(data);Console.WriteLine("prediction: "+ prediction.Sentiment)

Note – Code reference and whole code is here: https://github.com/dotnet/machinelearning/blob/master/test/Microsoft.ML.Tests/Scenarios/SentimentPredictionTests.cs

Get more details from here:https://www.microsoft.com/net/learn/apps/machine-learning-and-ai/ml-dotnet

That is it. You can predict the sentiment from the sentence using above code.

As I said, ML.NET is not mature yet but it is an awesome framework to keep a watch on.

Hope it helps.

machine learning ,.net ,artificial intelligence ,ai ,ML.NET

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}