コード例 #1
0
        private static Task ClassifyAsync(
            IModelDefinitionBuilder <SpamInput, MulticlassClassificationFoldsAverageMetricsResult> modelBuilder,
            string text,
            string expectedResult,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            return(Task.Run(
                       () =>
            {
                var predictor = modelBuilder.MLContext.Model.CreatePredictionEngine <SpamInput, SpamPrediction>(modelBuilder.Model);

                var input = new SpamInput {
                    Message = text
                };

                var prediction = predictor.Predict(input);

                var result = prediction.IsSpam == "spam" ? "spam" : "not spam";

                if (prediction.IsSpam == expectedResult)
                {
                    logger.LogInformation("[ClassifyAsync][Predict] result: '{0}' is {1}", input.Message, result);
                }
                else
                {
                    logger.LogWarning("[ClassifyAsync][Predict] result: '{0}' is {1}", input.Message, result);
                }
            },
                       cancellationToken));
        }
コード例 #2
0
        public bool isSpam(string text)
        {
            // Create MLContext
            MLContext mlContext = new MLContext();
            //Define DataViewSchema for data preparation pipeline and trained model
            DataViewSchema modelSchema;

            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content", "model.zip");

            // Load trained model
            var model = mlContext.Model.Load(filePath, out modelSchema);

            var predictor = mlContext.Model.CreatePredictionEngine <SpamInput, SpamPrediction>(model);
            var input     = new SpamInput {
                Message = text
            };
            var prediction = predictor.Predict(input);


            if (prediction.isSpam == "spam")
            {
                return(true);
            }
            return(false);
        }
コード例 #3
0
        public IActionResult Predict(SpamInput input)
        {
            var model = new SpamDetectionMLModel();

            model.Build();
            model.Train();
            ViewBag.Prediction = model.Predict(input);
            return(View());
        }
コード例 #4
0
        public static void ClassifyMessage(PredictionEngine <SpamInput, SpamPrediction> predictor, string message)
        {
            var input = new SpamInput {
                Message = message
            };
            var prediction = predictor.Predict(input);

            Console.WriteLine("The message '{0}' is {1}", input.Message, prediction.isSpam ? "spam" : "not spam");
        }
コード例 #5
0
        private void ClassifySpamMessage(PredictionEngine <SpamInput, SpamPrediction> predictor, string message)
        {
            var input = new SpamInput {
                Message = message
            };
            var prediction = predictor.Predict(input);

            _logger.LogInformation("The message '{0}' is {1}", input.Message, prediction.IsSpam == "spam" ? "spam" : "not spam");
        }
コード例 #6
0
        public static void ClassifyMessage(PredictionEngine <SpamInput, SpamPrediction> predictor, string message)
        {
            var input = new SpamInput {
                Message = message
            };
            var prediction = predictor.Predict(input);
            var isSpamStr  = prediction.IsSpam == "spam" ? "spam" : "not spam";

            Log.Information($"The message '{input.ToJson(true)}' is '{isSpamStr}'");
        }
        public bool IsTrue(SpamInput input)
        {
            var model = new SpamDetectionMLModel();

            model.Build();
            model.Train();
            var result = model.Predict(input);

            return(Convert.ToBoolean(result.isSpam));
        }
コード例 #8
0
        public static bool PredictMessage(string message)
        {
            if (SpamEngine == null)
            {
                Log.Information("SpamEngine need be built.");
                SetupEngine();
            }

            var input = new SpamInput {
                Message = message
            };
            var predict = SpamEngine.Predict(input);
            var isSpam  = predict.IsSpam == "spam";

            Log.Information($"IsSpam: {isSpam}");

            return(isSpam);
        }
        public IActionResult Predict(SpamInput input)
        {
            var predictVm = new PredictViewModel();
            var result    = predictVm.IsTrue(input);

            if (!ModelState.IsValid)
            {
                return(View());
            }

            if (result)
            {
                ModelState.AddModelError("Message", "Invalid text found");
                input.Message = "";
                return(View(input));
            }

            return(View());
        }
