■ 선형 회귀 모델 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


HouseData.cs

 

 

namespace TestProject

{

    /// <summary>

    /// 주택 데이터

    /// </summary>

    public class HouseData

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 크기 - Size

 

        /// <summary>

        /// 크기

        /// </summary>

        public float Size { get; set; }

 

        #endregion

        #region 가격 - Price

 

        /// <summary>

        /// 가격

        /// </summary>

        public float Price { get; set; }

 

        #endregion

    }

}

 

 

Prediction.cs

 

 

using Microsoft.ML.Data;

 

namespace TestProject

{

    /// <summary>

    /// 예측

    /// </summary>

    public class Prediction

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 가격 - Price

 

        /// <summary>

        /// 가격

        /// </summary>

        [ColumnName("Score")]

        public float Price { get; set; }

 

        #endregion

    }

}

 

 

Program.cs

 

 

using System;

using System.Globalization;

using System.Threading;

 

using Microsoft.ML;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            Console.WriteLine("BEGIN MAIN FUNCTION");

 

            CultureInfo cultureInfo = new CultureInfo("en-us");

 

            Thread.CurrentThread.CurrentCulture   = cultureInfo;

            Thread.CurrentThread.CurrentUICulture = cultureInfo;

 

            MLContext context = new MLContext();

   

            Console.WriteLine("BEGIN SET TRAINING DATA VIEW");

 

            HouseData[] trainingArray =

            {

                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 trainingDataView = context.Data.LoadFromEnumerable(trainingArray);

 

            Console.WriteLine("END SET TRAINING DATA VIEW");

 

            Console.WriteLine("BEGIN SET PIPELINE");

 

            var pipeline = context.Transforms.Concatenate("Features", new[] { "Size" })

                .Append(context.Regression.Trainers.Sdca(labelColumnName : "Price", maximumNumberOfIterations : 1000));

   

            Console.WriteLine("END SET PIPELINE");

 

            Console.WriteLine("BEGIN SET MODEL");

 

            var model = pipeline.Fit(trainingDataView);

   

            Console.WriteLine("END SET MODEL");

 

            Console.WriteLine("BEGIN PREDICT");

 

            HouseData houseData = new HouseData() { Size = 2.5f };

 

            Prediction predition = context.Model.CreatePredictionEngine<HouseData, Prediction>(model).Predict(houseData);

 

            Console.WriteLine("--------------------------------------------------");

            Console.WriteLine($"SIZE  : {houseData.Size * 1000} SQ FT");

            Console.WriteLine($"PRICE : {predition.Price * 100000:C}");

            Console.WriteLine("--------------------------------------------------");

 

            Console.WriteLine("END PREDICT");

 

            Console.WriteLine("END MAIN FUNCTION");

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요