Esempio n. 1
0
        static void Main(string[] args)
        {
            //1. carica i dati di training
            LoadTrainingData();

            //2. crea un oggetto di MLContext
            var mlContext = new MLContext();

            //3. converte i dato in IData View
            //LoadFromEnumerable era ReadFromEnumerable
            IDataView dataView = mlContext.Data.LoadFromEnumerable(trainingdata); //<FeedBackTrainingData> fra "LoadFromEnumerable" e "(trainingdata);"

            //4. crea la pipeline

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

            //5. train
            var model = pipeline.Fit(dataView);

            //6. testare con dati appositi, diversi da quelli di training
            LoadTestData();
            IDataView dataView1   = mlContext.Data.LoadFromEnumerable(trainingdata);
            var       predictions = model.Transform(dataView1);
            var       metrics     = mlContext.BinaryClassification.Evaluate(predictions, "Label");

            Console.WriteLine(metrics.Accuracy);
            Console.Read();

            //7. utilizzare il modello
            Console.WriteLine("Enter a feedback string: ");
            string feedbackstring = Console.Read().ToString();

            /*var predictionFunction = model.MakePredictionFunction
             *                              <FeedBackTrainingData, FeedBackPrediction>
             *                              (mlContext.Context);*/
            var feedbackinput = new FeedBackTrainingData();

            feedbackinput.FeedBackText = feedbackstring;
            predictionFunction.predict(feedbackinput);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            int i = 0;

            while (i < 3)
            {
                i += 1;
                //1. carica i dati di training
                LoadTrainingData();

                //2. crea un oggetto di MLContext
                var mlContext = new MLContext();

                //3. converte i dato in IData View
                IDataView dataView = mlContext.CreateStreamingDataView <FeedBackTrainingData>(trainingdata);

                //4. crea la pipeline

                var pipeline = mlContext.Transforms.Text.FeaturizeText("FeedBackText", "Features").Append(mlContext.BinaryClassification.Trainers.FastTree(numLeaves: 50, numTrees: 50, minDatapointsInLeaves: 1));

                //5. train
                var model = pipeline.Fit(dataView);

                //6. testare con dati appositi, diversi da quelli di training
                LoadTestData();
                IDataView dataView1   = mlContext.CreateStreamingDataView <FeedBackTrainingData>(testData);
                var       predictions = model.Transform(dataView1);
                var       metrics     = mlContext.BinaryClassification.Evaluate(predictions, "Label");
                Console.WriteLine(metrics.Accuracy);

                //7. utilizzare il modello
                Console.Write("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("Is good (predicted): " + feedbackpredicted.IsGood);
                Console.ReadLine();
            }
        }