StandardTrainersCatalog.LbfgsPoissonRegression 方法

定義

多載

LbfgsPoissonRegression(RegressionCatalog+RegressionTrainers, LbfgsPoissonRegressionTrainer+Options)

LbfgsPoissonRegressionTrainer使用進階選項建立,以使用線性回歸模型預測目標。

LbfgsPoissonRegression(RegressionCatalog+RegressionTrainers, String, String, String, Single, Single, Single, Int32, Boolean)

建立 LbfgsPoissonRegressionTrainer ,其會使用線性回歸模型預測目標。

LbfgsPoissonRegression(RegressionCatalog+RegressionTrainers, LbfgsPoissonRegressionTrainer+Options)

LbfgsPoissonRegressionTrainer使用進階選項建立,以使用線性回歸模型預測目標。

public static Microsoft.ML.Trainers.LbfgsPoissonRegressionTrainer LbfgsPoissonRegression (this Microsoft.ML.RegressionCatalog.RegressionTrainers catalog, Microsoft.ML.Trainers.LbfgsPoissonRegressionTrainer.Options options);
static member LbfgsPoissonRegression : Microsoft.ML.RegressionCatalog.RegressionTrainers * Microsoft.ML.Trainers.LbfgsPoissonRegressionTrainer.Options -> Microsoft.ML.Trainers.LbfgsPoissonRegressionTrainer
<Extension()>
Public Function LbfgsPoissonRegression (catalog As RegressionCatalog.RegressionTrainers, options As LbfgsPoissonRegressionTrainer.Options) As LbfgsPoissonRegressionTrainer

參數

catalog
RegressionCatalog.RegressionTrainers

回歸目錄定型器物件。

options
LbfgsPoissonRegressionTrainer.Options

定型器選項。

傳回

範例

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Trainers;

namespace Samples.Dynamic.Trainers.Regression
{
    public static class LbfgsPoissonRegressionWithOptions
    {
        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 LbfgsPoissonRegressionTrainer.Options
            {
                LabelColumnName = nameof(DataPoint.Label),
                FeatureColumnName = nameof(DataPoint.Features),
                // Reduce optimization tolerance to speed up training at the cost of
                // accuracy.
                OptimizationTolerance = 1e-4f,
                // Decrease history size to speed up training at the cost of
                // accuracy.
                HistorySize = 30,
                // Specify scale for initial weights.
                InitialWeightsDiameter = 0.2f
            };

            // Define the trainer.
            var pipeline =
                mlContext.Regression.Trainers.LbfgsPoissonRegression(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: 1.110
            //   Label: 0.155, Prediction: 0.169
            //   Label: 0.515, Prediction: 0.400
            //   Label: 0.566, Prediction: 0.415
            //   Label: 0.096, Prediction: 0.169

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

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

        private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
            int seed = 0)
        {
            var random = new Random(seed);
            for (int i = 0; i < count; i++)
            {
                float label = (float)random.NextDouble();
                yield return new DataPoint
                {
                    Label = label,
                    // Create random features that are correlated with the label.
                    Features = Enumerable.Repeat(label, 50).Select(
                        x => x + (float)random.NextDouble()).ToArray()
                };
            }
        }

        // Example with label and 50 feature values. A data set is a collection of
        // such examples.
        private class DataPoint
        {
            public float Label { get; set; }
            [VectorType(50)]
            public float[] Features { get; set; }
        }

        // Class used to capture predictions.
        private class Prediction
        {
            // Original label.
            public float Label { get; set; }
            // Predicted score from the trainer.
            public float Score { get; set; }
        }

        // Print some evaluation metrics to regression problems.
        private static void PrintMetrics(RegressionMetrics metrics)
        {
            Console.WriteLine("Mean Absolute Error: " + metrics.MeanAbsoluteError);
            Console.WriteLine("Mean Squared Error: " + metrics.MeanSquaredError);
            Console.WriteLine(
                "Root Mean Squared Error: " + metrics.RootMeanSquaredError);

            Console.WriteLine("RSquared: " + metrics.RSquared);
        }
    }
}

適用於

