/// <summary> /// Predict a target using a linear binary classification model trained with the <see cref="Microsoft.ML.Runtime.Learners.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, featuresName, labelName, weightsName, maxIterations, initLearningRate, l2Weight, loss, advancedSettings); if (onFit != null) { return(trainer.WithOnFitDelegate(trans => onFit(trans.Model))); } return(trainer); }, label, features, weights); return(rec.Output); }
/// <summary> /// Predict a target using a tree binary classification model trained with the <see cref="LightGbmBinaryTrainer"/>. /// </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 weights column.</param> /// <param name="numberOfLeaves">The number of leaves to use.</param> /// <param name="minimumExampleCountPerLeaf">The minimal number of data points allowed in a leaf of the tree, out of the subsampled data.</param> /// <param name="learningRate">The learning rate.</param> /// <param name="numberOfIterations">Number of iterations.</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 BinaryClassificationCatalog.BinaryClassificationTrainers catalog, Scalar <bool> label, Vector <float> features, Scalar <float> weights = null, int?numberOfLeaves = null, int?minimumExampleCountPerLeaf = null, double?learningRate = null, int numberOfIterations = Options.Defaults.NumberOfIterations, Action <CalibratedModelParametersBase <LightGbmBinaryModelParameters, PlattCalibrator> > onFit = null) { CheckUserValues(label, features, weights, numberOfLeaves, minimumExampleCountPerLeaf, learningRate, numberOfIterations, onFit); var rec = new TrainerEstimatorReconciler.BinaryClassifier( (env, labelName, featuresName, weightsName) => { var trainer = new LightGbmBinaryTrainer(env, labelName, featuresName, weightsName, numberOfLeaves, minimumExampleCountPerLeaf, learningRate, numberOfIterations); if (onFit != null) { return(trainer.WithOnFitDelegate(trans => onFit(trans.Model))); } else { return(trainer); } }, label, features, weights); return(rec.Output); }
/// <summary> /// Predict a target using a tree binary classification model trained with the <see cref="LightGbmBinaryTrainer"/>. /// </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 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> public static (Scalar <float> score, Scalar <float> probability, Scalar <bool> predictedLabel) LightGbm(this BinaryClassificationCatalog.BinaryClassificationTrainers catalog, Scalar <bool> label, Vector <float> features, Scalar <float> weights, Options options, Action <CalibratedModelParametersBase <LightGbmBinaryModelParameters, PlattCalibrator> > onFit = null) { CheckUserValues(label, features, weights, options, onFit); var rec = new TrainerEstimatorReconciler.BinaryClassifier( (env, labelName, featuresName, weightsName) => { options.LabelColumnName = labelName; options.FeatureColumnName = featuresName; options.ExampleWeightColumnName = weightsName; var trainer = new LightGbmBinaryTrainer(env, options); if (onFit != null) { return(trainer.WithOnFitDelegate(trans => onFit(trans.Model))); } else { return(trainer); } }, label, features, weights); return(rec.Output); }
/// <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{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 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) { 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); }
/// <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"); Contracts.CheckParam(!(l1Threshold < 0), nameof(l1Threshold), "Must not be negative"); 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); }