Esempio n. 1
0
        public float[] Evaluate(ITransformer transformer, DateTime dateTime)
        {
            if (_evaluation.ContainsKey(dateTime))
            {
                float[] score = new[] { _evaluation[dateTime] };
                return(score);
            }

            Dictionary <string, float>         feature  = new Dictionary <string, float>();
            List <Dictionary <string, float> > features = new List <Dictionary <string, float> >();

            var featuresDictionnary = GetFeatures(dateTime);

            if (featuresDictionnary == null)
            {
                return(null);
            }

            foreach (var item in featuresDictionnary)
            {
                feature.Add(item.Key, item.Value);
            }
            features.Add(feature);
            IDataView data = new FloatsDataView(features);

            MLContext mlContext = new MLContext();
            IDataView result    = transformer.Transform(data);

            float[] scoreColumn = result.GetColumn <float>("Score").ToArray();


            return(scoreColumn);
        }
Esempio n. 2
0
        internal void InitializeEvaluation(ITransformer transformer)
        {
            _evaluation = new Dictionary <DateTime, float>();
            DateTime from = _featurables.Min(c => c.Begin);
            //to = _featurables.Max(c => c.End);
            Period period      = Period.S;
            int    periodCount = 5;

            List <Dictionary <string, float> > features = new List <Dictionary <string, float> >();

            for (DateTime date = from; date < DateTime.Now; date = date.AddPeriod(period, periodCount))
            {
                if (_featurables.All(c => c.HasFeatures(date)))
                {
                    Dictionary <string, float> feature = new Dictionary <string, float>();
                    var featuresDictionnary            = GetFeatures(date);

                    if (featuresDictionnary == null)
                    {
                        continue;
                    }

                    foreach (var item in featuresDictionnary)
                    {
                        feature.Add(item.Key, item.Value);
                    }
                    feature["Label"] = 0;
                    features.Add(feature);
                    _evaluation[date] = 0;
                }
            }

            IDataView data   = new FloatsDataView(features);
            IDataView result = transformer.Transform(data);

            float[] scoreColumn = result.GetColumn <float>("Score").ToArray();

            int iterator = 0;

            for (DateTime date = from; date < DateTime.Now; date = date.AddPeriod(period, periodCount))
            {
                if (_evaluation.ContainsKey(date))
                {
                    _evaluation[date] = scoreColumn[iterator++];
                }
            }
        }
Esempio n. 3
0
        public ITransformer Fit(IEstimator <ITransformer> estimator, DateTime to)
        {
            Console.WriteLine($"Running {estimator.GetType().Name}");
            MLContext mlContext = new MLContext();

            DateTime from = _featurables.Min(c => c.Begin);
            //to = _featurables.Max(c => c.End);
            Period period      = Period.S;
            int    periodCount = 5;

            List <Dictionary <string, float> > features = new List <Dictionary <string, float> >();

            for (DateTime date = from; date < to; date = date.AddPeriod(period, periodCount))
            {
                if (_featurables.All(c => c.HasFeatures(date)) && _pipExpectation._futurPip.ContainsKey(date))
                {
                    Dictionary <string, float> feature = new Dictionary <string, float>();
                    var featuresDictionnary            = GetFeatures(date);

                    if (featuresDictionnary == null)
                    {
                        continue;
                    }

                    foreach (var item in featuresDictionnary)
                    {
                        feature.Add(item.Key, item.Value);
                    }
                    feature["Label"] = decimal.ToSingle(_pipExpectation._futurPip[date].Long);
                    features.Add(feature);
                }
            }

            IDataView data = new FloatsDataView(features);

            using (FileStream stream = new FileStream("data.tsv", FileMode.Create))
                mlContext.Data.SaveAsText(data, stream);

            string[] featureNames = features.First().Keys.Where(c => c != "Label").ToArray();

            var pipeline = mlContext.Transforms.Concatenate("Features", featureNames)
                           .Append(mlContext.Transforms.NormalizeMeanVariance("Features"))
                           .Append(mlContext.Transforms.NormalizeMinMax("Features"))
                           .AppendCacheCheckpoint(mlContext);
            //.Append(mlContext.Regression.Trainers.OnlineGradientDescent(numberOfIterations:10));

            // Create data prep transformer
            ITransformer transformData   = pipeline.Fit(data);
            IDataView    transformedData = transformData.Transform(data);

            //IEstimator<ITransformer> estimator = mlContext.Regression.Trainers.OnlineGradientDescent(numberOfIterations:10);

            var cvResults = mlContext.Regression.CrossValidate(transformedData, estimator, numberOfFolds: 5);


            // Apply transforms to training data
            var models = cvResults.OrderByDescending(fold => fold.Metrics.RSquared).ToArray();

            // Get Top Model
            var topModel = models[0];

            Console.WriteLine($"\tR2:                { topModel.Metrics.RSquared}");
            Console.WriteLine($"\tLossfunction:      { topModel.Metrics.LossFunction}");
            Console.WriteLine($"\tMeanAbsoluteError: { topModel.Metrics.MeanAbsoluteError}");

            //RegressionMetrics trainedModelMetrics = mlContext.Regression.Evaluate(testDataPredictions);
            //double rSquared = trainedModelMetrics.RSquared;

            return(transformData.Append(topModel.Model));
        }