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

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

            optionSweeper.SetDefaultOption(defaultOption);

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

                return context.Regression.Trainers.LightGbm(option);
            },
                       optionSweeper,
                       new string[] { labelColumnName, featureColumnName },
                       new string[] { Score },
                       nameof(LightGbmRegressionTrainer)));
        }
Example #2
0
        /// <summary>
        /// Predict a target using a tree regression model trained with the <see cref="LightGbmRegressionTrainer"/>.
        /// </summary>
        /// <param name="catalog">The <see cref="RegressionCatalog"/>.</param>
        /// <param name="label">The label column.</param>
        /// <param name="features">The features column.</param>
        /// <param name="weights">The weights column.</param>
        /// <param name="options">Algorithm advanced settings.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}.Fit(DataView{TInShape})"/> method is called on the
        /// <see cref="Estimator{TInShape, TOutShape, TTransformer}"/> instance created out of this. This delegate will receive
        /// the linear model that was trained. Note that this action cannot change the result in any way;
        /// it is only a way for the caller to be informed about what was learnt.</param>
        /// <returns>The Score output column indicating the predicted value.</returns>
        public static Scalar <float> LightGbm(this RegressionCatalog.RegressionTrainers catalog,
                                              Scalar <float> label, Vector <float> features, Scalar <float> weights,
                                              LightGbmRegressionTrainer.Options options,
                                              Action <LightGbmRegressionModelParameters> onFit = null)
        {
            Contracts.CheckValue(options, nameof(options));
            CheckUserValues(label, features, weights, onFit);

            var rec = new TrainerEstimatorReconciler.Regression(
                (env, labelName, featuresName, weightsName) =>
            {
                options.LabelColumnName         = labelName;
                options.FeatureColumnName       = featuresName;
                options.ExampleWeightColumnName = weightsName;

                var trainer = new LightGbmRegressionTrainer(env, options);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                return(trainer);
            }, label, features, weights);

            return(rec.Score);
        }
Example #3
0
        /// <summary>
        /// Create <see cref="LightGbmRegressionTrainer"/> using advanced options, which predicts a target using a gradient boosting decision tree regression model.
        /// </summary>
        /// <param name="catalog">The <see cref="RegressionCatalog"/>.</param>
        /// <param name="options">Trainer options.</param>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        /// [!code-csharp[LightGbmRegression](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/Regression/LightGbmWithOptions.cs)]
        /// ]]>
        /// </format>
        /// </example>
        public static LightGbmRegressionTrainer LightGbm(this RegressionCatalog.RegressionTrainers catalog,
                                                         LightGbmRegressionTrainer.Options options)
        {
            Contracts.CheckValue(catalog, nameof(catalog));
            var env = CatalogUtils.GetEnvironment(catalog);

            return(new LightGbmRegressionTrainer(env, options));
        }
Example #4
0
        public static ITransformer WaitTimeTrainWithLightGbm(MLContext mlContext, IDataView trainData, EstimatorChain <ColumnConcatenatingTransformer> basePipeline)
        {
            var options = new LightGbmRegressionTrainer.Options
            {
                NumberOfIterations         = 100,
                NumberOfLeaves             = 10,
                MinimumExampleCountPerLeaf = 18,
                LearningRate = 0.09
            };

            //Console.WriteLine($"[{DateTime.UtcNow}] Method WaitTimeTrainWithLightGbm start");
            IDataView dataView = trainData;
            var       pipeline = basePipeline.Append(mlContext.Regression.Trainers.LightGbm(options));

            var waitTimeModel = pipeline.Fit(dataView);

            //Console.WriteLine($"[{DateTime.UtcNow}] Method WaitTimeTrainWithLightGbm end");
            return(waitTimeModel);
        }
Example #5
0
        public override IEstimator <ITransformer> BuildFromOption(MLContext context, LgbmOption param)
        {
            var option = new LightGbmRegressionTrainer.Options()
            {
                NumberOfLeaves             = param.NumberOfLeaves,
                NumberOfIterations         = param.NumberOfTrees,
                MinimumExampleCountPerLeaf = param.MinimumExampleCountPerLeaf,
                LearningRate            = param.LearningRate,
                NumberOfThreads         = AutoMlUtils.GetNumberOfThreadFromEnvrionment(),
                LabelColumnName         = param.LabelColumnName,
                FeatureColumnName       = param.FeatureColumnName,
                ExampleWeightColumnName = param.ExampleWeightColumnName,
                Booster = new GradientBooster.Options()
                {
                    SubsampleFraction = param.SubsampleFraction,
                    FeatureFraction   = param.FeatureFraction,
                    L1Regularization  = param.L1Regularization,
                    L2Regularization  = param.L2Regularization,
                },
                MaximumBinCountPerFeature = param.MaximumBinCountPerFeature,
            };

            return(context.Regression.Trainers.LightGbm(option));
        }
        // This example requires installation of additional NuGet
        // package for Microsoft.ML.LightGBM
        // at https://www.nuget.org/packages/Microsoft.ML.LightGbm/
        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 LightGbmRegressionTrainer.Options
            {
                LabelColumnName   = nameof(DataPoint.Label),
                FeatureColumnName = nameof(DataPoint.Features),
                // How many leaves a single tree should have.
                NumberOfLeaves = 4,
                // Each leaf contains at least this number of training data points.
                MinimumExampleCountPerLeaf = 6,
                // The step size per update. Using a large value might reduce the
                // training time but also increase the algorithm's numerical
                // stability.
                LearningRate = 0.001,
                Booster      = new Microsoft.ML.Trainers.LightGbm.GossBooster.Options()
                {
                    TopRate   = 0.3,
                    OtherRate = 0.2
                }
            };

            // Define the trainer.
            var pipeline =
                mlContext.Regression.Trainers.LightGbm(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.866
            //   Label: 0.155, Prediction: 0.171
            //   Label: 0.515, Prediction: 0.470
            //   Label: 0.566, Prediction: 0.476
            //   Label: 0.096, Prediction: 0.140

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

            PrintMetrics(metrics);

            // Expected output:
            //   Mean Absolute Error: 0.04
            //   Mean Squared Error: 0.00
            //   Root Mean Squared Error: 0.06
            //   RSquared: 0.97 (closer to 1 is better. The worest case is 0)
        }
 public ITrainerEstimator CreateInstance(MLContext mlContext, IEnumerable <SweepableParam> sweepParams,
                                         ColumnInformation columnInfo)
 {
     LightGbmRegressionTrainer.Options options = TrainerExtensionUtil.CreateLightGbmOptions <LightGbmRegressionTrainer.Options, float, RegressionPredictionTransformer <LightGbmRegressionModelParameters>, LightGbmRegressionModelParameters>(sweepParams, columnInfo);
     return(mlContext.Regression.Trainers.LightGbm(options));
 }