LbfgsPoissonRegression(RegressionCatalog+RegressionTrainers, String, String, String, Single, Single, Single, Int32, Boolean)

建立 LbfgsPoissonRegressionTrainer ,其會使用線性回歸模型預測目標。

public static Microsoft.ML.Trainers.LbfgsPoissonRegressionTrainer LbfgsPoissonRegression (this Microsoft.ML.RegressionCatalog.RegressionTrainers catalog, string labelColumnName = "Label", string featureColumnName = "Features", string exampleWeightColumnName = default, float l1Regularization = 1, float l2Regularization = 1, float optimizationTolerance = 1E-07, int historySize = 20, bool enforceNonNegativity = false);
static member LbfgsPoissonRegression : Microsoft.ML.RegressionCatalog.RegressionTrainers * string * string * string * single * single * single * int * bool -> Microsoft.ML.Trainers.LbfgsPoissonRegressionTrainer
<Extension()>
Public Function LbfgsPoissonRegression (catalog As RegressionCatalog.RegressionTrainers, Optional labelColumnName As String = "Label", Optional featureColumnName As String = "Features", Optional exampleWeightColumnName As String = Nothing, Optional l1Regularization As Single = 1, Optional l2Regularization As Single = 1, Optional optimizationTolerance As Single = 1E-07, Optional historySize As Integer = 20, Optional enforceNonNegativity As Boolean = false) As LbfgsPoissonRegressionTrainer

參數

catalog
RegressionCatalog.RegressionTrainers

回歸目錄定型器物件。

labelColumnName
String

標籤資料行的名稱。 資料行資料必須是 Single

featureColumnName
String

功能資料行的名稱。 資料行資料必須是 的已知大小向量 Single

exampleWeightColumnName
String

範例加權資料行的名稱 (選擇性) 。

l1Regularization
Single

L1 正規化 超參數。 較高的值通常會導致更疏鬆的模型。

l2Regularization
Single

正規化的L2 權數。

optimizationTolerance
Single

優化工具聚合的臨界值。

historySize
Int32

要記住的先前反復專案數目,以估計 Hessian。 較低的值表示更快但較不精確的估計值。

enforceNonNegativity
Boolean

強制執行非負數權數。

傳回

範例

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Samples.Dynamic.Trainers.Regression
{
    public static class LbfgsPoissonRegression
    {
        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 the trainer.
            var pipeline = mlContext.Regression.Trainers.
                LbfgsPoissonRegression(
                labelColumnName: nameof(DataPoint.Label),
                featureColumnName: nameof(DataPoint.Features));

            // 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: 1.109
            //   Label: 0.155, Prediction: 0.171
            //   Label: 0.515, Prediction: 0.400
            //   Label: 0.566, Prediction: 0.417
            //   Label: 0.096, Prediction: 0.172

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

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

        private static IEnumerable<DataPoint> GenerateRandomDataPoints(int count,
            int seed = 0)
        {
            var random = new Random(seed);
            for (int i = 0; i < count; i++)
            {
                float label = (float)random.NextDouble();
                yield return new DataPoint
                {
                    Label = label,
                    // Create random features that are correlated with the label.
                    Features = Enumerable.Repeat(label, 50).Select(
                        x => x + (float)random.NextDouble()).ToArray()
                };
            }
        }

        // Example with label and 50 feature values. A data set is a collection of
        // such examples.
        private class DataPoint
        {
            public float Label { get; set; }
            [VectorType(50)]
            public float[] Features { get; set; }
        }

        // Class used to capture predictions.
        private class Prediction
        {
            // Original label.
            public float Label { get; set; }
            // Predicted score from the trainer.
            public float Score { get; set; }
        }

        // Print some evaluation metrics to regression problems.
        private static void PrintMetrics(RegressionMetrics metrics)
        {
            Console.WriteLine("Mean Absolute Error: " + metrics.MeanAbsoluteError);
            Console.WriteLine("Mean Squared Error: " + metrics.MeanSquaredError);
            Console.WriteLine(
                "Root Mean Squared Error: " + metrics.RootMeanSquaredError);

            Console.WriteLine("RSquared: " + metrics.RSquared);
        }
    }
}

適用於