示例#1
0
 /// <summary>
 /// Create an<see cref= "SymbolicSgdLogisticRegressionBinaryTrainer" />, which predicts a target using a linear binary classification model trained over boolean label data.
 /// Stochastic gradient descent (SGD) is an iterative algorithm that optimizes a differentiable objective function.
 /// The <see cref="SymbolicSgdLogisticRegressionBinaryTrainer"/> parallelizes SGD using <a href="https://www.microsoft.com/en-us/research/project/project-parade/#!symbolic-execution">symbolic execution</a>.
 /// </summary>
 /// <param name="catalog">The <see cref="BinaryClassificationCatalog"/>.</param>
 /// <param name="options">Algorithm advanced options. See <see cref="SymbolicSgdLogisticRegressionBinaryTrainer.Options"/>.</param>
 /// <example>
 /// <format type="text/markdown">
 /// <![CDATA[
 /// [!code-csharp[SymbolicSgdLogisticRegression](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/SymbolicSgdLogisticRegressionWithOptions.cs)]
 /// ]]>
 /// </format>
 /// </example>
 public static SymbolicSgdLogisticRegressionBinaryTrainer SymbolicSgdLogisticRegression(
     this BinaryClassificationCatalog.BinaryClassificationTrainers catalog,
     SymbolicSgdLogisticRegressionBinaryTrainer.Options options)
 {
     Contracts.CheckValue(catalog, nameof(catalog));
     Contracts.CheckValue(options, nameof(options));
     var env = CatalogUtils.GetEnvironment(catalog);
     return new SymbolicSgdLogisticRegressionBinaryTrainer(env, options);
 }
        /// <summary>
        /// Create an <see cref="SymbolicSgdLogisticRegressionBinaryTrainer"/> with advanced options, which predicts a target using a linear binary classification model trained over boolean label data.
        /// Stochastic gradient descent (SGD) is an iterative algorithm that optimizes a differentiable objective function.
        /// The <see cref="SymbolicSgdLogisticRegressionBinaryTrainer"/> parallelizes SGD using <a href="https://www.microsoft.com/en-us/research/project/project-parade/#!symbolic-execution">symbolic execution</a>.
        /// </summary>
        /// <param name="catalog">The <see cref="BinaryClassificationCatalog"/>.</param>
        /// <param name="labelColumnName">The name of the label column. The column data must be <see cref="System.Boolean"/>.</param>
        /// <param name="featureColumnName">The name of the feature column. The column data must be a known-sized vector of <see cref="System.Single"/>.</param>
        /// <param name="numberOfIterations">Number of training iterations.</param>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        /// [!code-csharp[SymbolicSgdLogisticRegression](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/SymbolicSgdLogisticRegression.cs)]
        /// ]]>
        /// </format>
        /// </example>
        public static SymbolicSgdLogisticRegressionBinaryTrainer SymbolicSgdLogisticRegression(this BinaryClassificationCatalog.BinaryClassificationTrainers catalog,
                                                                                               string labelColumnName   = DefaultColumnNames.Label,
                                                                                               string featureColumnName = DefaultColumnNames.Features,
                                                                                               int numberOfIterations   = SymbolicSgdLogisticRegressionBinaryTrainer.Defaults.NumberOfIterations)
        {
            Contracts.CheckValue(catalog, nameof(catalog));
            var env = CatalogUtils.GetEnvironment(catalog);

            var options = new SymbolicSgdLogisticRegressionBinaryTrainer.Options
            {
                LabelColumnName   = labelColumnName,
                FeatureColumnName = featureColumnName,
            };

            return(new SymbolicSgdLogisticRegressionBinaryTrainer(env, options));
        }
        // This example requires installation of additional NuGet package
        // <a href="https://www.nuget.org/packages/Microsoft.ML.Mkl.Components/">Microsoft.ML.Mkl.Components</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(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 SymbolicSgdLogisticRegressionBinaryTrainer.Options()
            {
                LearningRate       = 0.2f,
                NumberOfIterations = 10,
                NumberOfThreads    = 1,
            };

            // Define the trainer.
            var pipeline = mlContext.BinaryClassification.Trainers.SymbolicSgdLogisticRegression(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(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();

            // Print 5 predictions.
            foreach (var p in predictions.Take(5))
            {
                Console.WriteLine($"Label: {p.Label}, Prediction: {p.PredictedLabel}");
            }

            // Expected output:
            //   Label: True, Prediction: False
            //   Label: False, Prediction: False
            //   Label: True, Prediction: True
            //   Label: True, Prediction: True
            //   Label: False, Prediction: False

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

            PrintMetrics(metrics);

            // Expected output:
            //   Accuracy: 0.72
            //   AUC: 0.81
            //   F1 Score: 0.66
            //   Negative Precision: 0.68
            //   Negative Recall: 0.87
            //   Positive Precision: 0.80
            //   Positive Recall: 0.56
        }