Beispiel #1
0
        /// <summary>
        /// Ranks a series of inputs based on their relevance, training a decision tree ranking model through the <see cref="LightGbmRankingTrainer"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="RankingContext"/>.</param>
        /// <param name="label">The label column.</param>
        /// <param name="features">The features column.</param>
        /// <param name="groupId">The groupId 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>
        public static Scalar <float> LightGbm <TVal>(this RankingContext.RankingTrainers ctx,
                                                     Scalar <float> label, Vector <float> features, Key <uint, TVal> groupId, Scalar <float> weights = null,
                                                     int?numLeaves       = null,
                                                     int?minDataPerLeaf  = null,
                                                     double?learningRate = null,
                                                     int numBoostRound   = LightGbmArguments.Defaults.NumBoostRound,
                                                     Action <LightGbmArguments> advancedSettings = null,
                                                     Action <LightGbmRankingPredictor> onFit     = null)
        {
            CheckUserValues(label, features, weights, numLeaves, minDataPerLeaf, learningRate, numBoostRound, advancedSettings, onFit);
            Contracts.CheckValue(groupId, nameof(groupId));

            var rec = new TrainerEstimatorReconciler.Ranker <TVal>(
                (env, labelName, featuresName, groupIdName, weightsName) =>
            {
                var trainer = new LightGbmRankingTrainer(env, labelName, featuresName, groupIdName, weightsName, numLeaves,
                                                         minDataPerLeaf, learningRate, numBoostRound, advancedSettings);

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

            return(rec.Score);
        }
        /// <summary>
        /// FastTree <see cref="RegressionContext"/> extension method.
        /// Predicts a target using a decision tree regression model trained with the <see cref="FastTreeRegressionTrainer"/>.
        /// </summary>
        /// <param name="ctx">The <see cref="RegressionContext"/>.</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 a regression 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 Score output column indicating the predicted value.</returns>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[SDCA](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Static/FastTree.cs?range=6-11,19-69 "FastTree regression example.")]
        /// ]]></format>
        /// </example>
        public static Scalar <float> FastTree(this RegressionContext.RegressionTrainers ctx,
                                              Scalar <float> label, Vector <float> features, Scalar <float> weights = null,
                                              int numLeaves            = Defaults.NumLeaves,
                                              int numTrees             = Defaults.NumTrees,
                                              int minDatapointsInLeafs = Defaults.MinDocumentsInLeafs,
                                              double learningRate      = Defaults.LearningRates,
                                              Action <FastTreeRegressionTrainer.Arguments> advancedSettings = null,
                                              Action <FastTreeRegressionPredictor> onFit = null)
        {
            CheckUserValues(label, features, weights, numLeaves, numTrees, minDatapointsInLeafs, learningRate, advancedSettings, onFit);

            var rec = new TrainerEstimatorReconciler.Regression(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new FastTreeRegressionTrainer(env, labelName, featuresName, weightsName, numLeaves,
                                                            numTrees, minDatapointsInLeafs, learningRate, advancedSettings);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                return(trainer);
            }, label, features, weights);

            return(rec.Score);
        }
Beispiel #3
0
        /// <summary>
        /// KMeans <see cref="ClusteringCatalog"/> extension method.
        /// </summary>
        /// <param name="catalog">The clustering catalog trainer object.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="clustersCount">The number of clusters to use for KMeans.</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 predicted output.</returns>
        public static (Vector <float> score, Key <uint> predictedLabel) KMeans(this ClusteringCatalog.ClusteringTrainers catalog,
                                                                               Vector <float> features, Scalar <float> weights = null,
                                                                               int clustersCount = KMeansPlusPlusTrainer.Defaults.ClustersCount,
                                                                               Action <KMeansModelParameters> onFit = null)
        {
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckParam(clustersCount > 1, nameof(clustersCount), "If provided, must be greater than 1.");
            Contracts.CheckValueOrNull(onFit);

            var rec = new TrainerEstimatorReconciler.Clustering(
                (env, featuresName, weightsName) =>
            {
                var options = new KMeansPlusPlusTrainer.Options
                {
                    FeatureColumn = featuresName,
                    ClustersCount = clustersCount,
                    WeightColumn  = weightsName
                };

                var trainer = new KMeansPlusPlusTrainer(env, options);

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

            return(rec.Output);
        }
 internal static void CheckUserValues(PipelineColumn label, Vector <float> features, Scalar <float> weights,
                                      int?numLeaves,
                                      int?minDataPerLeaf,
                                      double?learningRate,
                                      int numBoostRound,
                                      Delegate advancedSettings,
                                      Delegate onFit)
 {
     Contracts.CheckValue(label, nameof(label));
     Contracts.CheckValue(features, nameof(features));
     Contracts.CheckValueOrNull(weights);
     Contracts.CheckParam(!(numLeaves < 2), nameof(numLeaves), "Must be at least 2.");
     Contracts.CheckParam(!(minDataPerLeaf <= 0), nameof(minDataPerLeaf), "Must be positive");
     Contracts.CheckParam(!(learningRate <= 0), nameof(learningRate), "Must be positive");
     Contracts.CheckParam(numBoostRound > 0, nameof(numBoostRound), "Must be positive");
     Contracts.CheckValueOrNull(advancedSettings);
     Contracts.CheckValueOrNull(onFit);
 }
        /// <summary>
        /// LightGbm <see cref="BinaryClassificationContext"/> extension method.
        /// </summary>
        /// <param name="ctx">The <see cref="BinaryClassificationContext"/>.</param>
        /// <param name="label">The label column.</param>
        /// <param name="features">The features colum.</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>
        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)
        {
            LightGbmStaticsUtils.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);
        }
Beispiel #6
0
        /// <summary>
        /// Predict a target using a linear regression model trained with the SDCA trainer.
        /// </summary>
        /// <param name="ctx">The regression 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="loss">The custom loss, if unspecified will be <see cref="SquaredLossSDCARegressionLossFunction"/>.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TInShape, TShape, 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 predicted output.</returns>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[SDCA](../../../docs/samples/Microsoft.ML.Samples.StaticPipe/Trainers.cs?range=5-8,12-70) "The SDCA regression example."]
        /// ]]></format>
        /// </example>
        public static Scalar <float> Sdca(this RegressionContext.RegressionTrainers ctx,
                                          Scalar <float> label, Vector <float> features, Scalar <float> weights = null,
                                          float?l2Const     = null,
                                          float?l1Threshold = null,
                                          int?maxIterations = null,
                                          ISupportSdcaRegressionLoss loss          = null,
                                          Action <LinearRegressionPredictor> 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(loss);
            Contracts.CheckValueOrNull(onFit);

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

            if (loss != null)
            {
                args.LossFunction = new TrivialRegressionLossFactory(loss);
            }

            var rec = new TrainerEstimatorReconciler.Regression(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new SdcaRegressionTrainer(env, args, featuresName, labelName, weightsName);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                return(trainer);
            }, label, features, weights);

            return(rec.Score);
        }
