/// <summary>
        ///  Predict a target using a linear binary classification model trained with the <see cref="SymSgdClassificationTrainer"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
        /// <param name="labelColumn">The labelColumn column.</param>
        /// <param name="featureColumn">The features column.</param>
        /// <param name="advancedSettings">Algorithm advanced settings.</param>
        public static SymSgdClassificationTrainer SymbolicStochasticGradientDescent(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                                    string labelColumn   = DefaultColumnNames.Label,
                                                                                    string featureColumn = DefaultColumnNames.Features,
                                                                                    Action <SymSgdClassificationTrainer.Arguments> advancedSettings = null)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            return(new SymSgdClassificationTrainer(env, labelColumn, featureColumn, advancedSettings));
        }
Esempio n. 2
0
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer.
        /// </summary>
        /// <param name="ctx">The binary classification context trainer object.</param>
        /// <param name="options">Advanced arguments to the algorithm.</param>
        public static AveragedPerceptronTrainer AveragedPerceptron(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx, AveragedPerceptronTrainer.Options options)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            Contracts.CheckValue(options, nameof(options));

            var env = CatalogUtils.GetEnvironment(ctx);

            return(new AveragedPerceptronTrainer(env, options));
        }
Esempio n. 3
0
        /// <summary>
        ///  Predict a target using a linear binary classification model trained with the <see cref="SymSgdClassificationTrainer"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
        /// <param name="options">Algorithm advanced options. See <see cref="SymSgdClassificationTrainer.Options"/>.</param>
        public static SymSgdClassificationTrainer SymbolicStochasticGradientDescent(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx,
            SymSgdClassificationTrainer.Options options)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            Contracts.CheckValue(options, nameof(options));
            var env = CatalogUtils.GetEnvironment(ctx);

            return(new SymSgdClassificationTrainer(env, options));
        }
Esempio n. 4
0
        /// <summary>
        /// Predict a target using a decision tree binary classification model trained with the <see cref="FastTreeBinaryClassificationTrainer"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
        /// <param name="options">Algorithm advanced settings.</param>
        public static FastTreeBinaryClassificationTrainer FastTree(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                   FastTreeBinaryClassificationTrainer.Options options)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            Contracts.CheckValue(options, nameof(options));

            var env = CatalogUtils.GetEnvironment(ctx);

            return(new FastTreeBinaryClassificationTrainer(env, options));
        }
 /// <summary>
 /// Predict a target using a linear binary classification model trained with the <see cref="LinearSvm"/> trainer.
 /// </summary>
 /// <remarks>
 /// <para>
 /// The idea behind support vector machines, is to map instances into a high dimensional space
 /// in which the two classes are linearly separable, i.e., there exists a hyperplane such that all the positive examples are on one side of it,
 /// and all the negative examples are on the other.
 /// </para>
 /// <para>
 /// After this mapping, quadratic programming is used to find the separating hyperplane that maximizes the
 /// margin, i.e., the minimal distance between it and the instances.
 /// </para>
 /// </remarks>
 /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
 /// <param name="labelColumn">The name of the label column. </param>
 /// <param name="featureColumn">The name of the feature column.</param>
 /// <param name="weightsColumn">The optional name of the weights column.</param>
 /// <param name="numIterations">The number of training iteraitons.</param>
 /// <param name="advancedSettings">A delegate to supply more advanced arguments to the algorithm.</param>
 public static LinearSvm LinearSupportVectorMachines(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                     string labelColumn   = DefaultColumnNames.Label,
                                                     string featureColumn = DefaultColumnNames.Features,
                                                     string weightsColumn = null,
                                                     int numIterations    = OnlineLinearArguments.OnlineDefaultArgs.NumIterations,
                                                     Action <LinearSvm.Arguments> advancedSettings = null)
 {
     Contracts.CheckValue(ctx, nameof(ctx));
     return(new LinearSvm(CatalogUtils.GetEnvironment(ctx), labelColumn, featureColumn, weightsColumn, numIterations, advancedSettings));
 }
