This post illustrates how to build a simple machine learning model to predict Dublin property price index using ML.NET on .NET Core. You can see the full code of this example in this Github repository.
What is ML.NET?
ML.NET is an open-source, cross-platform machine learning library for .NET applications to make automatic predictions using the data available to your applications. You can make predictions with ML.NET such as classification/categorisation, regression/predict continuous values, anomaly detection, recommendations, time series/sequential data, image classification and much more.
You can train a machine learning model or choose an algorithm with ML.NET CLI on any environment. If you are using Visual Studio on Windows, you may consider using ML.NET Model Builder Extension (currently in preview) to build, train and ship custom machine learning models.
Creating a Machine Learning Model
We'll use the Dublin residential property price index data of the past 15 years (thanks to Central Statistics Office) and build a machine learning model based on this data. Then we'll use this machine learning model to make some predictions. Although our machine learning model is not aware of Brexit and housing issues in Dublin, it can give us some ideas on property price trends.
How to Choose the Best Algorithm?
There are multiple training algorithms to choose from. It would help if you thought about data accuracy and efficiency, business goals, scalability, complexity, how fast the model is and much more.
We'll simply use ML.NET Model Builder to understand which algorithm is best for our scenario. Model Builder can help us with different scenarios below:
- Sentiment Analysis
- Issue Classification
- Price Prediction
- Image Classification
- Custom
You can see the metrics below that generated by ML.NET Model Builder based on our scenario (Price Prediction) and training data.
Trainer | RSquared | Absolute-loss | Squared-loss | RMS-loss | Duration | #Iteration |
---|---|---|---|---|---|---|
SdcaRegression | 0,9355 | 3,74 | 25,68 | 5,07 | 6,5 | 1 |
FastTreeRegression | 0,7864 | 6,67 | 85,10 | 9,22 | 1,6 | 2 |
LightGbmRegression | 0,2805 | 14,42 | 295,97 | 17,20 | 1,0 | 3 |
A supervised machine learning task, regression is used to predict numbers. In this case, we'll use SDCA (Stochastic Dual Coordinate Ascent) regression in our example application.
Creating the Application
Let's create a console application on .NET Core to implement the code for creating and testing the model.
Create the application:
> dotnet new console -o DublinPropertyPricePrediction
Go to the application folder:
> cd DublinPropertyPricePrediction
Add ML.NET dependency:
> dotnet add DublinPropertyPricePrediction.csproj package Microsoft.ML
Create the model and make some predictions:
You can run and test the application by typing the following command in your terminal window:
> dotnet run
And the result will be as shown below (Base price index is January-2005 → 100):
> August-2006 → Predicted Price: 129.40654 Price: 133.3
> February-2012 → Predicted Price: 57.666794 Price: 54.5
> October-2019 → Predicted Price: 105.090164 Price: 106.2
> February-2020 → Predicted Price: 94.979744
ML.NET is a great library to add some cool AI features to .NET applications without having to be an expert on machine learning. I was so surprised to see it was that easy to work with, I hope you enjoy it.
Happy coding!