ML with .Net

Get started with Machine learning with .NET

Thidas Senavirathna
4 min readMar 6, 2025
Photo by Pietro Jeng on Unsplash

What is machine learning ?

Machine learning is where the system able to learn and predict or make decision based on data. It’s a branch of Artificial Intelligence.

Type of machine learning ?

  • Supervised Learning — The Model train on Labeled data. for example model predict spam or not a spam by using labeled data which he trained on. Classification ,Regression are common types you see.
  • Unsupervised Learning — The Model train on Unlabeled data. for example A common example is customer segmentation, where the model identifies groups of customers with similar behaviors or characteristics. Clustering is common type you see.
  • Reinforcement Learning — The Model train by interacting with an environment and receiving feedback in the form of rewards or penalties. for example robot training or playing game.

Let’s Build a Classification Model using .NET

First step is to create a console .net application.

Then,

right click on solution panel and add machine learning model as showing on the above image.

Then select the scinario as showing below,

Now we have to select the enviorment,

For this scenario you won’t get Azure and GPU but for other you will able to use. click on next step,

You can choose which source you are going to feed the model either can be a SQL server or File. I’ll select File for now, and make sure to select column which is model going to predict other will automatically will be the features because it going to help to predict.

Now time to train the model you can set training time. I’ll give 120 seconds,

Once you finished trainig you can evaluate the model and consume.

Now your model is created you can use it in program.cs file

// See https://aka.ms/new-console-template for more information
using ML_Net;

Console.WriteLine("Hello, World!");

//Load sample data
var sampleData = new MLModelTest.ModelInput()
{
Col0 = @"Crust is not good.",
};

//Load model and predict output
var result = MLModelTest.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}");

Well Done you made your first model successfully!

Conclusion

Machine learning with .NET provides a robust and accessible framework for creating intelligent applications that can learn from data and make predictions or decisions. By leveraging the capabilities of ML.NET, developers can easily integrate machine learning into their .NET projects without requiring extensive expertise in data science.

In this guide, we explored

  • What is Machine Learning?: A brief overview of the concept and its types.
  • Types of Machine Learning: Supervised, unsupervised, and reinforcement learning, with practical examples.
  • Building a Classification Model using ML.NET: Step-by-step instructions to create and train a model using .NET.

With ML.NET, the process of developing machine learning models becomes straightforward. It supports various scenarios such as classification, regression, and clustering, with the ability to consume the models directly in your .NET applications.

Congratulations on successfully building your first machine learning model with .NET! This is just the beginning — the possibilities for applying ML to real-world problems are endless. Keep exploring and building!

Sign up to discover human stories that deepen your understanding of the world.

--

--

Thidas Senavirathna
Thidas Senavirathna

Written by Thidas Senavirathna

Software Engineer Intern at Seylan Bank PLC | Oracle AI Certified Professional | Tech world enthusiast

No responses yet

Write a response