Esempio n. 1
0
        static void Main(string[] args)
        {
            //Step 1 :-  We need to load the training data
            LoadTrainingData();

            // Step 2 :- Create object of MLCOntext
            var mlContext = new MLContext();
            // Step 3 :- Convert your data in to IDataView
            IDataView dataView = mlContext.CreateStreamingDataView
                                 <FeedBackTrainingData>(trainingdata);
            // Step 4 :- We need to create the pipe line
            // define the work flows in it.
            var pipeline = mlContext.Transforms.
                           Text.FeaturizeText("FeedBackText", "Features")
                           .Append(mlContext.BinaryClassification.Trainers.FastTree
                                       (numLeaves: 50, numTrees: 50, minDatapointsInLeaves: 1));
            // Step 5 :- Traing the algorithm and we want the model out
            var model = pipeline.Fit(dataView);

            // Step 6 :- Load the test data and run the test data
            // to check our models accuracy
            LoadTestData();
            IDataView dataView1 = mlContext.
                                  CreateStreamingDataView <FeedBackTrainingData>(testData);

            var predictions = model.Transform(dataView1);
            var metrics     = mlContext.BinaryClassification.Evaluate(predictions, "Label");

            Console.WriteLine(metrics.Accuracy);

            // Step 7 :- use the model
            string strcont = "Y";

            while (strcont == "Y")
            {
                Console.WriteLine("Enter a feedback string");
                string feedbackstring     = Console.ReadLine().ToString();
                var    predictionFunction = model.MakePredictionFunction
                                            <FeedBackTrainingData, FeedBackPrediction>
                                                (mlContext);
                var feedbackinput = new FeedBackTrainingData();
                feedbackinput.FeedBackText = feedbackstring;
                var feedbackpredicted = predictionFunction.Predict(feedbackinput);
                Console.WriteLine("Predicted :- " + feedbackpredicted.IsGood);
            }
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Very first machine learning algorithm \nTo determine is a sentence is good or bad\n.");
            //step one load training data
            LoadTraningData();

            var mlContext = new MLContext();

            IDataView dataview = mlContext.Data.LoadFromEnumerable <FeedBackTrainingData>(trainingData);

            var pipline = mlContext.Transforms
                          .Text.FeaturizeText("Features", "FeedbackText")
                          .Append(mlContext.BinaryClassification.Trainers
                                  .FastTree(numberOfLeaves: 50, numberOfTrees: 50, minimumExampleCountPerLeaf: 1));

            var model = pipline.Fit(dataview);

            LoadTestData();

            IDataView testdataview = mlContext.Data.LoadFromEnumerable <FeedBackTrainingData>(testData);

            var predictions = model.Transform(testdataview);

            var metrics = mlContext.BinaryClassification.Evaluate(predictions, "Label");

            var predictionFunction = mlContext.Model.CreatePredictionEngine
                                     <FeedBackTrainingData, FeedBackPrediction>
                                         (model);

            while (true)
            {
                Console.WriteLine("Enter a feed back string: ");

                string feedBackString = Console.ReadLine().ToString();

                var feedbackInput = new FeedBackTrainingData();

                feedbackInput.FeedbackText = feedBackString;

                var feedbackPredicted = predictionFunction.Predict(feedbackInput);

                Console.WriteLine("Predicted Text :- " + feedbackPredicted.IsGood);

                Console.WriteLine("Accuracy of Prediction:- " + metrics.Accuracy);
                Console.ReadLine();
            }
        }