A Simple ML.NET Linear Regression Machine Learning Application





The code in the video tutorial project demonstrates the simplest ML.NET application. This

application solution is based on the Microsoft document at:



https://docs.microsoft.com/en-us/dotnet/machine-learning/how-does-mldotnet-work#hello-mlnet-world



This example constructs a linear regression model to predict house prices using house size and price data.



#machinelearning

#datascience

#artificialintelligence

#mlnet

#mlnettutorial

Code:
using Microsoft.ML;
using Microsoft.ML.Data;
using System;

namespace Hello_ML.NET
{
    class Program
    {
        public class HouseData
        {
            public float Size { get; set; }
            public float Price { get; set; }
        }

        public class Prediction
        {
            [ColumnName("Score")]
            public float Price { get; set; }
        }
        static void Main(string[] args)
        {
            MLContext mlContext = new MLContext();
// create training data
            HouseData[] houseData = {
               new HouseData() { Size = 1.1F, Price = 1.2F },
               new HouseData() { Size = 1.9F, Price = 2.3F },
               new HouseData() { Size = 2.8F, Price = 3.0F },
               new HouseData() { Size = 3.4F, Price = 3.7F } };
            IDataView trainingData = mlContext.Data.LoadFromEnumerable(houseData);
            // Specify data preparation and model training pipeline
            var pipeline = mlContext.Transforms.Concatenate("Features", new[] { "Size" })
           .Append(mlContext.Regression.Trainers.Sdca(labelColumnName: "Price", maximumNumberOfIterations: 100));
            //Train model
            var model = pipeline.Fit(trainingData);
            // Make a prediction
            var size = new HouseData() { Size = 2.5F };
            var price = mlContext.Model.CreatePredictionEngine<HouseData, Prediction>(model).Predict(size);

            Console.WriteLine($"Predicted price for size: {size.Size * 1000} sq ft= {price.Price * 100:C}k");



        }
    }
}

Comments

Popular posts from this blog

How to Fix "JsonReaderException: '}' is invalid after a single JSON valu...

ASP NET Core 3 1 Build a Complete MVC ToDo List Application Part 11