static void Main(string[] args)
        {
            Console.WriteLine("SentimentAnalysis Start!");
            //1. Create ML.NET context/environment
            MLContext mLContext = new MLContext();
            //2. Create DataReader with data schema mapped to file's columns
            string baseDataPath = @"Data/base.tsv";

            var reader = new TextLoader.Arguments()
            {
                Separator = "tab",
                HasHeader = true,
                Column    = new TextLoader.Column[] {
                    new TextLoader.Column("Label", DataKind.Bool, 0),
                    new TextLoader.Column("Text", DataKind.Text, 1)
                }
            };
            //Load training data
            IDataView trainingDataView = mLContext.Data.TextReader(reader).Read(new MultiFileSource(baseDataPath));

            //3.Create a flexible pipeline (composed by a chain of estimators) for creating/traing the model.
            var pipeline = mLContext.Transforms.Text.FeaturizeText("Text", "Features")
                           .Append(mLContext.BinaryClassification.Trainers.FastTree(numLeaves: 50, numTrees: 50, minDatapointsInLeafs: 20));
            //Train model
            var model = pipeline.Fit(trainingDataView);

            //Evaluate model
            var       testDataPath = @"Data/test.tsv";
            IDataView testDataView = mLContext.Data.TextReader(reader).Read(new MultiFileSource(testDataPath));
            var       predictions  = model.Transform(testDataView);
            var       metrics      = mLContext.BinaryClassification.Evaluate(predictions, "Label");

            Console.WriteLine();
            Console.WriteLine("Model quality metrics evaluation");
            Console.WriteLine("--------------------------------");
            Console.WriteLine($"Accuracy: {metrics.Accuracy:P2}");
            Console.WriteLine($"Auc: {metrics.Auc:P2}");
            Console.WriteLine($"F1Score: {metrics.F1Score:P2}");
            Console.WriteLine("=============== End of model evaluation ===============");
            Console.ReadLine();

            //Save Model
            using (var stream = new FileStream(@"Data/model.zip", FileMode.Create, FileAccess.Write, FileShare.Write))
            {
                mLContext.Model.Save(model, stream);
            }
            //Consume model
            var predictionFunct = model.MakePredictionFunction <SentimentIssue, SentimentPrediction>(mLContext);
            var sampleStatement = new SentimentIssue
            {
                Text = "This is a very rude movie"
            };
            var resultprediction = predictionFunct.Predict(sampleStatement);

            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(resultprediction.Prediction ? "Negative" : "Positive")} sentiment");
            Console.ReadLine();
        }
示例#2
0
        // (選用) 使用儲存的定型模型進行預測
        private static void TestSinglePrediction(MLContext mlContext)
        {
            // 要預測的資料來源
            var sampleStatement = new SentimentIssue {
                Text = "This is a very rude movie"
            };

            // 載入定型模型
            using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var trainedModel = mlContext.Model.Load(stream);
                // 建立預測引擎
                var predictionEngine = trainedModel.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(mlContext);
                // 計算預測值
                var predict = predictionEngine.Predict(sampleStatement);

                Console.WriteLine($"=============== Single Prediction  ===============");
                Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(predict.Prediction) ? "Toxic" : "Nice")} sentiment | Probability: {predict.Probability} ");
                Console.WriteLine($"==================================================");
            }
        }
        // (OPTIONAL) Try/test a single prediction by loading the model from the file, first.
        private static void TestSinglePrediction(MLContext mlContext)
        {
            ConsoleHelper.ConsoleWriteHeader("=============== Testing prediction engine ===============");
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = "This is a very rude movie"
            };

            ITransformer trainedModel = mlContext.Model.Load(ModelPath, out var modelInputSchema);

            Console.WriteLine($"=============== Loaded Model OK  ===============");

            // Create prediction engine related to the loaded trained model
            var predEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(trainedModel);

            Console.WriteLine($"=============== Created Prediction Engine OK  ===============");
            // Score
            var predictedResult = predEngine.Predict(sampleStatement);

            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(predictedResult.Prediction) ? "Toxic" : "Non Toxic")} sentiment");
            Console.WriteLine($"==================================================");
        }
示例#4
0
        // (OPTIONAL) Try/test a single prediction by loding the model from the file, first.
        private static void TestSinglePrediction(MLContext mlContext)
        {
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = "This is a very rude movie"
            };

            ITransformer trainedModel;

            using (var stream = new FileStream(ModelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                trainedModel = mlContext.Model.Load(stream);
            }

            // Create prediction engine related to the loaded trained model
            var predEngine = trainedModel.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(mlContext);

            //Score
            var resultprediction = predEngine.Predict(sampleStatement);

            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Nice")} sentiment | Probability: {resultprediction.Probability} ");
            Console.WriteLine($"==================================================");
        }