Beispiel #7
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="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.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>
        /// <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              = Options.Defaults.L1Weight,
                                                                                                                                          float l2Weight              = Options.Defaults.L2Weight,
                                                                                                                                          float optimizationTolerance = Options.Defaults.OptTol,
                                                                                                                                          int memorySize              = Options.Defaults.MemorySize,
                                                                                                                                          bool enoforceNoNegativity   = Options.Defaults.EnforceNonNegativity,
                                                                                                                                          Action <ParameterMixingCalibratedPredictor> onFit = null)
        {
            LbfgsStaticUtils.ValidateParams(label, features, weights, l1Weight, l2Weight, optimizationTolerance, memorySize, enoforceNoNegativity, onFit);

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

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

            return(rec.Output);
        }
Beispiel #8
0
 internal static void CheckUserValues(PipelineColumn label, Vector <float> features, Scalar <float> weights,
                                      int numberOfLeaves,
                                      int numberOfTrees,
                                      int minimumExampleCountPerLeaf,
                                      double learningRate,
                                      Delegate onFit)
 {
     Contracts.CheckValue(label, nameof(label));
     Contracts.CheckValue(features, nameof(features));
     Contracts.CheckValueOrNull(weights);
     Contracts.CheckParam(numberOfLeaves >= 2, nameof(numberOfLeaves), "Must be at least 2.");
     Contracts.CheckParam(numberOfTrees > 0, nameof(numberOfTrees), "Must be positive");
     Contracts.CheckParam(minimumExampleCountPerLeaf > 0, nameof(minimumExampleCountPerLeaf), "Must be positive");
     Contracts.CheckParam(learningRate > 0, nameof(learningRate), "Must be positive");
     Contracts.CheckValueOrNull(onFit);
 }