コード例 #10
0
        static void userMessage(string message, PredictionEngine <SpamInput, SpamPrediction> predictionEngine)
        {
            var messages = new SpamInput[] {
                new SpamInput()
                {
                    Message = message
                }
            };

            var myPredictions = from m in messages
                                select(Message : m.Message, Prediction : predictionEngine.Predict(m));

            foreach (var p in myPredictions)
            {
                Console.WriteLine($"  [{p.Prediction.Probability:P2}] {p.Message}");
            }

            Console.WriteLine("Skriv noget spam eller ham: ");

            var userInput = Console.ReadLine();

            userMessage(userInput, predictionEngine);
        }
コード例 #11
0
        static void Main(string[] args)
        {
            var context = new MLContext();
            // data fra spam.csv(træningsæt) bliver læst in i memorien
            var data = context.Data.LoadFromTextFile <SpamInput>(
                path: dataPath,
                hasHeader: true,
                separatorChar: ',');
            // use 80% for training and 20% for testing
            var partitions = context.Data.TrainTestSplit(
                data,
                testFraction: 0.2);

            // Først så laver den spam, eller ham om en til en bool'
            // Som vores model kan arbejde med.
            // featurized text hjælper med hvordan model skal forstå data
            // til sidst bruger jeg en metode, der hjælper på at lave mere præsise forudsigelser
            var pipeline = context.Transforms.CustomMapping <FromLabel, ToLabel>(
                mapAction: (input, output) => { output.Label = input.RawLabel == "spam" ? true : false; },
                contractName: "MyLambda")

                           .Append(context.Transforms.Text.FeaturizeText(
                                       outputColumnName: "Features",
                                       inputColumnName: nameof(SpamInput.Message)))

                           .Append(context.BinaryClassification.Trainers.SdcaLogisticRegression());



            // her træner jeg min model.
            var model = pipeline.Fit(partitions.TrainSet);


            // Evaluerer min model
            Console.WriteLine("Evalulerer modellen");
            var predictions = model.Transform(partitions.TestSet);
            var metrics     = context.BinaryClassification.Evaluate(
                data: predictions,
                labelColumnName: "Label",
                scoreColumnName: "Score");


            Console.WriteLine($"  Accuracy:          {metrics.Accuracy:P2}");
            Console.WriteLine($"  Auc:               {metrics.AreaUnderRocCurve:P2}");
            Console.WriteLine($"  Auprc:             {metrics.AreaUnderPrecisionRecallCurve:P2}");
            Console.WriteLine($"  F1Score:           {metrics.F1Score:P2}");
            Console.WriteLine($"  LogLoss:           {metrics.LogLoss:0.##}");
            Console.WriteLine($"  LogLossReduction:  {metrics.LogLossReduction:0.##}");
            Console.WriteLine($"  PositivePrecision: {metrics.PositivePrecision:0.##}");
            Console.WriteLine($"  PositiveRecall:    {metrics.PositiveRecall:0.##}");
            Console.WriteLine($"  NegativePrecision: {metrics.NegativePrecision:0.##}");
            Console.WriteLine($"  NegativeRecall:    {metrics.NegativeRecall:0.##}");
            Console.WriteLine();


            Console.WriteLine("Predicter udfald af beskeder");
            var predictionEngine = context.Model.CreatePredictionEngine <SpamInput, SpamPrediction>(model);

            var messages = new SpamInput[] {
                new SpamInput()
                {
                    Message = "Hello, win free iPad"
                },
                new SpamInput()
                {
                    Message = "Im home in few"
                },
                new SpamInput()
                {
                    Message = "Hundreds of medications all 80% off or more!"
                },
                new SpamInput()
                {
                    Message = "CONGRATS U WON LOTERY CLAIM UR 1 MILIONN DOLARS PRIZE"
                },
            };

            var myPredictions = from m in messages
                                select(Message : m.Message, Prediction : predictionEngine.Predict(m));

            foreach (var p in myPredictions)
            {
                Console.WriteLine($"  [{p.Prediction.Probability:P2}] {p.Message}");
            }

            Console.WriteLine("Skriv noget spam eller ham :");

            var userInput = Console.ReadLine();

            userMessage(userInput, predictionEngine);
        }
        public SpamPrediction Predict(SpamInput input)
        {
            var predictor = mlContext.Model.CreatePredictionEngine <SpamInput, SpamPrediction>(_model);

            return(predictor.Predict(input));
        }