示例#1
0
        private static void Predict(MLContext mlContext, ITransformer model)
        {
            var           predictionFunction = model.MakePredictionFunction <SentimentData, SentimentPrediction>(mlContext);
            SentimentData sampleStatement    = new SentimentData
            {
                SentimentText = "This is a very rude movie"
            };
            var resultprediction = predictionFunction.Predict(sampleStatement);

            Console.WriteLine();
            Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============");

            Console.WriteLine();
            Console.WriteLine($"Sentiment: {sampleStatement.SentimentText} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Not Toxic")} | Probability: {resultprediction.Probability} ");
            Console.WriteLine("=============== End of Predictions ===============");
            Console.WriteLine();
        }
示例#2
0
        private static void UseModelWithSingleItem(MLContext mlContext, ITransformer model, string sentimentText)
        {
            PredictionEngine <SentimentData, SentimentPrediction> predictionFunction = model.CreatePredictionEngine <SentimentData, SentimentPrediction>(mlContext);

            SentimentData sampleStatement = new SentimentData
            {
                SentimentText = sentimentText
            };

            var resultprediction = predictionFunction.Predict(sampleStatement);

            Console.WriteLine();
            Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============");
            Console.WriteLine($"Sentiment: {sampleStatement.SentimentText} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultprediction.Probability} ");
            Console.WriteLine("=============== End of Predictions ===============");
            Console.WriteLine();
        }
示例#3
0
        private static void UseModelWithSingleItemMulti(MLContext mlContext, ITransformer model)
        {
            PredictionEngine <SentimentData, SentimentPredictionMulti> predictionFunction = mlContext.Model.CreatePredictionEngine <SentimentData, SentimentPredictionMulti>(model);
            SentimentData sampleStatement = new SentimentData
            {
                SentimentText = "This show is great"
            };
            var resultPrediction = predictionFunction.Predict(sampleStatement);

            Console.WriteLine();
            Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============");

            Console.WriteLine();
            Console.WriteLine($"Sentiment: {resultPrediction.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")}  ");

            Console.WriteLine("=============== End of Predictions ===============");
            Console.WriteLine();
        }
示例#4
0
        public static void UseModelWithSingleItem(MLContext mlContext, ITransformer model)
        {
            PredictionEngine <SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine <SentimentData, SentimentPrediction>(model);
            SentimentData sampleStatement = new SentimentData {
                SentimentText = "This is a very bad steak"
            };
            var resultPrediction = predictionFunction.Predict(sampleStatement);

            Console.WriteLine();
            Console.WriteLine("========== Prediction test of model with a single sample and test dataset ==========");

            Console.WriteLine();
            Console.WriteLine($"Sentiment: {resultPrediction.SentimentText} | " +
                              $"Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")} | " +
                              $"Probability: {resultPrediction.Probability}");

            Console.WriteLine("========== End of predictions ==========");
            Console.WriteLine();
        }
示例#5
0
        private static void UseLoadedModelWithUserInput(MLContext mlContext)
        {
            //Load the saved (already trained) model
            ITransformer loadedModel;

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

            PredictionEngine <SentimentData, SentimentPrediction> predictionFunction = loadedModel.CreatePredictionEngine <SentimentData, SentimentPrediction>(mlContext);

            Console.WriteLine();
            Console.WriteLine("=== Prediction test of model with user input ===");
            Console.WriteLine();
            Console.WriteLine("Please enter your comment here:");
            string        comment            = Console.ReadLine();
            SentimentData sentimentStatement = new SentimentData
            {
                SentimentText = comment
            };

            //Predict results for user input
            var resultprediction = predictionFunction.Predict(sentimentStatement);

            Console.WriteLine($"Sentiment: {sentimentStatement.SentimentText} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultprediction.Probability} ");

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Please enter another comment here:");
            string        comment2            = Console.ReadLine();
            SentimentData sentimentStatement2 = new SentimentData
            {
                SentimentText = comment2
            };
            //Just another example
            var resultprediction2 = predictionFunction.Predict(sentimentStatement2);

            Console.WriteLine($"Sentiment: {sentimentStatement2.SentimentText} | Prediction: {(Convert.ToBoolean(resultprediction2.Prediction) ? "Positive" : "Negative")} | Probability: {resultprediction2.Probability} ");

            Console.WriteLine("=== End of predictions with user data ===");
            Console.WriteLine();
        }
示例#6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="mlContext"></param>
        /// <param name="model"></param>
        private static void UseModelWithSingleItem(MLContext mlContext, ITransformer model, string opinion)
        {
            PredictionEngine <SentimentData, SentimentPrediction> predictionFunction = null;

            try
            {
                predictionFunction = mlContext.Model.CreatePredictionEngine <SentimentData, SentimentPrediction>(model);
                SentimentData sampleStatement = new SentimentData {
                    SentimentText = opinion
                };

                var resultPrediction = predictionFunction.Predict(sampleStatement);

                Console.WriteLine($"Sentiment: {resultPrediction.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultPrediction.Probability} ");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }
示例#7
0
        private static void UseModelWithSingleItem(MLContext mlContext, ITransformer model)
        {
            PredictionEngine <SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine <SentimentData, SentimentPrediction>(model);

            //The PredictionEngine allows you to perform a prediction on a single instance of data

            SentimentData sampleStatement = new SentimentData
            {
                SentimentText = "This place is very good"
            };
            var resultPrediction = predictionFunction.Predict(sampleStatement);

            Console.WriteLine();
            Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============");

            Console.WriteLine();
            Console.WriteLine($"Sentiment: {resultPrediction.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultPrediction.Probability} ");

            Console.WriteLine("=============== End of Predictions ===============");
            Console.WriteLine();
        }
示例#8
0
        private static void UseModelWithSingleItem(MLContext mlContext, ITransformer model)
        {
            PredictionEngine <SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine <SentimentData, SentimentPrediction>(model);
            SentimentData sampleStatement = new SentimentData
            {
                SentimentText = "This show is great"
            };
            var resultPrediction = predictionFunction.Predict(sampleStatement);

            Console.WriteLine();
            Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============");

            Console.WriteLine();
            Console.WriteLine($"Sentiment: {resultPrediction.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")}  ");

            Console.WriteLine("=============== End of Predictions ===============");
            Console.WriteLine();
            using (System.IO.StreamWriter file =
                       new System.IO.StreamWriter(@"C:\Users\siust\OneDrive\Desktop\test.txt", true))
            {
                file.WriteLine($"Sentiment: {resultPrediction.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")}  ");;
            }
        }