Beispiel #9
0
        /// <summary>
        /// FastTree <see cref="RegressionCatalog"/> extension method.
        /// Predicts a target using a decision tree regression model trained with the <see cref="FastTreeRegressionTrainer"/>.
        /// </summary>
        /// <param name="catalog">The <see cref="RegressionCatalog"/>.</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="numberOfTrees">Total number of decision trees to create in the ensemble.</param>
        /// <param name="numberOfLeaves">The maximum number of leaves per decision tree.</param>
        /// <param name="minimumExampleCountPerLeaf">The minimal number of data points allowed in a leaf of a regression tree, out of the subsampled data.</param>
        /// <param name="learningRate">The learning rate.</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 Score output column indicating the predicted value.</returns>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[FastTree](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Static/FastTreeRegression.cs)]
        /// ]]></format>
        /// </example>
        public static Scalar <float> FastTree(this RegressionCatalog.RegressionTrainers catalog,
                                              Scalar <float> label, Vector <float> features, Scalar <float> weights = null,
                                              int numberOfLeaves             = Defaults.NumberOfLeaves,
                                              int numberOfTrees              = Defaults.NumberOfTrees,
                                              int minimumExampleCountPerLeaf = Defaults.MinimumExampleCountPerLeaf,
                                              double learningRate            = Defaults.LearningRate,
                                              Action <FastTreeRegressionModelParameters> onFit = null)
        {
            CheckUserValues(label, features, weights, numberOfLeaves, numberOfTrees, minimumExampleCountPerLeaf, learningRate, onFit);

            var rec = new TrainerEstimatorReconciler.Regression(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new FastTreeRegressionTrainer(env, labelName, featuresName, weightsName, numberOfLeaves,
                                                            numberOfTrees, minimumExampleCountPerLeaf, learningRate);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                return(trainer);
            }, label, features, weights);

            return(rec.Score);
        }
Beispiel #10
0
            public CustomReconciler(Func <IHostEnvironment, string, string[], IEstimator <ITransformer> > factory, Scalar <bool> label, Vector <float>[] features)
                : base(MakeInputs(Contracts.CheckRef(label, nameof(label)), Contracts.CheckRef(features, nameof(features))), _fixedOutputNames)
            {
                Contracts.AssertValue(factory);
                _factory = factory;

                Output  = (new Impl(this), new ImplBool(this));
                Outputs = new PipelineColumn[] { Output.score, Output.predictedLabel };
            }
Beispiel #11
0
        /// <summary>
        /// FastTree <see cref="RankingCatalog"/>.
        /// Ranks a series of inputs based on their relevance, training a decision tree ranking model through the <see cref="FastTreeRankingTrainer"/>.
        /// </summary>
        /// <param name="catalog">The <see cref="RegressionCatalog"/>.</param>
        /// <param name="label">The label column.</param>
        /// <param name="features">The features column.</param>
        /// <param name="groupId">The groupId 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 Score output column indicating the predicted value.</returns>
        public static Scalar <float> FastTree <TVal>(this RankingCatalog.RankingTrainers catalog,
                                                     Scalar <float> label, Vector <float> features, Key <uint, TVal> groupId, Scalar <float> weights,
                                                     FastTreeRankingTrainer.Options options,
                                                     Action <FastTreeRankingModelParameters> onFit = null)
        {
            Contracts.CheckValueOrNull(options);
            CheckUserValues(label, features, weights, onFit);

            var rec = new TrainerEstimatorReconciler.Ranker <TVal>(
                (env, labelName, featuresName, groupIdName, weightsName) =>
            {
                options.LabelColumnName         = labelName;
                options.FeatureColumnName       = featuresName;
                options.RowGroupColumnName      = groupIdName;
                options.ExampleWeightColumnName = weightsName;

                var trainer = new FastTreeRankingTrainer(env, options);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                return(trainer);
            }, label, features, groupId, weights);

            return(rec.Score);
        }
