Build your first ML-Model with ML.NET Model Builder
Published Apr 22 2024 12:00 AM 11.3K Views
Copper Contributor

I am Hamna Khalil, a Beta Microsoft Learn Student Ambassador from Pakistan. Presently, I am in my third year of pursuing a bachelor's degree in Software Engineering at Fatima Jinnah Women University.


Integrating machine learning functionalities into your .NET applications can be both exciting and daunting. However, with the advent of tools like ML.NET Model Builder and Visual Studio's development environment, the process is significantly streamlined.


In this step-by-step guide, I'll walk you through the process of building your first machine learning model using ML.NET Model Builder within Visual Studio. From setting up your development environment to training and evaluating your model's performance, each step is meticulously outlined to ensure a smooth and rewarding experience.


Step 1: Download and install






Step 2: Create .NET console app

To create a .NET console app in Visual Studio, follow these steps:

  • Open Visual Studio 2022 and select Create a new project.


  • Select C# Console App project template.


  • Change the project name to myMLApp. Ensure that Place solution and project in the same directory is unchecked.


  • Select the Next button.
  • Select .NET 8.0 (Long Term support) as the Framework.


  • Select the Create button. 


Step 3: Add machine learning model

To add a machine learning model to your .NET console app in Visual Studio, follow these steps:

  • Right-click on the myMLApp project in Solution Explorer
  • Select Add > Machine Learning Model.


  • In the Add New Item dialog, ensure that Machine Learning Model (ML.NET) is selected.
  • Change the Name field to SentimentModel.mbconfig and select the Add button.



Step 4: Choose machine learning scenario

To build your sentiment analysis model, you first need to select your machine learning scenario. 


  • In the Model Builder Scenario screen, choose the Data classification scenario.



Step 5: Select training environment

After selecting the Data classification scenario, choose your training environment.

  • Select the Local (CPU).



Step 6: Prepare data

  • Download the Sentiment Labelled Sentences dataset from the UCI Machine Learning Repository.
  • Unzip sentiment labelled and save the yelp_labelled.txt file to the myMLApp directory. 


  • Select File as the input data source type. 
  • Browse for the yelp_labelled.txt file. Once selected, a preview of your data will appear in the Data Preview section. 
  • Under Column to predict (Label), select "col1". 



Step 7: Train the model

To train your model using Model Builder with the yelp_labelled.txt dataset, follow these steps:

  • Adjust Time to train to 60 seconds (you may increase this value if no models are found after training).
  • Select Start training to start the training process. Once training is done, you’ll see a summary of the training results.



Step 8: Evaluate model performance

The Evaluate step shows the best-performing algorithm and the best-accuracy. 

  • In the Try your model section, the textbox is pre-filled with the first line of data from your dataset. You can modify this input to test different sentiment predictions. 
  • After modifying the input, select the Predict button to see the model's prediction.



Step 9: Consume the trained model

In the Consume step in Model Builder, a code snippet is provided that generates sample input for the model. Additionally, Model Builder offers two Project templates (a console app and a web API), both designed to consume the trained model.


  • To consume your trained model, replace the Program.cs code with the following code:


using MyMLApp;
// Add input data
var sampleData = new SentimentModel.ModelInput()
    Col0 = "This restaurant was wonderful."

// Load model and predict output of sample data
var result = SentimentModel.Predict(sampleData);

// If Prediction is 1, sentiment is "Positive"; otherwise, sentiment is "Negative"
var sentiment = result.PredictedLabel == 1 ? "Positive" : "Negative";
Console.WriteLine($"Text: {sampleData.Col0}\nSentiment: {sentiment}");





Step 10: Run and debug

  • Navigate to the Debug menu and select Start Without Debugging or use the shortcut Ctrl + F5.
  • After running, you should see the following output, predicting whether the input statement is positive or negative.



Congratulations, you have successfully built and executed your first machine learning model with ML.NET Model Builder in Visual Studio!

From setting up your development environment to training and evaluating your model's performance, you've acquired the skills to leverage ML.NET's capabilities for crafting predictive models efficiently. Armed with the power of ML.NET, you're now equipped to seamlessly integrate advanced machine learning functionalities into your .NET applications.


Additional Resources



ML for Beginners 

Global AI Community

Microsoft AI Discord Community

Version history
Last update:
‎Apr 25 2024 08:04 AM
Updated by: