public IEnumerable <DemandPrediction> PredictBatch(IDataView inputDataView)
        {
            CheckTrained();
            var predictions = TrainedModel.Transform(inputDataView);

            return(predictions.AsEnumerable <DemandPrediction>(_mlcontext, reuseRowObject: false));
        }
        public RegressionEvaluator.Result Evaluate(IDataView testData)
        {
            CheckTrained();
            var predictions = TrainedModel.Transform(testData);
            var metrics     = _mlcontext.Regression.Evaluate(predictions, "Count", "Score");

            return(metrics);
        }
Exemple #3
0
        public override ModelFitness Evaluate(List <ModelDataSet> data, ModelValue prediction)
        {
            if (TrainedModel == null)
            {
                throw new Exception("Must initialize the model before calling");
            }

            lock (TrainedModel)
            {
#if ML_LEGACY
                var testData  = CollectionDataSource.Create(data);
                var evaluator = new RegressionEvaluator();
                var metrics   = evaluator.Evaluate(TrainedModel, testData);

                return(new ModelFitness()
                {
                    RMS = metrics.Rms,
                    RSquared = metrics.RSquared
                });
#else
                var textLoader = GetTextLoader(Context, prediction);

                var pathToData = "";
                try
                {
                    // ugh have to spill data to disk for it to work!
                    pathToData = WriteToDisk(data, prediction);

                    IDataView dataView    = textLoader.Read(pathToData);
                    var       predictions = TrainedModel.Transform(dataView);
                    var       metrics     = Context.Regression.Evaluate(predictions, label: "Label", score: "Score");

                    return(new ModelFitness()
                    {
                        RMS = metrics.Rms,
                        RSquared = metrics.RSquared
                    });
                }
                finally
                {
                    // cleanup
                    if (!string.IsNullOrWhiteSpace(pathToData) && File.Exists(pathToData))
                    {
                        File.Delete(pathToData);
                    }
                }
#endif
            }
        }
Exemple #4
0
        // return r^2
        public override double Evaluate(List <DataSet> data, List <float> labels = null)
        {
            if (TrainedModel == null)
            {
                throw new InvalidOperationException("Must train/load a model before evaluating");
            }

            lock (TrainedModel)
            {
#if ML_LEGACY
                var testData = CollectionDataSource.Create(data);
                //var evaluator = new RegressionEvaluator();
                //var metrics = evaluator.Evaluate(TrainedModel, testData);
                return(0);

                //return metrics.RSquared;
#else
                var textLoader = GetTextLoader(Context);

                var pathToData = "";
                try
                {
                    // ugh have to spill data to disk for it to work!
                    pathToData = WriteToDisk(data);

                    IDataView dataView    = textLoader.Load(pathToData);
                    var       predictions = TrainedModel.Transform(dataView);
                    var       metrics     = Context.Regression.Evaluate(predictions, labelColumnName: "Label", scoreColumnName: "Score");

                    return(metrics.RSquared);
                }
                finally
                {
                    // cleanup
                    if (!string.IsNullOrWhiteSpace(pathToData) && File.Exists(pathToData))
                    {
                        File.Delete(pathToData);
                    }
                }
#endif
            }
        }
Exemple #5
0
        public override ModelFitness Evaluate(List <ModelDataSet> data, ModelValue prediction)
        {
            if (TrainedModel == null)
            {
                throw new Exception("Must initialize the model before calling");
            }

            lock (TrainedModel)
            {
                var textLoader = GetTextLoader(Context, prediction);

                var pathToData = "";
                try
                {
                    // ugh have to spill data to disk for it to work!
                    pathToData = WriteToDisk(data, prediction);

                    IDataView dataView    = textLoader.Load(pathToData);
                    var       predictions = TrainedModel.Transform(dataView);
                    var       metrics     = Context.Regression.Evaluate(predictions, labelColumnName: "Label", scoreColumnName: "Score");

                    return(new ModelFitness()
                    {
                        RMS = metrics.RootMeanSquaredError,
                        RSquared = metrics.RSquared
                    });
                }
                finally
                {
                    // cleanup
                    if (!string.IsNullOrWhiteSpace(pathToData) && File.Exists(pathToData))
                    {
                        File.Delete(pathToData);
                    }
                }
            }
        }