示例#9
0
        /// <summary>
        /// 创建测试数据的单个注释。
        /// 根据测试数据预测情绪。
        /// 结合测试数据和预测进行报告。
        /// 显示预测结果。
        /// </summary>
        /// <param name="mlContext">环境</param>
        /// <param name="model">模型</param>
        private static void UseModelWithSingleItem(MLContext mlContext, ITransformer model)
        {
            // <SnippetCreatePredictionEngine1>
            // PredictionEngine 是一个简便 API,可使用它对单个数据实例执行预测   PredictionEngine 不是线程安全型
            // 生产环境请使用 PredictionEnginePool服务
            // 1. 创建一次性预测引擎
            // 2. 训练实体
            // 3. 测试实体 必须包含同样的列
            PredictionEngine <SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine <SentimentData, SentimentPrediction>(model);
            // </SnippetCreatePredictionEngine1>

            // <SnippetCreateTestIssue1>
            // 创建一个测试对象 写入说出的话
            SentimentData sampleStatement = new SentimentData
            {
                SentimentText = "This was a very bad steak"
            };
            // </SnippetCreateTestIssue1>

            // <SnippetPredict>
            //将测试评论数据传递到 PredictionEngine
            //Predict() 函数对单行数据进行预测
            var resultPrediction = predictionFunction.Predict(sampleStatement);

            // </SnippetPredict>
            // <SnippetOutputPrediction>
            Console.WriteLine();
            Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============");

            Console.WriteLine();
            Console.WriteLine($"Sentiment: {resultPrediction.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultPrediction.Probability} ");

            Console.WriteLine("=============== End of Predictions ===============");
            Console.WriteLine();
            // </SnippetOutputPrediction>
        }
示例#10
0
        static void Main(string[] args)
        {
            MLContext mlContext = new MLContext();

            // Load Data
            IDataView     dataView      = mlContext.Data.LoadFromTextFile <SentimentData>(_dataPath, hasHeader: false);
            TrainTestData splitDataView = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);

            // BuildAndTrainModel
            var estimator = mlContext.Transforms.Text
                            .FeaturizeText(outputColumnName: "Features", inputColumnName: nameof(SentimentData.SentimentText))
                            .Append(mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(labelColumnName: "Label", featureColumnName: "Features", maximumNumberOfIterations: 100));

            Console.WriteLine("=============== Create and Train the Model ===============");
            var model = estimator.Fit(splitDataView.TrainSet);

            Console.WriteLine("=============== End of training ===============");
            Console.WriteLine();

            // Evaluate
            Console.WriteLine("=============== Evaluating Model accuracy with Test data===============");
            IDataView predictions = model.Transform(splitDataView.TestSet);

            CalibratedBinaryClassificationMetrics 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.AreaUnderRocCurve:P2}");
            Console.WriteLine($"F1Score: {metrics.F1Score:P2}");
            Console.WriteLine("=============== End of model evaluation ===============");

            // UseModelWithSingleItem
            PredictionEngine <SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine <SentimentData, SentimentPrediction>(model);

            SentimentData sampleStatement = new SentimentData
            {
                SentimentText = "이 영화 정말 재미없어요"
            };

            var resultPrediction = predictionFunction.Predict(sampleStatement);

            Console.WriteLine();
            Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset===============");
            Console.WriteLine();
            Console.WriteLine($"Sentiment: {resultPrediction.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultPrediction.Probability} ");
            Console.WriteLine("=============== End of Predictions ===============");
            Console.WriteLine();

            // UseModelWithBatchItems
            IEnumerable <SentimentData> sentiments = new[]
            {
                new SentimentData {
                    SentimentText = "지루한 영화에요"
                },
                new SentimentData {
                    SentimentText = "이거 정말 최고에요!"
                },
                new SentimentData {
                    SentimentText = "올해의 영화로 손꼽고 싶군요"
                }
            };

            IDataView batchComments = mlContext.Data.LoadFromEnumerable(sentiments);

            predictions = model.Transform(batchComments);

            // Use model to predict whether comment data is Positive (1) or Negative (0).
            IEnumerable <SentimentPrediction> predictedResults = mlContext.Data.CreateEnumerable <SentimentPrediction>(predictions, reuseRowObject: false);

            Console.WriteLine();
            Console.WriteLine("=============== Prediction Test of loaded model with multiple samples ===============");
            foreach (SentimentPrediction prediction in predictedResults)
            {
                Console.WriteLine($"Sentiment: {prediction.SentimentText} | Prediction:{ (Convert.ToBoolean(prediction.Prediction) ? "Positive" : "Negative")} | Probability:{ prediction.Probability}");
            }
            Console.WriteLine("=============== End of predictions ===============");
        }