AveragedPerceptron(
            this SweepableBinaryClassificationTrainers trainer,
            string labelColumnName   = "Label",
            string featureColumnName = "Features",
            SweepableOption <AveragedPerceptronTrainer.Options> optionBuilder = null,
            AveragedPerceptronTrainer.Options defaultOption = null)
        {
            var context = trainer.Context;

            if (optionBuilder == null)
            {
                optionBuilder = AveragedPerceptronBinaryTrainerSweepableOptions.Default;
            }

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

                return context.BinaryClassification.Trainers.AveragedPerceptron(option);
            },
                       optionBuilder,
                       new string[] { labelColumnName, featureColumnName },
                       new string[] { PredictedLabel },
                       nameof(AveragedPerceptronTrainer)));
        }
Пример #2
0
        /// <summary>
        /// Predict a target using a linear binary classification model trained with <see cref="AveragedPerceptronTrainer"/> and advanced options.
        /// </summary>
        /// <param name="catalog">The binary classification catalog trainer object.</param>
        /// <param name="options">Trainer options.</param>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        /// [!code-csharp[AveragedPerceptron](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/Trainers/BinaryClassification/AveragedPerceptronWithOptions.cs)]
        /// ]]>
        /// </format>
        /// </example>
        public static AveragedPerceptronTrainer AveragedPerceptron(
            this BinaryClassificationCatalog.BinaryClassificationTrainers catalog, AveragedPerceptronTrainer.Options options)
        {
            Contracts.CheckValue(catalog, nameof(catalog));
            Contracts.CheckValue(options, nameof(options));

            var env = CatalogUtils.GetEnvironment(catalog);

            return(new AveragedPerceptronTrainer(env, options));
        }
Пример #3
0
        // In this examples we will use the adult income dataset. The goal is to predict
        // if a person's income is above $50K or not, based on demographic information about that person.
        // For more details about this dataset, please see https://archive.ics.uci.edu/ml/datasets/adult.
        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);

            // Download and featurize the dataset.
            var data = SamplesUtils.DatasetUtils.LoadFeaturizedAdultDataset(mlContext);

            // Leave out 10% of data for testing.
            var trainTestData = mlContext.Data.TrainTestSplit(data, testFraction: 0.1);

            // Define the trainer options.
            var options = new AveragedPerceptronTrainer.Options()
            {
                LossFunction       = new SmoothedHingeLoss(),
                LearningRate       = 0.1f,
                LazyUpdate         = false,
                RecencyGain        = 0.1f,
                NumberOfIterations = 10
            };

            // Create data training pipeline.
            var pipeline = mlContext.BinaryClassification.Trainers.AveragedPerceptron(options);

            // Fit this pipeline to the training data.
            var model = pipeline.Fit(trainTestData.TrainSet);

            // Evaluate how the model is doing on the test data.
            var dataWithPredictions = model.Transform(trainTestData.TestSet);
            var metrics             = mlContext.BinaryClassification.EvaluateNonCalibrated(dataWithPredictions);

            SamplesUtils.ConsoleUtils.PrintMetrics(metrics);

            // Expected output:
            //  Accuracy: 0.86
            //  AUC: 0.90
            //  F1 Score: 0.66
            //  Negative Precision: 0.89
            //  Negative Recall: 0.93
            //  Positive Precision: 0.72
            //  Positive Recall: 0.61
        }
 public ITrainerEstimator CreateInstance(MLContext mlContext, IEnumerable <SweepableParam> sweepParams,
                                         ColumnInformation columnInfo, IDataView validationSet)
 {
     AveragedPerceptronTrainer.Options options = null;
     if (sweepParams == null || !sweepParams.Any())
     {
         options = new AveragedPerceptronTrainer.Options();
         options.NumberOfIterations = DefaultNumIterations;
         options.LabelColumnName    = columnInfo.LabelColumnName;
     }
     else
     {
         options = TrainerExtensionUtil.CreateOptions <AveragedPerceptronTrainer.Options>(sweepParams, columnInfo.LabelColumnName);
         if (!sweepParams.Any(p => p.Name == "NumberOfIterations"))
         {
             options.NumberOfIterations = DefaultNumIterations;
         }
     }
     return(mlContext.BinaryClassification.Trainers.AveragedPerceptron(options));
 }
Пример #5
0
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer, and a custom loss.
        /// </summary>
        /// <param name="catalog">The binary classification catalog trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="lossFunction">The custom loss.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="options">Advanced arguments to the algorithm.</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, as well as the calibrator on top of that model. 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 set of output columns including in order the predicted binary classification score (which will range
        /// from negative to positive infinity), and the predicted label.</returns>
        /// <seealso cref="AveragedPerceptronTrainer"/>.
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[AveragedPerceptron](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Static/AveragedPerceptronBinaryClassification.cs)]
        /// ]]></format>
        /// </example>
        public static (Scalar <float> score, Scalar <bool> predictedLabel) AveragedPerceptron(
            this BinaryClassificationCatalog.BinaryClassificationTrainers catalog,
            Scalar <bool> label,
            Vector <float> features,
            Scalar <float> weights,
            IClassificationLoss lossFunction,
            AveragedPerceptronTrainer.Options options,
            Action <LinearBinaryModelParameters> onFit = null
            )
        {
            Contracts.CheckValue(label, nameof(label));
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckValueOrNull(options);
            Contracts.CheckValueOrNull(onFit);

            bool hasProbs = lossFunction is LogLoss;

            var rec = new TrainerEstimatorReconciler.BinaryClassifierNoCalibration(
                (env, labelName, featuresName, weightsName) =>
            {
                options.LabelColumn    = labelName;
                options.FeatureColumn  = featuresName;
                options.InitialWeights = weightsName;

                var trainer = new AveragedPerceptronTrainer(env, options);

                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                else
                {
                    return(trainer);
                }
            }, label, features, weights, hasProbs);

            return(rec.Output);
        }
Пример #6
0
        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 AveragedPerceptronTrainer.Options
            {
                LossFunction       = new SmoothedHingeLoss(),
                LearningRate       = 0.1f,
                LazyUpdate         = false,
                RecencyGain        = 0.1f,
                NumberOfIterations = 10
            };

            // Define the trainer.
            var pipeline = mlContext.BinaryClassification.Trainers.AveragedPerceptron(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: True
            //   Label: False, Prediction: False
            //   Label: True, Prediction: True
            //   Label: True, Prediction: True
            //   Label: False, Prediction: False

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

            PrintMetrics(metrics);

            // Expected output:
            //   Accuracy: 0.89
            //   AUC: 0.96
            //   F1 Score: 0.88
            //   Negative Precision: 0.87
            //   Negative Recall: 0.92
            //   Positive Precision: 0.91
            //   Positive Recall: 0.85
        }