FastTreeTweedie( this SweepableRegressionTrainers trainers, string labelColumnName = "Label", string featureColumnName = "Features", SweepableOption <FastTreeTweedieTrainer.Options> optionSweeper = null, FastTreeTweedieTrainer.Options defaultOption = null) { var context = trainers.Context; if (optionSweeper == null) { optionSweeper = FastTreeTweedieTrainerSweepableOptions.Default; } optionSweeper.SetDefaultOption(defaultOption); return(context.AutoML().CreateSweepableEstimator( (context, option) => { option.LabelColumnName = labelColumnName; option.FeatureColumnName = featureColumnName; return context.Regression.Trainers.FastTreeTweedie(option); }, optionSweeper, new string[] { labelColumnName, featureColumnName }, new string[] { Score }, nameof(FastTreeTweedieTrainer))); }
/// <summary> /// Predict a target using a decision tree regression model trained with the <see cref="FastTreeTweedieTrainer"/>. /// </summary> /// <param name="ctx">The <see cref="RegressionContext"/>.</param> /// <param name="options">Algorithm advanced settings.</param> public static FastTreeTweedieTrainer FastTreeTweedie(this RegressionContext.RegressionTrainers ctx, FastTreeTweedieTrainer.Options options) { Contracts.CheckValue(ctx, nameof(ctx)); var env = CatalogUtils.GetEnvironment(ctx); return(new FastTreeTweedieTrainer(env, options)); }
public override IEstimator <ITransformer> BuildFromOption(MLContext context, FastTreeOption param) { var option = new FastTreeTweedieTrainer.Options() { NumberOfLeaves = param.NumberOfLeaves, NumberOfTrees = param.NumberOfTrees, MinimumExampleCountPerLeaf = param.MinimumExampleCountPerLeaf, LearningRate = param.LearningRate, LabelColumnName = param.LabelColumnName, FeatureColumnName = param.FeatureColumnName, ExampleWeightColumnName = param.ExampleWeightColumnName, NumberOfThreads = AutoMlUtils.GetNumberOfThreadFromEnvrionment(), MaximumBinCountPerFeature = param.MaximumBinCountPerFeature, FeatureFraction = param.FeatureFraction, }; return(context.Regression.Trainers.FastTreeTweedie(option)); }
public void TestFastTreeTweedieFeaturizationInPipeline() { int dataPointCount = 200; var data = SamplesUtils.DatasetUtils.GenerateFloatLabelFloatFeatureVectorSamples(dataPointCount).ToList(); var dataView = ML.Data.LoadFromEnumerable(data); dataView = ML.Data.Cache(dataView); var trainerOptions = new FastTreeTweedieTrainer.Options { NumberOfThreads = 1, NumberOfTrees = 10, NumberOfLeaves = 4, MinimumExampleCountPerLeaf = 10, FeatureColumnName = "Features", LabelColumnName = "Label" }; var options = new FastTreeTweedieFeaturizationEstimator.Options() { InputColumnName = "Features", TreesColumnName = "Trees", LeavesColumnName = "Leaves", PathsColumnName = "Paths", TrainerOptions = trainerOptions }; var pipeline = ML.Transforms.FeaturizeByFastTreeTweedie(options) .Append(ML.Transforms.Concatenate("CombinedFeatures", "Features", "Trees", "Leaves", "Paths")) .Append(ML.Regression.Trainers.Sdca("Label", "CombinedFeatures")); var model = pipeline.Fit(dataView); var prediction = model.Transform(dataView); var metrics = ML.Regression.Evaluate(prediction); Assert.True(metrics.MeanAbsoluteError < 0.25); Assert.True(metrics.MeanSquaredError < 0.1); }
// 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 FastTreeTweedieTrainer.Options { // Use L2Norm for early stopping. EarlyStoppingMetric = EarlyStoppingMetric.L2Norm, // Create a simpler model by penalizing usage of new features. FeatureFirstUsePenalty = 0.1, // Reduce the number of trees to 50. NumberOfTrees = 50 }; // Define the trainer. var pipeline = mlContext.Regression.Trainers.FastTreeTweedie(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.954 // Label: 0.155, Prediction: 0.103 // Label: 0.515, Prediction: 0.450 // Label: 0.566, Prediction: 0.515 // Label: 0.096, Prediction: 0.078 // Evaluate the overall metrics var metrics = mlContext.Regression.Evaluate(transformedTestData); SamplesUtils.ConsoleUtils.PrintMetrics(metrics); // Expected output: // Mean Absolute Error: 0.05 // Mean Squared Error: 0.00 // Root Mean Squared Error: 0.07 // RSquared: 0.95 }
// 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 data points. var dataPoints = GenerateRandomDataPoints(100).ToList(); // Convert the list of data points to an IDataView object, which is // consumable by ML.NET API. var dataView = mlContext.Data.LoadFromEnumerable(dataPoints); // ML.NET doesn't cache data set by default. Therefore, if one reads a // data set from a file and accesses it many times, it can be slow due // to expensive featurization and disk operations. When the considered // data can fit into memory, a solution is to cache the data in memory. // Caching is especially helpful when working with iterative algorithms // which needs many data passes. dataView = mlContext.Data.Cache(dataView); // Define input and output columns of tree-based featurizer. string labelColumnName = nameof(DataPoint.Label); string featureColumnName = nameof(DataPoint.Features); string treesColumnName = nameof(TransformedDataPoint.Trees); string leavesColumnName = nameof(TransformedDataPoint.Leaves); string pathsColumnName = nameof(TransformedDataPoint.Paths); // Define the configuration of the trainer used to train a tree-based // model. var trainerOptions = new FastTreeTweedieTrainer.Options { // Only use 80% of features to reduce over-fitting. FeatureFraction = 0.8, // Create a simpler model by penalizing usage of new features. FeatureFirstUsePenalty = 0.1, // Reduce the number of trees to 3. NumberOfTrees = 3, // Number of leaves per tree. NumberOfLeaves = 6, LabelColumnName = labelColumnName, FeatureColumnName = featureColumnName }; // Define the tree-based featurizer's configuration. var options = new FastTreeTweedieFeaturizationEstimator.Options { InputColumnName = featureColumnName, TreesColumnName = treesColumnName, LeavesColumnName = leavesColumnName, PathsColumnName = pathsColumnName, TrainerOptions = trainerOptions }; // Define the featurizer. var pipeline = mlContext.Transforms.FeaturizeByFastTreeTweedie( options); // Train the model. var model = pipeline.Fit(dataView); // Create testing data. Use different random seed to make it different // from training data. var transformed = model.Transform(dataView); // Convert IDataView object to a list. Each element in the resulted list // corresponds to a row in the IDataView. var transformedDataPoints = mlContext.Data.CreateEnumerable < TransformedDataPoint>(transformed, false).ToList(); // Print out the transformation of the first 3 data points. for (int i = 0; i < 3; ++i) { var dataPoint = dataPoints[i]; var transformedDataPoint = transformedDataPoints[i]; Console.WriteLine("The original feature vector [" + String.Join(",", dataPoint.Features) + "] is transformed to three different " + "tree-based feature vectors:"); Console.WriteLine(" Trees' output values: [" + String.Join(",", transformedDataPoint.Trees) + "]."); Console.WriteLine(" Leave IDs' 0-1 representation: [" + String .Join(",", transformedDataPoint.Leaves) + "]."); Console.WriteLine(" Paths IDs' 0-1 representation: [" + String .Join(",", transformedDataPoint.Paths) + "]."); } // Expected output: // The original feature vector [1.543569,1.494266,1.284405] is // transformed to three different tree-based feature vectors: // Trees' output values: [-0.05652997,-0.02312196,-0.01179363]. // Leave IDs' 0-1 representation: [0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0]. // Paths IDs' 0-1 representation: [1,0,0,0,0,1,1,0,1,0,1,1,0,0,0]. // The original feature vector [0.764918,1.11206,0.648211] is // transformed to three different tree-based feature vectors: // Trees' output values: [-0.1933938,-0.1042738,-0.2312837]. // Leave IDs' 0-1 representation: [0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0]. // Paths IDs' 0-1 representation: [1,1,1,0,0,1,1,0,0,0,1,0,0,0,0]. // The original feature vector [1.251254,1.269456,1.444864] is // transformed to three different tree-based feature vectors: // Trees' output values: [-0.05652997,-0.06082304,-0.04528879]. // Leave IDs' 0-1 representation: [0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0]. // Paths IDs' 0-1 representation: [1,0,0,0,0,1,1,0,1,0,1,1,1,0,1]. }
// 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 FastTreeTweedieTrainer.Options { LabelColumnName = nameof(DataPoint.Label), FeatureColumnName = nameof(DataPoint.Features), // Use L2Norm for early stopping. EarlyStoppingMetric = Microsoft.ML.Trainers.FastTree.EarlyStoppingMetric.L2Norm, // Create a simpler model by penalizing usage of new features. FeatureFirstUsePenalty = 0.1, // Reduce the number of trees to 50. NumberOfTrees = 50 }; // Define the trainer. var pipeline = mlContext.Regression.Trainers.FastTreeTweedie(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.954 // Label: 0.155, Prediction: 0.103 // Label: 0.515, Prediction: 0.450 // Label: 0.566, Prediction: 0.515 // Label: 0.096, Prediction: 0.078 // 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.05 // RSquared: 0.98 (closer to 1 is better. The worst case is 0) }