■ 이진 클래스 분류 모델 사용하기

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


TestProject.zip


SentimentData.cs

 

 

using Microsoft.ML.Data;

 

namespace TestProject

{

    /// <summary>

    /// 감정 데이터

    /// </summary>

    public class SentimentData

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

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

 

        #region Field

 

        /// <summary>

        /// 감정 텍스트

        /// </summary>

        [LoadColumn(0)]

        public string SentimentText;

 

        /// <summary>

        /// 감정

        /// </summary>

        [LoadColumn(1)]

        [ColumnName("Label")]

        public bool Sentiment;

 

        #endregion

    }

}

 

 

SentimentPrediction.cs

 

 

using Microsoft.ML.Data;

 

namespace TestProject

{

    /// <summary>

    /// 감정 예측

    /// </summary>

    public class SentimentPrediction : SentimentData

    {

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

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

 

        #region 예측 - Prediction

 

        /// <summary>

        /// 예측

        /// </summary>

        [ColumnName("PredictedLabel")]

        public bool Prediction { get; set; }

 

        #endregion

        #region 확률 - Probability

 

        /// <summary>

        /// 확률

        /// </summary>

        public float Probability { get; set; }

 

        #endregion

        #region 점수 - Score

 

        /// <summary>

        /// 점수

        /// </summary>

        public float Score { get; set; }

 

        #endregion

    }

}

 

 

Program.cs

 

 

using System;

using System.Collections.Generic;

using System.IO;

 

using Microsoft.ML;

