Machine Learning for .NET Developers: How to build your own AI using ML.NET

Machine Learning for .NET Developers: How to build your own AI using ML.NET

Artificial Intelligence (AI) is transforming the way we develop applications. As a .NET developer, you can use ML.NET from Microsoft to integrate machine learning (ML) into your .NET applications.

In this blog post, I’ll walk you through creating a simple AI application capable of predicting house prices using ML.NET in less than 5 minutes.

Step 1: Setup your .NET project

From your terminal, create a new console application using:

dotnet new console

Now install the ML.NET nuget package

dotnet add package Microsoft.ML

 

Step 2: Prepare your data

For this example, we'll use a simple dataset: housing.csv containing house features and prices. Add the CSV file to your project directory. Ensure it has the following columns: Size, Bedrooms and Price.

Size,Bedrooms,Price 
850,2,200000 
900,3,230000 
1200,3,300000 
1500,3,400000 
1750,3,450000 
1850,4,500000 
2000,4,550000 
2100,4,600000 
2500,4,650000 
2700,5,700000 
3000,5,750000

 

Step 3: Define your data models

Create a new class for your data named HousingData.cs that has the same properties as the columns in the previously added .csv file:

Now create another class called HousingPricePrediction.cs that we'll use to populate the output prediction:

 

Step 4: Training your model

In our example we’ll be using a Regression trainer to created predictions based on our house pricing data. In your Program.cs file, add the following code to train you model:

(I’ve explained each code snippet in details in the code.) 

Now run your project. If you’re curious, you can now see your trained model in the specified output file.

 

Step 5: Use your AI to make predictions

Lastly we’ll load the previously created model (the .zip) in to something called a prediction engine. Using this engine we can create a house price prediction based on input sample data.

Run your application! You should now see the predicted price for the sample data printed in the console, in this case $167,405.52.

 

Congratulations! 🥳

You've built your very first AI application with ML.NET. This example demonstrates how to integrate machine learning into your .NET applications.

In this example we used the Regression trainer to created predictions based on house pricing data, but there are a variety of different trainers available in ML.NET such as Text Generation, Image & Object Classification, Recommendation Systems and much more.

Some trainers are more advanced than others which is why I recommend the regression trainer as a first introduction to machine learning since it is quite straightforward. 

To explore more advanced features of ML.NET to enhance your AI capabilities visit the ML.NET documentation

 

Source code

"There is a special place in hell for those who put screenshots of code in tutorials"

Yes I'm quoting you Callum 🫣 but don't worry. I've included the full source code for this tutorial here and I've also linked all the screenshots to the corresponding source code file from GitHub. I do agree with Callum that code in screenshots is not ideal but I have yet to found a code formatter for this blog that is not too bloated.

Cheers friends! ❤️