Beispiel #12
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{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 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);
        }
Beispiel #13
0
        /// <summary>
        /// Predict a target using a linear regression model trained with the SDCA trainer.
        /// </summary>
        /// <param name="catalog">The regression catalog 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="options">Advanced arguments to the algorithm.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TInShape, TShape, 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 predicted output.</returns>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[SDCA](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Static/SDCARegression.cs)]
        /// ]]></format>
        /// </example>
        public static Scalar <float> Sdca(this RegressionCatalog.RegressionTrainers catalog,
                                          Scalar <float> label, Vector <float> features, Scalar <float> weights,
                                          SdcaRegressionTrainer.Options options,
                                          Action <LinearRegressionModelParameters> onFit = null)
        {
            Contracts.CheckValue(label, nameof(label));
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckValueOrNull(options);
            Contracts.CheckValueOrNull(onFit);

            var rec = new TrainerEstimatorReconciler.Regression(
                (env, labelName, featuresName, weightsName) =>
            {
                options.LabelColumnName   = labelName;
                options.FeatureColumnName = featuresName;

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

            return(rec.Score);
        }
Beispiel #14
0
        /// <summary>
        /// Predict a target using a linear regression model trained with the SDCA trainer.
        /// </summary>
        /// <param name="catalog">The regression catalog 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="l2Regularization">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="numberOfIterations">The maximum number of passes to perform over the data.</param>
        /// <param name="loss">The custom loss, if unspecified will be <see cref="SquaredLoss"/>.</param>
        /// <param name="onFit">A delegate that is called every time the
        /// <see cref="Estimator{TInShape, TShape, 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 predicted output.</returns>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[SDCA](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Static/SDCARegression.cs)]
        /// ]]></format>
        /// </example>
        public static Scalar <float> Sdca(this RegressionCatalog.RegressionTrainers catalog,
                                          Scalar <float> label, Vector <float> features, Scalar <float> weights = null,
                                          float?l2Regularization          = null,
                                          float?l1Threshold               = null,
                                          int?numberOfIterations          = null,
                                          ISupportSdcaRegressionLoss loss = null,
                                          Action <LinearRegressionModelParameters> onFit = null)
        {
            Contracts.CheckValue(label, nameof(label));
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckParam(!(l2Regularization < 0), nameof(l2Regularization), "Must not be negative, if specified.");
            Contracts.CheckParam(!(l1Threshold < 0), nameof(l1Threshold), "Must not be negative, if specified.");
            Contracts.CheckParam(!(numberOfIterations < 1), nameof(numberOfIterations), "Must be positive if specified");
            Contracts.CheckValueOrNull(loss);
            Contracts.CheckValueOrNull(onFit);

            var rec = new TrainerEstimatorReconciler.Regression(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new SdcaRegressionTrainer(env, labelName, featuresName, weightsName, loss, l2Regularization, l1Threshold, numberOfIterations);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                return(trainer);
            }, label, features, weights);

            return(rec.Score);
        }
Beispiel #15
0
 /// <summary>
 /// Perform IID spike detection over a column of time series data. See <see cref="IidSpikeEstimator"/>.
 /// </summary>
 public static Vector <double> IidSpikeDetect(
     this Scalar <float> input,
     int confidence,
     int pvalueHistoryLength,
     AnomalySide side = AnomalySide.TwoSided
     ) => new OutColumn(input, confidence, pvalueHistoryLength, side);
Beispiel #16
0
        /// <summary>
        /// FastTree <see cref="BinaryClassificationCatalog"/> extension method.
        /// Predict a target using a decision tree binary classificaiton model trained with the <see cref="FastTreeBinaryClassificationTrainer"/>.
        /// </summary>
        /// <param name="catalog">The <see cref="BinaryClassificationCatalog"/>.</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="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 BinaryClassificationCatalog.BinaryClassificationTrainers catalog,
                                                                                                                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 <CalibratedModelParametersBase <FastTreeBinaryModelParameters, PlattCalibrator> > onFit = null)
        {
            CheckUserValues(label, features, weights, numLeaves, numTrees, minDatapointsInLeaves, learningRate, onFit);

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

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

            return(rec.Output);
        }
Beispiel #17
0
 /// <summary>
 /// Perform IID change point detection over a column of time series data. See <see cref="IidChangePointEstimator"/>.
 /// </summary>
 public static Vector <double> IidChangePointDetect(
     this Scalar <float> input,
     int confidence,
     int changeHistoryLength,
     MartingaleType martingale = MartingaleType.Power,
     double eps = 0.1) => new OutColumn(input, confidence, changeHistoryLength, martingale, eps);
Beispiel #18
0
        /// <summary>
        /// FastTree <see cref="BinaryClassificationCatalog"/> extension method.
        /// Predict a target using a decision tree binary classificaiton model trained with the <see cref="FastTreeBinaryClassificationTrainer"/>.
        /// </summary>
        /// <param name="catalog">The <see cref="BinaryClassificationCatalog"/>.</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 BinaryClassificationCatalog.BinaryClassificationTrainers catalog,
                                                                                                                Scalar <bool> label, Vector <float> features, Scalar <float> weights,
                                                                                                                FastTreeBinaryClassificationTrainer.Options options,
                                                                                                                Action <CalibratedModelParametersBase <FastTreeBinaryModelParameters, PlattCalibrator> > onFit = null)
        {
            Contracts.CheckValueOrNull(options);
            CheckUserValues(label, features, weights, 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 FastTreeBinaryClassificationTrainer(env, options);

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

            return(rec.Output);
        }
Beispiel #19
0
        /// <summary>
        /// KMeans <see cref="ClusteringContext"/> extension method.
        /// </summary>
        /// <param name="ctx">The regression context trainer object.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</param>
        /// <param name="clustersCount">The number of clusters to use for KMeans.</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 predicted output.</returns>
        public static (Vector <float> score, Key <uint> predictedLabel) KMeans(this ClusteringContext.ClusteringTrainers ctx,
                                                                               Vector <float> features, Scalar <float> weights = null,
                                                                               int clustersCount = KMeansPlusPlusTrainer.Defaults.K,
                                                                               Action <KMeansPlusPlusTrainer.Arguments> advancedSettings = null,
                                                                               Action <KMeansModelParameters> onFit = null)
        {
            Contracts.CheckValue(features, nameof(features));
            Contracts.CheckValueOrNull(weights);
            Contracts.CheckParam(clustersCount > 1, nameof(clustersCount), "If provided, must be greater than 1.");
            Contracts.CheckValueOrNull(onFit);
            Contracts.CheckValueOrNull(advancedSettings);

            var rec = new TrainerEstimatorReconciler.Clustering(
                (env, featuresName, weightsName) =>
            {
                var trainer = new KMeansPlusPlusTrainer(env, featuresName, clustersCount, weightsName, advancedSettings);

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

            return(rec.Output);
        }
Beispiel #20
0
        /// <summary>
        /// FastTree <see cref="RankingCatalog"/>.
        /// Ranks a series of inputs based on their relevance, training a decision tree ranking model through the <see cref="FastTreeRankingTrainer"/>.
        /// </summary>
        /// <param name="catalog">The <see cref="RegressionCatalog"/>.</param>
        /// <param name="label">The label column.</param>
        /// <param name="features">The features column.</param>
        /// <param name="groupId">The groupId 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 a regression tree, out of the subsampled data.</param>
        /// <param name="learningRate">The learning rate.</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 Score output column indicating the predicted value.</returns>
        public static Scalar <float> FastTree <TVal>(this RankingCatalog.RankingTrainers catalog,
                                                     Scalar <float> label, Vector <float> features, Key <uint, TVal> groupId, Scalar <float> weights = null,
                                                     int numLeaves             = Defaults.NumLeaves,
                                                     int numTrees              = Defaults.NumTrees,
                                                     int minDatapointsInLeaves = Defaults.MinDocumentsInLeaves,
                                                     double learningRate       = Defaults.LearningRates,
                                                     Action <FastTreeRankingModelParameters> onFit = null)
        {
            CheckUserValues(label, features, weights, numLeaves, numTrees, minDatapointsInLeaves, learningRate, onFit);

            var rec = new TrainerEstimatorReconciler.Ranker <TVal>(
                (env, labelName, featuresName, groupIdName, weightsName) =>
            {
                var trainer = new FastTreeRankingTrainer(env, labelName, featuresName, groupIdName, weightsName, numLeaves,
                                                         numTrees, minDatapointsInLeaves, learningRate);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                return(trainer);
            }, label, features, groupId, weights);

            return(rec.Score);
        }
Beispiel #21
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);
        }
Beispiel #22
0
 internal static void CheckUserValues(PipelineColumn label, Vector <float> features, Scalar <float> weights,
                                      Delegate onFit)
 {
     Contracts.CheckValue(label, nameof(label));
     Contracts.CheckValue(features, nameof(features));
     Contracts.CheckValueOrNull(weights);
     Contracts.CheckValueOrNull(onFit);
 }
        /// <summary>
        /// LightGbm <see cref="RegressionContext"/> extension method.
        /// </summary>
        /// <param name="ctx">The <see cref="RegressionContext"/>.</param>
        /// <param name="label">The label column.</param>
        /// <param name="features">The features colum.</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 Score output column indicating the predicted value.</returns>
        public static Scalar <float> LightGbm(this RegressionContext.RegressionTrainers ctx,
                                              Scalar <float> 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 <LightGbmRegressionPredictor> onFit  = null)
        {
            LightGbmStaticsUtils.CheckUserValues(label, features, weights, numLeaves, minDataPerLeaf, learningRate, numBoostRound, advancedSettings, onFit);

            var rec = new TrainerEstimatorReconciler.Regression(
                (env, labelName, featuresName, weightsName) =>
            {
                var trainer = new LightGbmRegressorTrainer(env, labelName, featuresName, weightsName, numLeaves,
                                                           minDataPerLeaf, learningRate, numBoostRound, advancedSettings);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                return(trainer);
            }, label, features, weights);

            return(rec.Score);
        }
Beispiel #24
0
        /// <summary>
        /// FastTree <see cref="RegressionCatalog"/> extension method.
        /// Predicts a target using a decision tree regression model trained with the <see cref="FastTreeRegressionTrainer"/>.
        /// </summary>
        /// <param name="catalog">The <see cref="RegressionCatalog"/>.</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 Score output column indicating the predicted value.</returns>
        /// <example>
        /// <format type="text/markdown">
        /// <![CDATA[
        ///  [!code-csharp[FastTree](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Static/FastTreeRegression.cs)]
        /// ]]></format>
        /// </example>
        public static Scalar <float> FastTree(this RegressionCatalog.RegressionTrainers catalog,
                                              Scalar <float> label, Vector <float> features, Scalar <float> weights,
                                              FastTreeRegressionTrainer.Options options,
                                              Action <FastTreeRegressionModelParameters> onFit = null)
        {
            Contracts.CheckValueOrNull(options);
            CheckUserValues(label, features, weights, onFit);

            var rec = new TrainerEstimatorReconciler.Regression(
                (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 FastTreeRegressionTrainer(env, options);
                if (onFit != null)
                {
                    return(trainer.WithOnFitDelegate(trans => onFit(trans.Model)));
                }
                return(trainer);
            }, label, features, weights);

            return(rec.Score);
        }
 internal static void CheckUserValues(PipelineColumn label, Vector <float> features, Scalar <float> weights,
                                      int numLeaves,
                                      int numTrees,
                                      int minDatapointsInLeafs,
                                      double learningRate,
                                      Delegate advancedSettings,
                                      Delegate onFit)
 {
     Contracts.CheckValue(label, nameof(label));
     Contracts.CheckValue(features, nameof(features));
     Contracts.CheckValueOrNull(weights);
     Contracts.CheckParam(numLeaves >= 2, nameof(numLeaves), "Must be at least 2.");
     Contracts.CheckParam(numTrees > 0, nameof(numTrees), "Must be positive");
     Contracts.CheckParam(minDatapointsInLeafs > 0, nameof(minDatapointsInLeafs), "Must be positive");
     Contracts.CheckParam(learningRate > 0, nameof(learningRate), "Must be positive");
     Contracts.CheckValueOrNull(advancedSettings);
     Contracts.CheckValueOrNull(onFit);
 }
Beispiel #26
0
 /// <summary>
 /// Converts the categorical value into an indicator array by building a dictionary of categories based on the data and using the id in the dictionary as the index in the array.
 /// </summary>
 /// <param name="input">Incoming data.</param>
 /// <param name="outputKind">Specify the output type of indicator array: array or binary encoded data.</param>
 /// <param name="order">How the Id for each value would be assigined: by occurrence or by value.</param>
 /// <param name="maxItems">Maximum number of ids to keep during data scanning.</param>
 /// <param name="onFit">Called upon fitting with the learnt enumeration on the dataset.</param>
 public static Vector <float> OneHotEncoding(this Scalar <string> input, OneHotScalarOutputKind outputKind = (OneHotScalarOutputKind)DefOut, KeyValueOrder order = DefSort,
                                             int maxItems = DefMax, ToKeyFitResult <ReadOnlyMemory <char> > .OnFit onFit = null)
 {
     Contracts.CheckValue(input, nameof(input));
     return(new ImplScalar <string>(input, new Config((OneHotVectorOutputKind)outputKind, order, maxItems, Wrap(onFit))));
 }
        /// <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="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>
        /// <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>
        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 minDatapointsInLeafs = Defaults.MinDocumentsInLeafs,
                                                                                                                double learningRate      = Defaults.LearningRates,
                                                                                                                Action <FastTreeBinaryClassificationTrainer.Arguments> advancedSettings = null,
                                                                                                                Action <IPredictorWithFeatureWeights <float> > onFit = null)
        {
            CheckUserValues(label, features, weights, numLeaves, numTrees, minDatapointsInLeafs, learningRate, advancedSettings, onFit);

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

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

            return(rec.Output);
        }
