■ 선형 회귀 모델을 사용하는 방법을 보여준다.
▶ HouseData.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
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 } } |