DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Accelerating AI Inference With TensorRT
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Getting Started With GenAI on BigQuery: A Step-by-Step Guide

Trending

  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • Testing SingleStore's MCP Server
  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Machine Learning for .Net Developers Using Visual Studio

Machine Learning for .Net Developers Using Visual Studio

Se how to install an extension to Visual Studio and a Nuget package.

By 
Ajay Kumar Singh user avatar
Ajay Kumar Singh
·
May. 21, 19 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
15.3K Views

Join the DZone community and get the full member experience.

Join For Free

To do machine learning, I have been learning Python and R, and learning a new language sometimes takes time and effort (but of course Python is easy).

But I am very happy with the fact that I can also create machine learning applications using my .Net skills. The only extra effort I have to put forth is installing an extension to Visual Studio and a Nuget package. Let's see how to do this.

What we need:

Visual Studio 2017 15.6 or later

What we will build:

We will be creating a machine learning Hello World! app.

Installing Extension:

Download and install the Extension "ML.NET Model Builder."

Let's Start

Once we are done with the extension, let's create a Console App (.Net Core) named myMLApp.

Image title

The above is a simple console solution. Now we make use of the extension. When we right-click the project and click Add, We see an option for machine learning.

Image title

So let's add it. After adding, we get the below model builder.
Image titleChoose Sentiment Analysis (Binary classification) for Now. On the next step, we are asked to add data. The best thing is that it also has the option to add data from SQL Server.
Image titleBut we would 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). After 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 think that's the best part of it.
Image titleWe can see that the best accuracy for the data we uploaded is 82.35 percent. No, we would also like to evaluate. Below, we can see why the particular algorithm was chosen and we could also increase the time and look for more options. But we'll continue with this for now.
Image titleNow comes the best part where the model builder generates code for us. So we just move to the next tab, "Code," and click "Add Projects." This has added 2 new projects in my solution, as shown below.
Image titleFor now, we can run the myMLAppML.ConsoleApp to try the model. In the above screenshot, we also have the sample code to consume the model.

Let's consume the model:

1. From myMLApp, add reference 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. Change the Program.cs Code for myMLApp to the 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."
Image titleWe're done! I tried to make it simple to understand, but you can also look at the below pages 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
Machine learning

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Accelerating AI Inference With TensorRT
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Getting Started With GenAI on BigQuery: A Step-by-Step Guide

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!