Пример #1
0
        public static PredictionEngine <TIn, TOut> SdcaLogisticRegression <TIn, TOut>(
            IEnumerable <TIn> trainDataset,
            string outputColumnName                   = "PredictedLabel",
            string exampleWeightColumnName            = null,
            float?l1Regularization                    = null,
            float?l2Regularization                    = null,
            int?maximumNumberOfIterations             = null,
            Action <ITransformer> additionModelAction = null)
            where TIn : class, new()
            where TOut : class, new()
        {
            var context         = new MLContext();
            var type            = typeof(TIn);
            var labelColumnName = Preprocessing.LabelColumn(type.GetProperties()).Name;
            var properties      = Preprocessing.ExcludeColumns(type.GetProperties());

            var preprocessor = context.OneHotEncoding(properties);

            var trainDataframe = context.Data.LoadFromEnumerable(trainDataset);
            var pipeline       = context.Transforms.Concatenate("Features", preprocessor.CombinedFeatures.ToArray())
                                 .Append(preprocessor.OneHotEncodingEstimator)
                                 .AppendCacheCheckpoint(context)
                                 .Append(context.BinaryClassification.Trainers.SdcaLogisticRegression(
                                             labelColumnName: labelColumnName,
                                             featureColumnName: "Features",
                                             exampleWeightColumnName: exampleWeightColumnName,
                                             l1Regularization: l1Regularization,
                                             l2Regularization: l2Regularization,
                                             maximumNumberOfIterations: maximumNumberOfIterations
                                             ));

            var model         = pipeline.Fit(trainDataframe);
            var predictEngine = context.Model.CreatePredictionEngine <TIn, TOut>(model);

            additionModelAction?.Invoke(model);
            return(predictEngine);
        }
Пример #2
0
        /// <summary>
        /// Create engine of Limited-Memory Broyden–Fletcher–Goldfarb–Shanno algorithm using training dataset and hyperparameters
        /// </summary>
        /// <typeparam name="TIn"></typeparam>
        /// <typeparam name="TOut"></typeparam>
        /// <param name="trainDataset">Enumerable of TIn type.</param>
        /// <param name="labelColumnName">The name of the label column.</param>
        /// <param name="outputColumnName">The name of the feature column.</param>
        /// <param name="exampleWeightColumnName">The name of the example weight column.</param>
        /// <param name="l1Regularization">Weight of L1 regularization term.</param>
        /// <param name="l2Regularization">Weight of L2 regularization term.</param>
        /// <param name="optimizationTolerance">Threshold for optimizer convergence.</param>
        /// <param name="historySize">Memory size. Low=faster, less accurate.</param>
        /// <param name="enforceNonNegativity">Enforce non-negative weights.</param>
        /// <returns></returns>
        public static PredictionEngine <TIn, TOut> LbfgsMaximumEntropy <TIn, TOut>(IEnumerable <TIn> trainDataset, string outputColumnName = "PredictedLabel", string exampleWeightColumnName = null, float l1Regularization = 1, float l2Regularization = 1, double optimizationTolerance = 1e-07, int historySize = 20, bool enforceNonNegativity = false, Action <ITransformer> additionModelAction = null)
            where TIn : class, new()
            where TOut : class, new()
        {
            var context         = new MLContext();
            var type            = typeof(TIn);
            var labelColumnName = Preprocessing.LabelColumn(type.GetProperties()).Name;
            var properties      = Preprocessing.ExcludeColumns(type.GetProperties());

            var preprocessor = context.OneHotEncoding(properties);

            var trainDataframe = context.Data.LoadFromEnumerable(trainDataset);
            var pipeline       = context.Transforms.Conversion.MapValueToKey(labelColumnName)
                                 .Append(preprocessor.OneHotEncodingEstimator)
                                 .Append(context.Transforms.Concatenate("Features", preprocessor.CombinedFeatures.ToArray()))
                                 .Append(context.Transforms.ProjectToPrincipalComponents(outputColumnName: "PCAFeatures", inputColumnName: "Features", rank: 2))
                                 .AppendCacheCheckpoint(context)
                                 .Append(context.MulticlassClassification.Trainers.LbfgsMaximumEntropy(
                                             labelColumnName: labelColumnName,
                                             featureColumnName: "Features",
                                             exampleWeightColumnName: exampleWeightColumnName,
                                             l1Regularization: l1Regularization,
                                             l2Regularization: l2Regularization,
                                             optimizationTolerance: (float)optimizationTolerance,
                                             historySize: historySize,
                                             enforceNonNegativity: enforceNonNegativity
                                             )
                                         )
                                 .Append(context.Transforms.Conversion.MapKeyToValue(outputColumnName));


            var model         = pipeline.Fit(trainDataframe);
            var predictEngine = context.Model.CreatePredictionEngine <TIn, TOut>(model);

            additionModelAction?.Invoke(model);
            return(predictEngine);
        }
Пример #3
0
        public static PredictionEngine <TIn, TOut> FastForest <TIn, TOut>(
            IEnumerable <TIn> trainDataset,
            string exampleWeightColumnName = null,
            int numberOfLeaves             = 20,
            int numberOfTrees = 100,
            int minimumExampleCountPerLeaft           = 10,
            Action <ITransformer> additionModelAction = null)
            where TIn : class, new()
            where TOut : class, new()
        {
            var context         = new MLContext();
            var type            = typeof(TIn);
            var labelColumnName = Preprocessing.LabelColumn(type.GetProperties()).Name;
            var properties      = Preprocessing.ExcludeColumns(type.GetProperties());

            var preprocessor = context.OneHotEncoding(properties);

            var trainDataframe = context.Data.LoadFromEnumerable(trainDataset);
            var pipeline       = context.Transforms.Concatenate("Features", preprocessor.CombinedFeatures.ToArray())
                                 .Append(preprocessor.OneHotEncodingEstimator)
                                 .AppendCacheCheckpoint(context)
                                 .Append(context.BinaryClassification.Trainers.FastForest(
                                             labelColumnName: labelColumnName,
                                             featureColumnName: "Features",
                                             exampleWeightColumnName: exampleWeightColumnName,
                                             numberOfLeaves: numberOfLeaves,
                                             numberOfTrees: numberOfTrees,
                                             minimumExampleCountPerLeaf: minimumExampleCountPerLeaft
                                             ));

            var model         = pipeline.Fit(trainDataframe);
            var predictEngine = context.Model.CreatePredictionEngine <TIn, TOut>(model);

            additionModelAction?.Invoke(model);
            return(predictEngine);
        }