Esempio n. 6
0
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the SDCA trainer.
        /// </summary>
        /// <param name="ctx">The binary classification context trainer object.</param>
        /// <param name="options">Advanced arguments to the algorithm.</param>
        public static SdcaBinaryTrainer StochasticDualCoordinateAscent(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx,
            SdcaBinaryTrainer.Options options)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            Contracts.CheckValue(options, nameof(options));

            var env = CatalogUtils.GetEnvironment(ctx);

            return(new SdcaBinaryTrainer(env, options));
        }
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the SDCA trainer, and a custom loss.
        /// Note that because we cannot be sure that all loss functions will produce naturally calibrated outputs, setting
        /// a custom loss function will not produce a calibrated probability column.
        /// </summary>
        /// <param name="ctx">The binary classification context trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="loss">The custom loss.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="l2Const">The L2 regularization hyperparameter.</param>
        /// <param name="l1Threshold">The L1 regularization hyperparameter. Higher values will tend to lead to more sparse model.</param>
        /// <param name="maxIterations">The maximum number of passes to perform over the data.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}.Fit(DataView{TTupleInShape})"/> method is called on the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, 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="Sdca(BinaryClassificationContext.BinaryClassificationTrainers, Scalar{bool}, Vector{float}, Scalar{float}, float?, float?, int?, Action{LinearBinaryPredictor, ParameterMixingCalibratedPredictor})"/>
        public static (Scalar <float> score, Scalar <bool> predictedLabel) Sdca(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx,
            Scalar <bool> label, Vector <float> features,
            ISupportSdcaClassificationLoss loss,
            Scalar <float> weights = null,
            float?l2Const          = null,
            float?l1Threshold      = null,
            int?maxIterations      = null,
            Action <LinearBinaryPredictor> onFit = null
            )
        {
            Contracts.CheckValue(label, nameof(label));
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValue(loss, nameof(loss));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckParam(!(l2Const < 0), nameof(l2Const), "Must not be negative, if specified.");
            Contracts.CheckParam(!(l1Threshold < 0), nameof(l1Threshold), "Must not be negative, if specified.");
            Contracts.CheckParam(!(maxIterations < 1), nameof(maxIterations), "Must be positive if specified");
            Contracts.CheckValueOrNull(onFit);

            bool hasProbs = loss is LogLoss;

            var args = new LinearClassificationTrainer.Arguments()
            {
                L2Const       = l2Const,
                L1Threshold   = l1Threshold,
                MaxIterations = maxIterations,
                LossFunction  = new TrivialSdcaClassificationLossFactory(loss)
            };

            var rec = new TrainerEstimatorReconciler.BinaryClassifierNoCalibration(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new LinearClassificationTrainer(env, args, featuresName, labelName, weightsName);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans =>
                    {
                        var model = trans.Model;
                        if (model is ParameterMixingCalibratedPredictor cali)
                        {
                            onFit((LinearBinaryPredictor)cali.SubPredictor);
                        }
                        else
                        {
                            onFit((LinearBinaryPredictor)model);
                        }
                    }));
                }
                return(trainer);
            }, label, features, weights, hasProbs);

            return(rec.Output);
        }
Esempio n. 8
0
        /// <summary>
        /// Predict a target using a decision tree regression model trained with the <see cref="FastTreeRegressionTrainer"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
        /// <param name="labelColumn">The labelColumn column.</param>
        /// <param name="featureColumn">The featureColumn column.</param>
        /// <param name="weights">The optional weights column.</param>
        /// <param name="minDatapointsInLeaves">The minimal number of datapoints allowed in a leaf of the tree, out of the subsampled data.</param>
        /// <param name="learningRate">The learning rate.</param>
        /// <param name="advancedSettings">Algorithm advanced settings.</param>
        public static BinaryClassificationGamTrainer GeneralizedAdditiveModels(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                               string labelColumn        = DefaultColumnNames.Label,
                                                                               string featureColumn      = DefaultColumnNames.Features,
                                                                               string weights            = null,
                                                                               int minDatapointsInLeaves = Defaults.MinDocumentsInLeaves,
                                                                               double learningRate       = Defaults.LearningRates,
                                                                               Action <BinaryClassificationGamTrainer.Arguments> advancedSettings = null)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            return(new BinaryClassificationGamTrainer(env, labelColumn, featureColumn, weights, minDatapointsInLeaves, learningRate, advancedSettings));
        }
Esempio n. 9
0
 /// <summary>
 /// Predict a target using a decision tree binary classification model trained with the <see cref="FastTreeBinaryClassificationTrainer"/>.
 /// </summary>
 /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
 /// <param name="label">The label column.</param>
 /// <param name="features">The features column.</param>
 /// <param name="weights">The optional weights column.</param>
 /// <param name="numTrees">Total number of decision trees to create in the ensemble.</param>
 /// <param name="numLeaves">The maximum number of leaves per decision tree.</param>
 /// <param name="minDatapointsInLeafs">The minimal number of datapoints allowed in a leaf of the tree, out of the subsampled data.</param>
 /// <param name="learningRate">The learning rate.</param>
 /// <param name="advancedSettings">Algorithm advanced settings.</param>
 public static FastTreeBinaryClassificationTrainer FastTree(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
     string label = DefaultColumnNames.Label,
     string features = DefaultColumnNames.Features,
     string weights = null,
     int numLeaves = Defaults.NumLeaves,
     int numTrees = Defaults.NumTrees,
     int minDatapointsInLeafs = Defaults.MinDocumentsInLeafs,
     double learningRate = Defaults.LearningRates,
     Action<FastTreeBinaryClassificationTrainer.Arguments> advancedSettings = null)
 {
     Contracts.CheckValue(ctx, nameof(ctx));
     var env = CatalogUtils.GetEnvironment(ctx);
     return new FastTreeBinaryClassificationTrainer(env, label, features, weights, numLeaves, numTrees, minDatapointsInLeafs, learningRate, advancedSettings);
 }
Esempio n. 10
0
        /// <summary>
        /// Predict a target using a decision tree regression model trained with the <see cref="FastForestClassification"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
        /// <param name="labelColumn">The labelColumn column.</param>
        /// <param name="featureColumn">The featureColumn column.</param>
        /// <param name="weights">The optional weights column.</param>
        /// <param name="numTrees">Total number of decision trees to create in the ensemble.</param>
        /// <param name="numLeaves">The maximum number of leaves per decision tree.</param>
        /// <param name="minDatapointsInLeaves">The minimal number of datapoints allowed in a leaf of the tree, out of the subsampled data.</param>
        /// <param name="learningRate">The learning rate.</param>
        public static FastForestClassification FastForest(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                          string labelColumn        = DefaultColumnNames.Label,
                                                          string featureColumn      = DefaultColumnNames.Features,
                                                          string weights            = null,
                                                          int numLeaves             = Defaults.NumLeaves,
                                                          int numTrees              = Defaults.NumTrees,
                                                          int minDatapointsInLeaves = Defaults.MinDocumentsInLeaves,
                                                          double learningRate       = Defaults.LearningRates)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            return(new FastForestClassification(env, labelColumn, featureColumn, weights, numLeaves, numTrees, minDatapointsInLeaves, learningRate));
        }
Esempio n. 11
0
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer, and a custom loss.
        /// </summary>
        /// <param name="ctx">The binary classification context 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="learningRate">The learning Rate.</param>
        /// <param name="decreaseLearningRate">Decrease learning rate as iterations progress.</param>
        /// <param name="l2RegularizerWeight">L2 regularization weight.</param>
        /// <param name="numIterations">Number of training iterations through the data.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}.Fit(DataView{TTupleInShape})"/> method is called on the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, 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"/>.
        public static (Scalar <float> score, Scalar <bool> predictedLabel) AveragedPerceptron(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx,
            IClassificationLoss lossFunction,
            Scalar <bool> label, Vector <float> features, Scalar <float> weights = null,
            float learningRate                   = AveragedLinearArguments.AveragedDefaultArgs.LearningRate,
            bool decreaseLearningRate            = AveragedLinearArguments.AveragedDefaultArgs.DecreaseLearningRate,
            float l2RegularizerWeight            = AveragedLinearArguments.AveragedDefaultArgs.L2RegularizerWeight,
            int numIterations                    = AveragedLinearArguments.AveragedDefaultArgs.NumIterations,
            Action <LinearBinaryPredictor> onFit = null
            )
        {
            OnlineLinearStaticUtils.CheckUserParams(label, features, weights, learningRate, l2RegularizerWeight, numIterations, onFit);

            bool hasProbs = lossFunction is HingeLoss;

            var args = new AveragedPerceptronTrainer.Arguments()
            {
                LearningRate         = learningRate,
                DecreaseLearningRate = decreaseLearningRate,
                L2RegularizerWeight  = l2RegularizerWeight,
                NumIterations        = numIterations
            };

            if (lossFunction != null)
            {
                args.LossFunction = new TrivialClassificationLossFactory(lossFunction);
            }

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

                var trainer = new AveragedPerceptronTrainer(env, args);

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

            return(rec.Output);
        }
        /// <summary>
        /// Predict a target using a decision tree binary classification model trained with the <see cref="LightGbmBinaryTrainer"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
        /// <param name="labelColumn">The labelColumn column.</param>
        /// <param name="featureColumn">The features column.</param>
        /// <param name="weights">The weights column.</param>
        /// <param name="numLeaves">The number of leaves to use.</param>
        /// <param name="numBoostRound">Number of iterations.</param>
        /// <param name="minDataPerLeaf">The minimal number of documents allowed in a leaf of the tree, out of the subsampled data.</param>
        /// <param name="learningRate">The learning rate.</param>
        /// <param name="advancedSettings">A delegate to set more settings.
        /// The settings here will override the ones provided in the direct signature,
        /// if both are present and have different values.
        /// The columns names, however need to be provided directly, not through the <paramref name="advancedSettings"/>.</param>
        public static LightGbmBinaryTrainer LightGbm(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                     string labelColumn   = DefaultColumnNames.Label,
                                                     string featureColumn = DefaultColumnNames.Features,
                                                     string weights       = null,
                                                     int?numLeaves        = null,
                                                     int?minDataPerLeaf   = null,
                                                     double?learningRate  = null,
                                                     int numBoostRound    = LightGbmArguments.Defaults.NumBoostRound,
                                                     Action <LightGbmArguments> advancedSettings = null)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            return(new LightGbmBinaryTrainer(env, labelColumn, featureColumn, weights, numLeaves, minDataPerLeaf, learningRate, numBoostRound, advancedSettings));
        }
Esempio n. 13
0
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the SDCA trainer.
        /// </summary>
        /// <param name="ctx">The binary classification context trainer object.</param>
        /// <param name="labelColumn">The labelColumn, or dependent variable.</param>
        /// <param name="featureColumn">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="loss">The custom loss. Defaults to log-loss if not specified.</param>
        /// <param name="l2Const">The L2 regularization hyperparameter.</param>
        /// <param name="l1Threshold">The L1 regularization hyperparameter. Higher values will tend to lead to more sparse model.</param>
        /// <param name="maxIterations">The maximum number of passes to perform over the data.</param>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[SDCA](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/SDCA.cs)]
        /// ]]></format>
        /// </example>
        public static SdcaBinaryTrainer StochasticDualCoordinateAscent(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx,
            string labelColumn   = DefaultColumnNames.Label,
            string featureColumn = DefaultColumnNames.Features,
            string weights       = null,
            ISupportSdcaClassificationLoss loss = null,
            float?l2Const     = null,
            float?l1Threshold = null,
            int?maxIterations = null)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            return(new SdcaBinaryTrainer(env, labelColumn, featureColumn, weights, loss, l2Const, l1Threshold, maxIterations));
        }
Esempio n. 14
0
        /// <summary>
        ///  Predict a target using a linear binary classification model trained with the <see cref="Microsoft.ML.Learners.LogisticRegression"/> trainer.
        /// </summary>
        /// <param name="ctx">The binary classificaiton context trainer object.</param>
        /// <param name="labelColumn">The label column name, or dependent variable.</param>
        /// <param name="featureColumn">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="enforceNoNegativity">Enforce non-negative weights.</param>
        /// <param name="l1Weight">Weight of L1 regularization term.</param>
        /// <param name="l2Weight">Weight of L2 regularization term.</param>
        /// <param name="memorySize">Memory size for <see cref="Microsoft.ML.Learners.LogisticRegression"/>. Low=faster, less accurate.</param>
        /// <param name="optimizationTolerance">Threshold for optimizer convergence.</param>
        public static LogisticRegression LogisticRegression(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                            string labelColumn          = DefaultColumnNames.Label,
                                                            string featureColumn        = DefaultColumnNames.Features,
                                                            string weights              = null,
                                                            float l1Weight              = LROptions.Defaults.L1Weight,
                                                            float l2Weight              = LROptions.Defaults.L2Weight,
                                                            float optimizationTolerance = LROptions.Defaults.OptTol,
                                                            int memorySize              = LROptions.Defaults.MemorySize,
                                                            bool enforceNoNegativity    = LROptions.Defaults.EnforceNonNegativity)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            return(new LogisticRegression(env, labelColumn, featureColumn, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enforceNoNegativity));
        }
Esempio n. 15
0
        /// <summary>
        ///  Predict a target using a linear binary classification model trained with the <see cref="SymSgdClassificationTrainer"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
        /// <param name="labelColumn">The label column.</param>
        /// <param name="featureColumn">The features column.</param>
        public static SymSgdClassificationTrainer SymbolicStochasticGradientDescent(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx,
            string labelColumn   = DefaultColumnNames.Label,
            string featureColumn = DefaultColumnNames.Features)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            var options = new SymSgdClassificationTrainer.Options
            {
                LabelColumn   = labelColumn,
                FeatureColumn = featureColumn,
            };

            return(new SymSgdClassificationTrainer(env, options));
        }
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer, and a custom loss.
        /// </summary>
        /// <param name="ctx">The binary classification context trainer object.</param>
        /// <param name="labelColumn">The name of the label column, or dependent variable.</param>
        /// <param name="featureColumn">The features, or independent variables.</param>
        /// <param name="lossFunction">The custom loss.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="learningRate">The learning Rate.</param>
        /// <param name="decreaseLearningRate">Decrease learning rate as iterations progress.</param>
        /// <param name="l2RegularizerWeight">L2 regularization weight.</param>
        /// <param name="numIterations">Number of training iterations through the data.</param>
        /// <param name="advancedSettings">A delegate to supply more advanced arguments to the algorithm.</param>
        public static AveragedPerceptronTrainer AveragedPerceptron(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx,
            string labelColumn               = DefaultColumnNames.Label,
            string featureColumn             = DefaultColumnNames.Features,
            string weights                   = null,
            IClassificationLoss lossFunction = null,
            float learningRate               = AveragedLinearArguments.AveragedDefaultArgs.LearningRate,
            bool decreaseLearningRate        = AveragedLinearArguments.AveragedDefaultArgs.DecreaseLearningRate,
            float l2RegularizerWeight        = AveragedLinearArguments.AveragedDefaultArgs.L2RegularizerWeight,
            int numIterations                = AveragedLinearArguments.AveragedDefaultArgs.NumIterations,
            Action <AveragedPerceptronTrainer.Arguments> advancedSettings = null)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            return(new AveragedPerceptronTrainer(env, labelColumn, featureColumn, weights, lossFunction ?? new LogLoss(), learningRate, decreaseLearningRate, l2RegularizerWeight, numIterations, advancedSettings));
        }
