Exemplo n.º 1
0
        /// <summary>
        /// Performs cross validation on a pipeline.
        /// </summary>
        /// <typeparam name="TInput">Class type that represents input schema.</typeparam>
        /// <typeparam name="TOutput">Class type that represents prediction schema.</typeparam>
        /// <param name="pipeline">Machine learning pipeline may contain loader, transforms and at least one trainer.</param>
        /// <returns>List containing metrics and predictor model for each fold</returns>
        public CrossValidationOutput <TInput, TOutput> CrossValidate <TInput, TOutput>(LearningPipeline pipeline)
            where TInput : class
            where TOutput : class, new()
        {
            var environment = new MLContext();
            {
                Experiment                     subGraph              = environment.CreateExperiment();
                ILearningPipelineStep          step                  = null;
                List <ILearningPipelineLoader> loaders               = new List <ILearningPipelineLoader>();
                List <Var <TransformModel> >   transformModels       = new List <Var <TransformModel> >();
                Var <TransformModel>           lastTransformModel    = null;
                Var <IDataView>                firstPipelineDataStep = null;
                Var <PredictorModel>           firstModel            = null;
                ILearningPipelineItem          firstTransform        = null;
                foreach (ILearningPipelineItem currentItem in pipeline)
                {
                    if (currentItem is ILearningPipelineLoader loader)
                    {
                        loaders.Add(loader);
                        continue;
                    }

                    step = currentItem.ApplyStep(step, subGraph);

                    if (step is ILearningPipelineDataStep dataStep && dataStep.Model != null)
                    {
                        transformModels.Add(dataStep.Model);
                        if (firstPipelineDataStep == null)
                        {
                            firstPipelineDataStep = dataStep.Data;
                            firstTransform        = currentItem;
                        }
                    }
Exemplo n.º 2
0
        /// <summary>
        /// Performs train-test on a pipeline.
        /// </summary>
        /// <typeparam name="TInput">Class type that represents input schema.</typeparam>
        /// <typeparam name="TOutput">Class type that represents prediction schema.</typeparam>
        /// <param name="pipeline">Machine learning pipeline that contains <see cref="ILearningPipelineLoader"/>,
        /// transforms and at least one trainer.</param>
        /// <param name="testData"><see cref="ILearningPipelineLoader"/> that represents the test dataset.</param>
        /// <returns>Metrics and predictor model.</returns>
        public TrainTestEvaluatorOutput <TInput, TOutput> TrainTestEvaluate <TInput, TOutput>(LearningPipeline pipeline, ILearningPipelineLoader testData)
            where TInput : class
            where TOutput : class, new()
        {
            using (var environment = new TlcEnvironment())
            {
                Experiment                     subGraph              = environment.CreateExperiment();
                ILearningPipelineStep          step                  = null;
                List <ILearningPipelineLoader> loaders               = new List <ILearningPipelineLoader>();
                List <Var <ITransformModel> >  transformModels       = new List <Var <ITransformModel> >();
                Var <ITransformModel>          lastTransformModel    = null;
                Var <IDataView>                firstPipelineDataStep = null;
                Var <IPredictorModel>          firstModel            = null;
                ILearningPipelineItem          firstTransform        = null;
                foreach (ILearningPipelineItem currentItem in pipeline)
                {
                    if (currentItem is ILearningPipelineLoader loader)
                    {
                        loaders.Add(loader);
                        continue;
                    }

                    step = currentItem.ApplyStep(step, subGraph);

                    if (step is ILearningPipelineDataStep dataStep && dataStep.Model != null)
                    {
                        transformModels.Add(dataStep.Model);
                        if (firstPipelineDataStep == null)
                        {
                            firstPipelineDataStep = dataStep.Data;
                            firstTransform        = currentItem;
                        }
                    }
Exemplo n.º 3
0
        public PredictionModel<IrisData, IrisPrediction> Train()
        {
            string datapath = textBox1.Text;
            int index = listBox1.SelectedIndex;
            switch (index)
            {
                case 0:
                    algorithm = new StochasticDualCoordinateAscentClassifier();
                    break;
                case 1:
                    algorithm = new NaiveBayesClassifier();
                    break;
                case 2:
                    algorithm = new FastForestBinaryClassifier();
                    break;
            }

            GetColumns gc = new GetColumns();
            var iris = gc.getColumns();

            var pipeline = new LearningPipeline {
                new TextLoader(datapath).CreateFrom<IrisData>(useHeader: true, separator: ','),
                new Dictionarizer("Label"),
                new ColumnConcatenator("Features", iris),
                algorithm,
                new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" }
            };
            PredictionModel<IrisData, IrisPrediction> model = pipeline.Train<IrisData, IrisPrediction>();
            return model;
        }
        /// <summary>
        /// Removes an existing <see cref="ILearningPipelineItem"/>ILearningPipelineItem</see> item.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public IFluentLearningPipeline <TInput, TOutput> Remove(ILearningPipelineItem item)
        {
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (HasPipeline)
            {
                _pipeline.Remove(item);
            }

            return(this);
        }
        /// <summary>
        /// Add a <see cref="ILearningPipelineItem"/>ILearningPipelineItem</see> to the LearningPipeline
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public IFluentLearningPipeline <TInput, TOutput> Add(ILearningPipelineItem item)
        {
            if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (HasPipeline && !_pipeline.Contains(item))
            {
                _pipeline.Add(item);
            }

            return(this);
        }
Exemplo n.º 6
0
 public ModelBuilder(string trainingDataLocation, ILearningPipelineItem algorythm)
 {
     _trainingDataLocation = trainingDataLocation;
     _algorythm            = algorythm;
 }
Exemplo n.º 7
0
        TrainAndGetMetrics(ILearningPipelineLoader dataTrain, ILearningPipelineLoader dataTest, ILearningPipelineItem trainer)
        {
            var pipeline = new LearningPipeline();

            pipeline.Add(dataTrain);
            pipeline.Add(trainer);
            var model     = pipeline.Train <MLNetData, MLNetPredict>();
            var evaluator = new BinaryClassificationEvaluator();
            var metrics   = evaluator.Evaluate(model, dataTest);

            return(metrics.Accuracy, metrics.Auc, metrics.F1Score, model);
        }
Exemplo n.º 8
0
        private LocalEnvironment _mlContext = new LocalEnvironment(seed: null); // v0.6;

        public PredictionModel <BinaryClassificationData, BinaryClassificationPrediction> BuildAndTrain(string trainingDataPath, ILearningPipelineItem algorithm)
        {
            var pipeline = new LearningPipeline();

            pipeline.Add(new TextLoader(trainingDataPath).CreateFrom <BinaryClassificationData>(useHeader: true, separator: ';'));
            pipeline.Add(new MissingValueSubstitutor("FixedAcidity")
            {
                ReplacementKind = NAReplaceTransformReplacementKind.Mean
            });
            pipeline.Add(MakeNormalizer());
            pipeline.Add(new ColumnConcatenator("Features",
                                                "FixedAcidity",
                                                "VolatileAcidity",
                                                "CitricAcid",
                                                "ResidualSugar",
                                                "Chlorides",
                                                "FreeSulfurDioxide",
                                                "TotalSulfurDioxide",
                                                "Density",
                                                "Ph",
                                                "Sulphates",
                                                "Alcohol"));
            pipeline.Add(algorithm);

            return(pipeline.Train <BinaryClassificationData, BinaryClassificationPrediction>());
        }
 public Task <PredictionModel <BinaryClassificationData, BinaryClassificationPrediction> > BuildAndTrain(string trainingDataPath, ILearningPipelineItem algorithm)
 {
     return(Task.Run(() =>
     {
         return _model.BuildAndTrain(trainingDataPath, algorithm);
     }));
 }