Exemple #1
0
        public void CreatePredictionModel()
        {
            // Define a feature contribution calculator for all the features.
            // Don't normalize the contributions.
            // https://docs.microsoft.com/en-us/dotnet/api/microsoft.ml.transforms.featurecontributioncalculatingestimator?view=ml-dotnet
            // "Does this estimator need to look at the data to train its parameters? No"
            var regressionData = _regressionModel.Transform(MLContext.Data.TakeRows(_transformedData, 1));

            var featureContributionCalculator = MLContext.Transforms
                                                .CalculateFeatureContribution(_regressionModel, normalize: false) // Estimator
                                                .Fit(regressionData);                                             // Transformer

            // Create the full transformer chain.
            var scoringPipeline = _transformationModel
                                  .Append(_regressionModel)
                                  .Append(featureContributionCalculator);

            // Create the prediction engine.
            _predictionEngine = MLContext.Model.CreatePredictionEngine <FeatureContributionData, FeatureContributionPrediction>(scoringPipeline);
        }
        public (double RSquaredX, double RSquaredY) Train(IList <Record> data)
        {
            mlContext = new MLContext(seed: 0);
            var train = data.Select(r => new RegressionRecord(r));

            dataView = mlContext.Data.LoadFromEnumerable(train);

            labelX = nameof(RegressionRecord.DisplayX);
            var pipelineX = mlContext.Regression.Trainers.FastTree(labelX, FEATURE_COLUMN_NAME);

            estimatorX = pipelineX.Fit(dataView);
            labelY     = nameof(RegressionRecord.DisplayY);
            var pipelineY = mlContext.Regression.Trainers.FastTree(labelY, FEATURE_COLUMN_NAME);

            estimatorY = pipelineY.Fit(dataView);
            GenPredictors();

            var transX = estimatorX.Transform(dataView);
            var evalX  = mlContext.Regression.Evaluate(transX, labelX);
            var transY = estimatorY.Transform(dataView);
            var evalY  = mlContext.Regression.Evaluate(transY, labelY);

            return(evalX.RSquared, evalY.RSquared);
        }