using Microsoft.ML.Data;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

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

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

 

        #region Field

 

        /// <summary>

        /// 레이블 파일 경로

        /// </summary>

        private static readonly string _labelFilePath = Path.Combine(Environment.CurrentDirectory, "Data", "label.txt");

 

        #endregion

 

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

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

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

 

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

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            Console.WriteLine("BEGIN MAIN FUNCTION");

 

            MLContext context = new MLContext();

 

            DataOperationsCatalog.TrainTestData trainTestData = GetTrainTestData(context);

 

            ITransformer model = GetModel(context, trainTestData.TrainSet);

 

            Evaluate(context, model, trainTestData.TestSet);

 

            PredictSingleItem(context, model);

 

            Predict(context, model);

 

            Console.WriteLine("END MAIN FUNCTION");

        }

 

        #endregion

 

        #region 훈련 테스트 데이터 구하기 - GetTrainTestData(context)

 

        /// <summary>

        /// 훈련 테스트 데이터 구하기

        /// </summary>

        /// <param name="context">ML 컨텍스트</param>

        /// <returns>훈련 테스트 데이터</returns>

        public static DataOperationsCatalog.TrainTestData GetTrainTestData(MLContext context)

        {

            Console.WriteLine("BEGIN GET TRAIN TEST DATA");

 

            IDataView dataView = context.Data.LoadFromTextFile<SentimentData>(_labelFilePath, hasHeader : false);

 

            DataOperationsCatalog.TrainTestData trainTestData = context.Data.TrainTestSplit(dataView, testFraction : 0.2);

 

            Console.WriteLine("END GET TRAIN TEST DATA");

 

            return trainTestData;

        }

 

        #endregion

        #region 모델 구하기 - GetModel(context, trainingDataView)

 

        /// <summary>

        /// 모델 구하기

        /// </summary>

        /// <param name="context">컨텍스트</param>

        /// <param name="trainingDataView">훈련 데이터 뷰</param>

        /// <returns>모델</returns>

        public static ITransformer GetModel(MLContext context, IDataView trainingDataView)

        {

            Console.WriteLine("BEGIN GET MODEL FUNCTION");

 

            var estimator = context.Transforms.Text.FeaturizeText

            (

                outputColumnName : "Features",

                inputColumnName  : nameof(SentimentData.SentimentText)

            )

            .Append

            (

                context.BinaryClassification.Trainers.SdcaLogisticRegression

                (

                    labelColumnName   : "Label",

                    featureColumnName : "Features"

                )

            );

 

            Console.WriteLine("BEGIN CREATE AND TRAIN THE MODEL");

 

            var model = estimator.Fit(trainingDataView);

 

            Console.WriteLine("END CREATE AND TRAIN THE MODEL");

 

            Console.WriteLine("END GET MODEL FUNCTION");

 

            return model;

        }

 

        #endregion

        #region 평가하기 - Evaluate(context, model, testDataView)

 

        /// <summary>

        /// 평가하기

        /// </summary>

        /// <param name="context">ML 컨텍스트</param>

        /// <param name="model">모델</param>

        /// <param name="testDataView">테스트 데이터 뷰</param>

        public static void Evaluate(MLContext context, ITransformer model, IDataView testDataView)

        {

            Console.WriteLine("BEGIN EVALUATE FUNCTION");

 

            IDataView predictions = model.Transform(testDataView);

 

            CalibratedBinaryClassificationMetrics metrics = context.BinaryClassification.Evaluate(predictions, "Label");

 

            Console.WriteLine("MODEL QUALITY METRICS EVALUATION");

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

            Console.WriteLine($"ACCURACY : {metrics.Accuracy:P2}"         );

            Console.WriteLine($"AUC      : {metrics.AreaUnderRocCurve:P2}");

            Console.WriteLine($"F1SCORE  : {metrics.F1Score:P2}"          );

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

 

            Console.WriteLine("END EVALUATE FUNCTION");

        }

 

        #endregion

        #region 단일 항목 예측하기 - PredictSingleItem(context, model)

 

        /// <summary>

        /// 단일 항목 예측하기

        /// </summary>

        /// <param name="context">컨텍스트</param>

        /// <param name="model">모델</param>

        private static void PredictSingleItem(MLContext context, ITransformer model)

        {

            Console.WriteLine("BEGIN PREDICT SINGLE ITEM FUNCTION");

 

            PredictionEngine<SentimentData, SentimentPrediction> predictionEngine =

                context.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(model);

 

            SentimentData sentimentData = new SentimentData { SentimentText = "This was a very bad steak" };

 

            SentimentPrediction sentimentPrediction = predictionEngine.Predict(sentimentData);

 

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

            Console.WriteLine($"SENTIMENT   : {sentimentPrediction.SentimentText}"                                            );

            Console.WriteLine($"PREDICTION  : {(Convert.ToBoolean(sentimentPrediction.Prediction) ? "Positive" : "Negative")}");

            Console.WriteLine($"PROBABILITY : {sentimentPrediction.Probability}"                                              );

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

 

            Console.WriteLine("END PREDICT SINGLE ITEM FUNCTION");

        }

 

        #endregion

        #region 예측하기 - Predict(context, model)

 

        /// <summary>

        /// 예측하기

        /// </summary>

        /// <param name="context">ML 컨텍스트</param>

        /// <param name="model">모델</param>

        public static void Predict(MLContext context, ITransformer model)

        {

            Console.WriteLine("BEGIN PREDICT FUNCTION");

 

            IEnumerable<SentimentData> sentimentDataEnumerable = new[]

            {

                new SentimentData { SentimentText = "This was a horrible meal" },

                new SentimentData { SentimentText = "I love this spaghetti."   }

            };

 

            IDataView sentimentDataView = model.Transform(context.Data.LoadFromEnumerable(sentimentDataEnumerable));

 

            IEnumerable<SentimentPrediction> sentimentPredictionEnumerable =

                context.Data.CreateEnumerable<SentimentPrediction>(sentimentDataView, reuseRowObject : false);

 

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

 

            foreach(SentimentPrediction sentimentPrediction  in sentimentPredictionEnumerable)

            {

                Console.WriteLine($"SENTIMENT   : {sentimentPrediction.SentimentText}");

                Console.WriteLine($"PREDICTION  : {(Convert.ToBoolean(sentimentPrediction.Prediction) ? "Positive" : "Negative")}");

                Console.WriteLine($"PROBABILITY : {sentimentPrediction.Probability}");

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

            }

 

            Console.WriteLine("END PREDICT FUNCTION");

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요