Beispiel #28
0
 /// <summary>
 /// Converts the categorical value into an indicator array by hashing categories into certain value and using that value as the index in the array.
 /// </summary>
 /// <param name="input">Incoming data.</param>
 /// <param name="outputKind">Specify the output type of indicator array: array or binary encoded data.</param>
 /// <param name="hashBits">Amount of bits to use for hashing.</param>
 /// <param name="seed">Seed value used for hashing.</param>
 /// <param name="ordered">Whether the position of each term should be included in the hash.</param>
 /// <param name="invertHash">During hashing we constuct mappings between original values and the produced hash values.
 /// Text representation of original values are stored in the slot names of the  metadata for the new column.Hashing, as such, can map many initial values to one.
 /// <paramref name="invertHash"/> specifies the upper bound of the number of distinct input values mapping to a hash that should be retained.
 /// <value>0</value> does not retain any input values. <value>-1</value> retains all input values mapping to each hash.</param>
 public static Vector <float> OneHotHashEncoding(this Scalar <string> input, OneHotHashScalarOutputKind outputKind = (OneHotHashScalarOutputKind)DefOut,
                                                 int hashBits = DefHashBits, uint seed = DefSeed, bool ordered = DefOrdered, int invertHash = DefInvertHash)
 {
     Contracts.CheckValue(input, nameof(input));
     return(new ImplScalar <string>(input, new Config((OneHotHashVectorOutputKind)outputKind, hashBits, seed, ordered, invertHash)));
 }
Beispiel #29
0
        /// <summary>
        /// KMeans <see cref="ClusteringCatalog"/> extension method.
        /// </summary>
        /// <param name="catalog">The regression catalog trainer object.</param>
        /// <param name="features">The features, or independent variables.</param>
        /// <param name="weights">The optional example weights.</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 predicted output.</returns>
        public static (Vector <float> score, Key <uint> predictedLabel) KMeans(this ClusteringCatalog.ClusteringTrainers catalog,
                                                                               Vector <float> features, Scalar <float> weights,
                                                                               KMeansPlusPlusTrainer.Options options,
                                                                               Action <KMeansModelParameters> onFit = null)
        {
            Contracts.CheckValueOrNull(onFit);
            Contracts.CheckValue(options, nameof(options));

            var rec = new TrainerEstimatorReconciler.Clustering(
                (env, featuresName, weightsName) =>
            {
                options.FeatureColumn = featuresName;
                options.WeightColumn  = weightsName;

                var trainer = new KMeansPlusPlusTrainer(env, options);

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

            return(rec.Output);
        }
Beispiel #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="catalog">The binary classificaiton catalog 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 BinaryClassificationCatalog.BinaryClassificationTrainers catalog,
                                                                                                                                                      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);
        }