예제 #1
0
        Gam(
            this SweepableRegressionTrainers trainers,
            string labelColumnName   = "Label",
            string featureColumnName = "Features",
            SweepableOption <GamRegressionTrainer.Options> optionSweeper = null,
            GamRegressionTrainer.Options defaultOption = null)
        {
            var context = trainers.Context;

            if (optionSweeper == null)
            {
                optionSweeper = GamRegressionTrainerSweepableOptions.Default;
            }

            optionSweeper.SetDefaultOption(defaultOption);

            return(context.AutoML().CreateSweepableEstimator(
                       (context, option) =>
            {
                option.LabelColumnName = labelColumnName;
                option.FeatureColumnName = featureColumnName;

                return context.Regression.Trainers.Gam(option);
            },
                       optionSweeper,
                       new string[] { labelColumnName, featureColumnName },
                       new string[] { Score },
                       nameof(GamRegressionTrainer)));
        }
예제 #2
0
        /// <summary>
        /// Create <see cref="GamRegressionTrainer"/> using advanced options, which predicts a target using generalized additive models (GAM).
        /// </summary>
        /// <param name="catalog">The <see cref="RegressionCatalog"/>.</param>
        /// <param name="options">Trainer options.</param>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        /// [!code-csharp[Gam](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/GamWithOptions.cs)]
        /// ]]>
        /// </format>
        /// </example>
        public static GamRegressionTrainer Gam(this RegressionCatalog.RegressionTrainers catalog,
                                               GamRegressionTrainer.Options options)
        {
            Contracts.CheckValue(catalog, nameof(catalog));
            var env = CatalogUtils.GetEnvironment(catalog);

            return(new GamRegressionTrainer(env, options));
        }
예제 #3
0
        public static PredictionEngine <TIn, TOut> GeneralizedAdditiveModel <TIn, TOut>(
            IEnumerable <TIn> trainDataset,
            GamRegressionTrainer.Options options,
            Action <ITransformer> additionModelAction = null) where TIn : class, new() where TOut : class, new()
        {
            var context          = new MLContext();
            var model            = context.RegressionTrainerTemplate(trainDataset, context.Regression.Trainers.Gam(options));
            var predictionEngine = context.Model.CreatePredictionEngine <TIn, TOut>(model);

            additionModelAction?.Invoke(model);
            return(predictionEngine);
        }
예제 #4
0
        // This example requires installation of additional NuGet package
        // <a href="https://www.nuget.org/packages/Microsoft.ML.FastTree/">Microsoft.ML.FastTree</a>.
        public static void Example()
        {
            // Create a new context for ML.NET operations. It can be used for exception tracking and logging,
            // as a catalog of available operations and as the source of randomness.
            // Setting the seed to a fixed number in this example to make outputs deterministic.
            var mlContext = new MLContext(seed: 0);

            // Create a list of training examples.
            var examples = GenerateRandomDataPoints(1000);

            // Convert the examples list to an IDataView object, which is consumable by ML.NET API.
            var trainingData = mlContext.Data.LoadFromEnumerable(examples);

            // Define trainer options.
            var options = new GamRegressionTrainer.Options
            {
                // The entropy (regularization) coefficient.
                EntropyCoefficient = 0.3,
                // Reduce the number of iterations to 50.
                NumberOfIterations = 50
            };

            // Define the trainer.
            var pipeline = mlContext.Regression.Trainers.Gam(options);

            // Train the model.
            var model = pipeline.Fit(trainingData);

            // Create testing examples. Use different random seed to make it different from training data.
            var testData = mlContext.Data.LoadFromEnumerable(GenerateRandomDataPoints(500, seed: 123));

            // Run the model on test data set.
            var transformedTestData = model.Transform(testData);

            // Convert IDataView object to a list.
            var predictions = mlContext.Data.CreateEnumerable <Prediction>(transformedTestData, reuseRowObject: false).ToList();

            // Look at 5 predictions
            foreach (var p in predictions.Take(5))
            {
                Console.WriteLine($"Label: {p.Label:F3}, Prediction: {p.Score:F3}");
            }

            // Expected output:
            //   Label: 0.985, Prediction: 0.841
            //   Label: 0.155, Prediction: 0.187
            //   Label: 0.515, Prediction: 0.496
            //   Label: 0.566, Prediction: 0.467
            //   Label: 0.096, Prediction: 0.144

            // Evaluate the overall metrics
            var metrics = mlContext.Regression.Evaluate(transformedTestData);

            Microsoft.ML.SamplesUtils.ConsoleUtils.PrintMetrics(metrics);

            // Expected output:
            //   Mean Absolute Error: 0.06
            //   Mean Squared Error: 0.01
            //   Root Mean Squared Error: 0.08
            //   RSquared: 0.93
        }
예제 #5
0
        // This example requires installation of additional NuGet
        // package for Microsoft.ML.FastTree found at
        // https://www.nuget.org/packages/Microsoft.ML.FastTree/
        public static void Example()
        {
            // Create a new context for ML.NET operations. It can be used for
            // exception tracking and logging, as a catalog of available operations
            // and as the source of randomness. Setting the seed to a fixed number
            // in this example to make outputs deterministic.
            var mlContext = new MLContext(seed: 0);

            // Create a list of training data points.
            var dataPoints = GenerateRandomDataPoints(1000);

            // Convert the list of data points to an IDataView object, which is
            // consumable by ML.NET API.
            var trainingData = mlContext.Data.LoadFromEnumerable(dataPoints);

            // Define trainer options.
            var options = new GamRegressionTrainer.Options
            {
                LabelColumnName   = nameof(DataPoint.Label),
                FeatureColumnName = nameof(DataPoint.Features),
                // The entropy (regularization) coefficient.
                EntropyCoefficient = 0.3,
                // Reduce the number of iterations to 50.
                NumberOfIterations = 50
            };

            // Define the trainer.
            var pipeline =
                mlContext.Regression.Trainers.Gam(options);

            // Train the model.
            var model = pipeline.Fit(trainingData);

            // Create testing data. Use different random seed to make it different
            // from training data.
            var testData = mlContext.Data.LoadFromEnumerable(
                GenerateRandomDataPoints(5, seed: 123));

            // Run the model on test data set.
            var transformedTestData = model.Transform(testData);

            // Convert IDataView object to a list.
            var predictions = mlContext.Data.CreateEnumerable <Prediction>(
                transformedTestData, reuseRowObject: false).ToList();

            // Look at 5 predictions for the Label, side by side with the actual
            // Label for comparison.
            foreach (var p in predictions)
            {
                Console.WriteLine($"Label: {p.Label:F3}, Prediction: {p.Score:F3}");
            }

            // Expected output:
            //   Label: 0.985, Prediction: 0.841
            //   Label: 0.155, Prediction: 0.187
            //   Label: 0.515, Prediction: 0.496
            //   Label: 0.566, Prediction: 0.467
            //   Label: 0.096, Prediction: 0.144

            // Evaluate the overall metrics
            var metrics = mlContext.Regression.Evaluate(transformedTestData);

            PrintMetrics(metrics);

            // Expected output:
            //   Mean Absolute Error: 0.04
            //   Mean Squared Error: 0.01
            //   Root Mean Squared Error: 0.05
            //   RSquared: 0.98 (closer to 1 is better. The worst case is 0)
        }