[C#/ML.NET/.NETCORE] 모델 빌더를 사용해 택시 요금 예측하기
■ 모델 빌더를 사용해 택시 요금을 예측하는 방법을 보여준다. [TestLibrary 프로젝트] ▶ ModelInput.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 |
using Microsoft.ML.Data; namespace TestLibrary { /// <summary> /// 모델 입력 /// </summary> public class ModelInput { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 공급자 ID - VendorID /// <summary> /// 공급자 ID /// </summary> [ColumnName("vendor_id"), LoadColumn(0)] public string VendorID { get; set; } #endregion #region 요금 코드 - RateCode /// <summary> /// 요금 코드 /// </summary> [ColumnName("rate_code"), LoadColumn(1)] public float RateCode { get; set; } #endregion #region 승객 수 - PassengerCount /// <summary> /// 승객 수 /// </summary> [ColumnName("passenger_count"), LoadColumn(2)] public float PassengerCount { get; set; } #endregion #region 이동 시간 (단위 : 초) - TripTimeInSeconds /// <summary> /// 이동 시간 (단위 : 초) /// </summary> [ColumnName("trip_time_in_secs"), LoadColumn(3)] public float TripTimeInSeconds { get; set; } #endregion #region 이동 거리 - TripDistance /// <summary> /// 이동 거리 /// </summary> [ColumnName("trip_distance"), LoadColumn(4)] public float TripDistance { get; set; } #endregion #region 지불 타입 - PaymentType /// <summary> /// 지불 타입 /// </summary> [ColumnName("payment_type"), LoadColumn(5)] public string PaymentType { get; set; } #endregion #region 요금 - FareAmount /// <summary> /// 요금 /// </summary> [ColumnName("fare_amount"), LoadColumn(6)] public float FareAmount { get; set; } #endregion } } |
▶ ModelOutput.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
namespace TestLibrary { /// <summary> /// 모델 입력 /// </summary> public class ModelOutput { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 점수 - Score /// <summary> /// 점수 /// </summary> public float Score { get; set; } #endregion } } |
▶ ConsumeModel.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 |
using Microsoft.ML; using System; namespace TestLibrary { /// <summary> /// 모델 소비 /// </summary> public class ConsumeModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 예측 엔진 LAZY /// </summary> private static Lazy<PredictionEngine<ModelInput, ModelOutput>> _predictionEngineLazy = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(CreatePredictionEngine); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 예측 엔진 생성하기 - CreatePredictionEngine() /// <summary> /// 예측 엔진 생성하기 /// </summary> /// <returns>예측 엔진</returns> public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine() { MLContext context = new MLContext(); string filePath = @"WEIGHT\MLModel.zip"; ITransformer mlModel = context.Model.Load(filePath, out var modelInputSchema); PredictionEngine<ModelInput, ModelOutput> predictionEngine = context.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel); return predictionEngine; } #endregion #region 예측하기 - Predict(input) /// <summary> /// 예측하기 /// </summary> /// <param name="input">모델 입력</param> /// <returns>모델 출력</returns> public static ModelOutput Predict(ModelInput input) { ModelOutput result = _predictionEngineLazy.Value.Predict(input); return result; } #endregion } } |
[TestProtect 프로젝트]