Esempio n. 17
0
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the SDCA trainer, and a custom loss.
        /// Note that because we cannot be sure that all loss functions will produce naturally calibrated outputs, setting
        /// a custom loss function will not produce a calibrated probability column.
        /// </summary>
        /// <param name="ctx">The binary classification context trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="loss">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>
        public static (Scalar <float> score, Scalar <bool> predictedLabel) Sdca(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx,
            Scalar <bool> label, Vector <float> features,
            Scalar <float> weights,
            ISupportSdcaClassificationLoss loss,
            SdcaBinaryTrainer.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 = loss is LogLoss;

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

                var trainer = new SdcaBinaryTrainer(env, options);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans =>
                    {
                        var model = trans.Model;
                        if (model is ParameterMixingCalibratedPredictor cali)
                        {
                            onFit((LinearBinaryModelParameters)cali.SubPredictor);
                        }
                        else
                        {
                            onFit((LinearBinaryModelParameters)model);
                        }
                    }));
                }
                return(trainer);
            }, label, features, weights, hasProbs);

            return(rec.Output);
        }
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the SDCA trainer, and log-loss.
        /// </summary>
        /// <param name="ctx">The binary classification context trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="l2Const">The L2 regularization hyperparameter.</param>
        /// <param name="l1Threshold">The L1 regularization hyperparameter. Higher values will tend to lead to more sparse model.</param>
        /// <param name="maxIterations">The maximum number of passes to perform over the data.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}.Fit(DataView{TTupleInShape})"/> method is called on the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, 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), the calibrated prediction (from 0 to 1), and the predicted label.</returns>
        public static (Scalar <float> score, Scalar <float> probability, Scalar <bool> predictedLabel) Sdca(
            this BinaryClassificationContext.BinaryClassificationTrainers ctx,
            Scalar <bool> label, Vector <float> features, Scalar <float> weights = null,
            float?l2Const     = null,
            float?l1Threshold = null,
            int?maxIterations = null,
            Action <LinearBinaryPredictor, ParameterMixingCalibratedPredictor> onFit = null)
        {
            Contracts.CheckValue(label, nameof(label));
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckParam(!(l2Const < 0), nameof(l2Const), "Must not be negative, if specified.");
            Contracts.CheckParam(!(l1Threshold < 0), nameof(l1Threshold), "Must not be negative, if specified.");
            Contracts.CheckParam(!(maxIterations < 1), nameof(maxIterations), "Must be positive if specified");
            Contracts.CheckValueOrNull(onFit);

            var args = new LinearClassificationTrainer.Arguments()
            {
                L2Const       = l2Const,
                L1Threshold   = l1Threshold,
                MaxIterations = maxIterations,
            };

            var rec = new TrainerEstimatorReconciler.BinaryClassifier(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new LinearClassificationTrainer(env, args, featuresName, labelName, weightsName);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans =>
                    {
                        // Under the default log-loss we assume a calibrated predictor.
                        var model = trans.Model;
                        var cali = (ParameterMixingCalibratedPredictor)model;
                        var pred = (LinearBinaryPredictor)cali.SubPredictor;
                        onFit(pred, cali);
                    }));
                }
                return(trainer);
            }, label, features, weights);

            return(rec.Output);
        }
Esempio n. 19
0
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer, and a custom loss.
        /// </summary>
        /// <param name="ctx">The binary classification context 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 BinaryClassificationContext.BinaryClassificationTrainers ctx,
            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);
        }
Esempio n. 20
0
        /// <summary>
        /// Predict a target using a linear binary classification model trained with the AveragedPerceptron trainer, and a custom loss.
        /// </summary>
        /// <param name="ctx">The binary classification context 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="learningRate">The learning Rate.</param>
        /// <param name="decreaseLearningRate">Decrease learning rate as iterations progress.</param>
        /// <param name="l2RegularizerWeight">L2 regularization weight.</param>
        /// <param name="numIterations">Number of training iterations through the data.</param>
        /// <param name="advancedSettings">A delegate to supply more avdanced 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 BinaryClassificationContext.BinaryClassificationTrainers ctx,
            Scalar <bool> label,
            Vector <float> features,
            Scalar <float> weights           = null,
            IClassificationLoss lossFunction = null,
            float learningRate        = AveragedLinearArguments.AveragedDefaultArgs.LearningRate,
            bool decreaseLearningRate = AveragedLinearArguments.AveragedDefaultArgs.DecreaseLearningRate,
            float l2RegularizerWeight = AveragedLinearArguments.AveragedDefaultArgs.L2RegularizerWeight,
            int numIterations         = AveragedLinearArguments.AveragedDefaultArgs.NumIterations,
            Action <AveragedPerceptronTrainer.Arguments> advancedSettings = null,
            Action <LinearBinaryModelParameters> onFit = null
            )
        {
            OnlineLinearStaticUtils.CheckUserParams(label, features, weights, learningRate, l2RegularizerWeight, numIterations, onFit, advancedSettings);

            bool hasProbs = lossFunction is LogLoss;

            var rec = new TrainerEstimatorReconciler.BinaryClassifierNoCalibration(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new AveragedPerceptronTrainer(env, labelName, featuresName, weightsName, lossFunction,
                                                            learningRate, decreaseLearningRate, l2RegularizerWeight, numIterations, advancedSettings);

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

            return(rec.Output);
        }
        /// <summary>
        ///  Predict a target using a linear binary classification model trained with the <see cref="StochasticGradientDescentClassificationTrainer"/> trainer.
        /// </summary>
        /// <param name="ctx">The binary classificaiton context trainer object.</param>
        /// <param name="labelColumn">The name of the label column.</param>
        /// <param name="featureColumn">The name of the feature column.</param>
        /// <param name="weights">The name for the example weight column.</param>
        /// <param name="maxIterations">The maximum number of iterations; set to 1 to simulate online learning.</param>
        /// <param name="initLearningRate">The initial learning rate used by SGD.</param>
        /// <param name="l2Weight">The L2 regularization constant.</param>
        /// <param name="loss">The loss function to use.</param>
        /// <param name="advancedSettings">A delegate to apply all the advanced arguments to the algorithm.</param>
        public static StochasticGradientDescentClassificationTrainer StochasticGradientDescent(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                                               string labelColumn      = DefaultColumnNames.Label,
                                                                                               string featureColumn    = DefaultColumnNames.Features,
                                                                                               string weights          = null,
                                                                                               int maxIterations       = SgdArguments.Defaults.MaxIterations,
                                                                                               double initLearningRate = SgdArguments.Defaults.InitLearningRate,
                                                                                               float l2Weight          = SgdArguments.Defaults.L2Weight,
                                                                                               ISupportClassificationLossFactory loss = null,
                                                                                               Action <SgdArguments> advancedSettings = null)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            return(new StochasticGradientDescentClassificationTrainer(env, labelColumn, featureColumn, weights, maxIterations, initLearningRate, l2Weight, loss, advancedSettings));
        }
Esempio n. 22
0
        /// <summary>
        ///  Predict a target using a linear binary classification model trained with the <see cref="Microsoft.ML.Runtime.Learners.LogisticRegression"/> trainer.
        /// </summary>
        /// <param name="ctx">The binary classificaiton context trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="enoforceNoNegativity">Enforce non-negative weights.</param>
        /// <param name="l1Weight">Weight of L1 regularization term.</param>
        /// <param name="l2Weight">Weight of L2 regularization term.</param>
        /// <param name="memorySize">Memory size for <see cref="Microsoft.ML.Runtime.Learners.LogisticRegression"/>. Low=faster, less accurate.</param>
        /// <param name="optimizationTolerance">Threshold for optimizer convergence.</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>
        /// <param name="advancedSettings">A delegate to apply all the advanced arguments to the algorithm.</param>
        /// <returns>The predicted output.</returns>
        public static (Scalar <float> score, Scalar <float> probability, Scalar <bool> predictedLabel) LogisticRegressionBinaryClassifier(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                                                                                          Scalar <bool> label,
                                                                                                                                          Vector <float> features,
                                                                                                                                          Scalar <float> weights              = null,
                                                                                                                                          float l1Weight                      = Arguments.Defaults.L1Weight,
                                                                                                                                          float l2Weight                      = Arguments.Defaults.L2Weight,
                                                                                                                                          float optimizationTolerance         = Arguments.Defaults.OptTol,
                                                                                                                                          int memorySize                      = Arguments.Defaults.MemorySize,
                                                                                                                                          bool enoforceNoNegativity           = Arguments.Defaults.EnforceNonNegativity,
                                                                                                                                          Action <Arguments> advancedSettings = null,
                                                                                                                                          Action <ParameterMixingCalibratedPredictor> onFit = null)
        {
            LbfgsStaticUtils.ValidateParams(label, features, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enoforceNoNegativity, advancedSettings, onFit);

            var rec = new TrainerEstimatorReconciler.BinaryClassifier(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new LogisticRegression(env, labelName, featuresName, weightsName,
                                                     l1Weight, l2Weight, optimizationTolerance, memorySize, enoforceNoNegativity, advancedSettings);

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

            return(rec.Output);
        }
        /// <summary>
        /// FastTree <see cref="BinaryClassificationContext"/> extension method.
        /// Predict a target using a decision tree binary classificaiton model trained with the <see cref="FastTreeBinaryClassificationTrainer"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
        /// <param name="label">The label column.</param>
        /// <param name="features">The features column.</param>
        /// <param name="weights">The optional 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 set of output columns including in order the predicted binary classification score (which will range
        /// from negative to positive infinity), the calibrated prediction (from 0 to 1), and the predicted label.</returns>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[FastTree](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Static/FastTreeBinaryClassification.cs)]
        /// ]]></format>
        /// </example>
        public static (Scalar <float> score, Scalar <float> probability, Scalar <bool> predictedLabel) FastTree(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                                                                Scalar <bool> label, Vector <float> features, Scalar <float> weights,
                                                                                                                FastTreeBinaryClassificationTrainer.Options options,
                                                                                                                Action <IPredictorWithFeatureWeights <float> > onFit = null)
        {
            Contracts.CheckValueOrNull(options);
            CheckUserValues(label, features, weights, onFit);

            var rec = new TrainerEstimatorReconciler.BinaryClassifier(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new FastTreeBinaryClassificationTrainer(env, options);

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

            return(rec.Output);
        }
Esempio n. 24
0
        /// <summary>
        ///  Predict a target using a linear binary classification model trained with the <see cref="Microsoft.ML.Learners.LogisticRegression"/> trainer.
        /// </summary>
        /// <param name="ctx">The binary classificaiton context trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</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>
        /// <param name="options">Advanced arguments to the algorithm.</param>
        /// <returns>The predicted output.</returns>
        public static (Scalar <float> score, Scalar <float> probability, Scalar <bool> predictedLabel) LogisticRegressionBinaryClassifier(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                                                                                          Scalar <bool> label,
                                                                                                                                          Vector <float> features,
                                                                                                                                          Scalar <float> weights,
                                                                                                                                          Options options,
                                                                                                                                          Action <ParameterMixingCalibratedPredictor> onFit = null)
        {
            Contracts.CheckValue(label, nameof(label));
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValue(options, nameof(options));
            Contracts.CheckValueOrNull(onFit);

            var rec = new TrainerEstimatorReconciler.BinaryClassifier(
                (env, labelName, featuresName, weightsName) =>
            {
                options.LabelColumn   = labelName;
                options.FeatureColumn = featuresName;
                options.WeightColumn  = weightsName != null ? Optional <string> .Explicit(weightsName) : Optional <string> .Implicit(DefaultColumnNames.Weight);

                var trainer = new LogisticRegression(env, options);

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

            return(rec.Output);
        }
Esempio n. 25
0
        /// <summary>
        /// Predict a target using a tree binary classification model trained with the <see cref="LightGbmBinaryTrainer"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
        /// <param name="label">The label column.</param>
        /// <param name="features">The features column.</param>
        /// <param name="weights">The weights column.</param>
        /// <param name="numLeaves">The number of leaves to use.</param>
        /// <param name="numBoostRound">Number of iterations.</param>
        /// <param name="minDataPerLeaf">The minimal number of documents allowed in a leaf of the tree, out of the subsampled data.</param>
        /// <param name="learningRate">The learning rate.</param>
        /// <param name="advancedSettings">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 set of output columns including in order the predicted binary classification score (which will range
        /// from negative to positive infinity), the calibrated prediction (from 0 to 1), and the predicted label.</returns>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[LightGBM](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Static/LightGBMBinaryClassification.cs)]
        /// ]]></format>
        /// </example>
        public static (Scalar <float> score, Scalar <float> probability, Scalar <bool> predictedLabel) LightGbm(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                                                                Scalar <bool> label, Vector <float> features, Scalar <float> weights = null,
                                                                                                                int?numLeaves       = null,
                                                                                                                int?minDataPerLeaf  = null,
                                                                                                                double?learningRate = null,
                                                                                                                int numBoostRound   = LightGbmArguments.Defaults.NumBoostRound,
                                                                                                                Action <LightGbmArguments> advancedSettings          = null,
                                                                                                                Action <IPredictorWithFeatureWeights <float> > onFit = null)
        {
            CheckUserValues(label, features, weights, numLeaves, minDataPerLeaf, learningRate, numBoostRound, advancedSettings, onFit);

            var rec = new TrainerEstimatorReconciler.BinaryClassifier(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new LightGbmBinaryTrainer(env, labelName, featuresName, weightsName, numLeaves,
                                                        minDataPerLeaf, learningRate, numBoostRound, advancedSettings);

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

            return(rec.Output);
        }
Esempio n. 26
0
        /// <summary>
        /// Predict a target using a field-aware factorization machine.
        /// </summary>
        /// <param name="ctx">The binary classifier context trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="learningRate">Initial learning rate.</param>
        /// <param name="numIterations">Number of training iterations.</param>
        /// <param name="numLatentDimensions">Latent space dimensions.</param>
        /// <param name="advancedSettings">A delegate to set more settings.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}.Fit(DataView{TTupleInShape})"/> method is called on the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}"/> instance created out of this. This delegate will receive
        /// the 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 predicted output.</returns>
        public static (Scalar <float> score, Scalar <bool> predictedLabel) FieldAwareFactorizationMachine(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                                                          Scalar <bool> label, Vector <float>[] features,
                                                                                                          float learningRate      = 0.1f,
                                                                                                          int numIterations       = 5,
                                                                                                          int numLatentDimensions = 20,
                                                                                                          Action <FieldAwareFactorizationMachineTrainer.Arguments> advancedSettings = null,
                                                                                                          Action <FieldAwareFactorizationMachinePredictor> onFit = null)
        {
            Contracts.CheckValue(label, nameof(label));
            Contracts.CheckNonEmpty(features, nameof(features));

            Contracts.CheckParam(learningRate > 0, nameof(learningRate), "Must be positive");
            Contracts.CheckParam(numIterations > 0, nameof(numIterations), "Must be positive");
            Contracts.CheckParam(numLatentDimensions > 0, nameof(numLatentDimensions), "Must be positive");
            Contracts.CheckValueOrNull(advancedSettings);
            Contracts.CheckValueOrNull(onFit);

            var rec = new CustomReconciler((env, labelCol, featureCols) =>
            {
                var trainer = new FieldAwareFactorizationMachineTrainer(env, labelCol, featureCols, advancedSettings:
                                                                        args =>
                {
                    advancedSettings?.Invoke(args);
                    args.LearningRate = learningRate;
                    args.Iters        = numIterations;
                    args.LatentDim    = numLatentDimensions;
                });
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                else
                {
                    return(trainer);
                }
            }, label, features);

            return(rec.Output);
        }
        /// <summary>
        /// Predict a target using a field-aware factorization machine algorithm.
        /// </summary>
        /// <param name="ctx">The binary classification context trainer object.</param>
        /// <param name="label">The label, or dependent variable.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="advancedSettings">A delegate to set more settings.</param>
        /// <returns></returns>
        public static FieldAwareFactorizationMachineTrainer FieldAwareFactorizationMachine(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                                           string label, string[] features,
                                                                                           string weights = null,
                                                                                           Action <FieldAwareFactorizationMachineTrainer.Arguments> advancedSettings = null)
        {
            Contracts.CheckValue(ctx, nameof(ctx));
            var env = CatalogUtils.GetEnvironment(ctx);

            return(new FieldAwareFactorizationMachineTrainer(env, label, features, weights, advancedSettings: advancedSettings));
        }
Esempio n. 28
0
        /// <summary>
        /// FastTree <see cref="BinaryClassificationContext"/> extension method.
        /// Predict a target using a decision tree binary classificaiton model trained with the <see cref="FastTreeBinaryClassificationTrainer"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
        /// <param name="label">The label column.</param>
        /// <param name="features">The features column.</param>
        /// <param name="weights">The optional weights column.</param>
        /// <param name="numTrees">Total number of decision trees to create in the ensemble.</param>
        /// <param name="numLeaves">The maximum number of leaves per decision tree.</param>
        /// <param name="minDatapointsInLeaves">The minimal number of datapoints allowed in a leaf of the tree, out of the subsampled data.</param>
        /// <param name="learningRate">The learning rate.</param>
        /// <param name="advancedSettings">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 set of output columns including in order the predicted binary classification score (which will range
        /// from negative to positive infinity), the calibrated prediction (from 0 to 1), and the predicted label.</returns>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[FastTree](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Static/FastTreeBinaryClassification.cs)]
        /// ]]></format>
        /// </example>
        public static (Scalar <float> score, Scalar <float> probability, Scalar <bool> predictedLabel) FastTree(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                                                                Scalar <bool> label, Vector <float> features, Scalar <float> weights = null,
                                                                                                                int numLeaves             = Defaults.NumLeaves,
                                                                                                                int numTrees              = Defaults.NumTrees,
                                                                                                                int minDatapointsInLeaves = Defaults.MinDocumentsInLeaves,
                                                                                                                double learningRate       = Defaults.LearningRates,
                                                                                                                Action <FastTreeBinaryClassificationTrainer.Arguments> advancedSettings = null,
                                                                                                                Action <IPredictorWithFeatureWeights <float> > onFit = null)
        {
            CheckUserValues(label, features, weights, numLeaves, numTrees, minDatapointsInLeaves, learningRate, advancedSettings, onFit);

            var rec = new TrainerEstimatorReconciler.BinaryClassifier(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new FastTreeBinaryClassificationTrainer(env, labelName, featuresName, weightsName, numLeaves,
                                                                      numTrees, minDatapointsInLeaves, learningRate, advancedSettings);

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

            return(rec.Output);
        }
Esempio n. 29
0
        /// <summary>
        ///  Predict a target using a linear binary classification model trained with the <see cref="Microsoft.ML.Trainers.StochasticGradientDescentClassificationTrainer"/> trainer.
        /// </summary>
        /// <param name="ctx">The binary classificaiton context trainer object.</param>
        /// <param name="label">The name of the label column.</param>
        /// <param name="features">The name of the feature column.</param>
        /// <param name="weights">The name for the example weight column.</param>
        /// <param name="options">Advanced arguments to the algorithm.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}.Fit(DataView{TTupleInShape})"/> method is called on the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, 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 predicted output.</returns>
        public static (Scalar <float> score, Scalar <float> probability, Scalar <bool> predictedLabel) StochasticGradientDescentClassificationTrainer(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                                                                                                      Scalar <bool> label,
                                                                                                                                                      Vector <float> features,
                                                                                                                                                      Scalar <float> weights,
                                                                                                                                                      Options options,
                                                                                                                                                      Action <IPredictorWithFeatureWeights <float> > onFit = null)
        {
            var rec = new TrainerEstimatorReconciler.BinaryClassifier(
                (env, labelName, featuresName, weightsName) =>
            {
                options.FeatureColumn = featuresName;
                options.LabelColumn   = labelName;
                options.WeightColumn  = weightsName != null ? Optional <string> .Explicit(weightsName) : Optional <string> .Implicit(DefaultColumnNames.Weight);

                var trainer = new StochasticGradientDescentClassificationTrainer(env, options);

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

            return(rec.Output);
        }
Esempio n. 30
0
        /// <summary>
        ///  Predict a target using a linear binary classification model trained with the <see cref="Microsoft.ML.Trainers.StochasticGradientDescentClassificationTrainer"/> trainer.
        /// </summary>
        /// <param name="ctx">The binary classificaiton context trainer object.</param>
        /// <param name="label">The name of the label column.</param>
        /// <param name="features">The name of the feature column.</param>
        /// <param name="weights">The name for the example weight column.</param>
        /// <param name="maxIterations">The maximum number of iterations; set to 1 to simulate online learning.</param>
        /// <param name="initLearningRate">The initial learning rate used by SGD.</param>
        /// <param name="l2Weight">The L2 regularization constant.</param>
        /// <param name="loss">The loss function to use.</param>
        /// <param name="advancedSettings">A delegate to apply all the advanced arguments to the algorithm.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, TTransformer}.Fit(DataView{TTupleInShape})"/> method is called on the
        /// <see cref="Estimator{TTupleInShape, TTupleOutShape, 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 predicted output.</returns>
        public static (Scalar <float> score, Scalar <float> probability, Scalar <bool> predictedLabel) StochasticGradientDescentClassificationTrainer(this BinaryClassificationContext.BinaryClassificationTrainers ctx,
                                                                                                                                                      Scalar <bool> label,
                                                                                                                                                      Vector <float> features,
                                                                                                                                                      Scalar <float> weights  = null,
                                                                                                                                                      int maxIterations       = Arguments.Defaults.MaxIterations,
                                                                                                                                                      double initLearningRate = Arguments.Defaults.InitLearningRate,
                                                                                                                                                      float l2Weight          = Arguments.Defaults.L2Weight,
                                                                                                                                                      ISupportClassificationLossFactory loss = null,
                                                                                                                                                      Action <Arguments> advancedSettings    = null,
                                                                                                                                                      Action <IPredictorWithFeatureWeights <float> > onFit = null)
        {
            var rec = new TrainerEstimatorReconciler.BinaryClassifier(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new StochasticGradientDescentClassificationTrainer(env, labelName, featuresName, weightsName, maxIterations, initLearningRate, l2Weight, loss, advancedSettings);

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

            return(rec.Output);
        }