/// <summary> /// Iterative random selection of ensemble models. /// </summary> /// <param name="metric">Metric to minimize</param> /// <param name="ensembleStrategy">Strategy for ensembling models</param> /// <param name="numberOfModelsToSelect">Number of models to select</param> /// <param name="iterations">Number of iterations to try random selection</param> /// <param name="selectWithReplacement">If true the same model can be selected multiple times. /// This will correspond to weighting the models. If false each model can only be selected once</param> /// <param name="seed"></param> public RandomClassificationEnsembleSelection(IMetric <double, ProbabilityPrediction> metric, IClassificationEnsembleStrategy ensembleStrategy, int numberOfModelsToSelect, int iterations, bool selectWithReplacement, int seed = 42) { if (metric == null) { throw new ArgumentNullException("metric"); } if (ensembleStrategy == null) { throw new ArgumentNullException("ensembleStrategy"); } if (numberOfModelsToSelect < 1) { throw new ArgumentException("numberOfModelsToSelect must be at least 1"); } if (iterations < 1) { throw new ArgumentException("Number of iterations"); } m_metric = metric; m_ensembleStrategy = ensembleStrategy; m_numberOfModelsToSelect = numberOfModelsToSelect; m_selectWithReplacement = selectWithReplacement; m_iterations = iterations; m_random = new Random(seed); }
/// <summary> /// Greedy forward selection of ensemble models. /// </summary> /// <param name="metric">Metric to minimize</param> /// <param name="ensembleStrategy">Strategy for ensembling models</param> /// <param name="numberOfModelsToSelect">Number of models to select</param> /// <param name="numberOfModelsFromStart">Number of models from start of the search. /// The top n models will be selected based in their solo performance</param> /// <param name="selectWithReplacement">If true the same model can be selected multiple times. /// This will correspond to weighting the models. If false each model can only be selected once</param> public ForwardSearchClassificationEnsembleSelection(IMetric <double, ProbabilityPrediction> metric, IClassificationEnsembleStrategy ensembleStrategy, int numberOfModelsToSelect, int numberOfModelsFromStart, bool selectWithReplacement) { if (metric == null) { throw new ArgumentNullException("metric"); } if (ensembleStrategy == null) { throw new ArgumentNullException("ensembleStrategy"); } if (numberOfModelsToSelect < 1) { throw new ArgumentException("numberOfModelsToSelect must be at least 1"); } if (numberOfModelsFromStart < 1) { throw new ArgumentException("numberOfModelsFromStart must be at least 1"); } if (numberOfModelsFromStart > numberOfModelsToSelect) { throw new ArgumentException("numberOfModelsFromStart must be smaller than numberOfModelsToSelect"); } m_metric = metric; m_ensembleStrategy = ensembleStrategy; m_numberOfModelsToSelect = numberOfModelsToSelect; m_numberOfModelsFromStart = numberOfModelsFromStart; m_selectWithReplacement = selectWithReplacement; }
/// <summary> /// Classification model selecting EnsembleLearner. /// Trains several models and selects the best subset of models for the ensemble. /// The selection of the best set of models is based on cross validation. /// http://www.cs.cornell.edu/~alexn/papers/shotgun.icml04.revised.rev2.pdf /// </summary> /// <param name="learners">Learners in the ensemble</param> /// <param name="crossValidation">Cross validation method</param> /// <param name="ensembleStrategy">Strategy on how to combine the models</param> /// <param name="ensembleSelection">Ensemble selection method used to find the beset subset of models</param> public ClassificationModelSelectingEnsembleLearner( IIndexedLearner <ProbabilityPrediction>[] learners, ICrossValidation <ProbabilityPrediction> crossValidation, IClassificationEnsembleStrategy ensembleStrategy, IClassificationEnsembleSelection ensembleSelection) : this(learners, crossValidation, () => ensembleStrategy, ensembleSelection) { }
/// <summary> /// Classification ensemble learner. Combines several models into a single ensemble model. /// </summary> /// <param name="learners">Learners in the ensemble</param> /// <param name="ensembleStrategy">Strategy on how to combine the models</param> /// <param name="subSampleRatio">Default is 1.0. All models are trained on all data. /// If different from 1.0 models are trained using bagging with the chosen sub sample ratio</param> /// <param name="seed">Seed for the bagging when used</param> public ClassificationEnsembleLearner( IIndexedLearner <ProbabilityPrediction>[] learners, IClassificationEnsembleStrategy ensembleStrategy, double subSampleRatio = 1.0, int seed = 24) : this(learners.Select(l => new Func <F64Matrix, double[], int[], IPredictorModel <ProbabilityPrediction> >((o, t, i) => l.Learn(o, t, i))).ToArray(), () => ensembleStrategy, subSampleRatio, seed) { }
/// <summary> /// Classification model selecting EnsembleLearner. /// Trains several models and selects the best subset of models for the ensemble using greedy backward elimination. /// The selection of the best set of models is based on cross validation. /// http://www.cs.cornell.edu/~alexn/papers/shotgun.icml04.revised.rev2.pdf /// </summary> /// <param name="learners">Learners in the ensemble</param> /// <param name="numberOfModelsToSelect">Number of models to select</param> /// <param name="crossValidation">Cross validation method</param> /// <param name="ensembleStrategy">Strategy for ensembling models</param> /// <param name="metric">Metric to minimize</param> public ClassificationBackwardEliminationModelSelectingEnsembleLearner( IIndexedLearner <ProbabilityPrediction>[] learners, int numberOfModelsToSelect, ICrossValidation <ProbabilityPrediction> crossValidation, IClassificationEnsembleStrategy ensembleStrategy, IMetric <double, ProbabilityPrediction> metric) : base(learners, crossValidation, ensembleStrategy, new BackwardEliminationClassificationEnsembleSelection( metric, ensembleStrategy, numberOfModelsToSelect)) { }
/// <summary> /// Classification ensemble model /// </summary> /// <param name="ensembleModels">Models included in the ensemble</param> /// <param name="ensembleStrategy">Strategy on how to combine the models</param> public ClassificationEnsembleModel(IPredictorModel <ProbabilityPrediction>[] ensembleModels, IClassificationEnsembleStrategy ensembleStrategy) { if (ensembleModels == null) { throw new ArgumentNullException("ensembleModels"); } if (ensembleStrategy == null) { throw new ArgumentNullException("ensembleStrategy"); } m_ensembleModels = ensembleModels; m_ensembleStrategy = ensembleStrategy; }
/// <summary> /// Greedy backwards elimination of ensemble models. /// </summary> /// <param name="metric">Metric to minimize</param> /// <param name="ensembleStrategy">Strategy for ensembling models</param> /// <param name="numberOfModelsToSelect">Number of models to select</param> public BackwardEliminationClassificationEnsembleSelection( IMetric <double, ProbabilityPrediction> metric, IClassificationEnsembleStrategy ensembleStrategy, int numberOfModelsToSelect) { m_metric = metric ?? throw new ArgumentNullException(nameof(metric)); m_ensembleStrategy = ensembleStrategy ?? throw new ArgumentNullException(nameof(ensembleStrategy)); if (numberOfModelsToSelect < 1) { throw new ArgumentException("numberOfModelsToSelect must be at least 1"); } m_numberOfModelsToSelect = numberOfModelsToSelect; }
/// <summary> /// Classification model selecting EnsembleLearner. /// Trains several models and selects the best subset of models for the ensemble using greedy forward selection. /// The selection of the best set of models is based on cross validation. /// http://www.cs.cornell.edu/~alexn/papers/shotgun.icml04.revised.rev2.pdf /// </summary> /// <param name="learners">Learners in the ensemble</param> /// <param name="numberOfModelsToSelect">Number of models to select</param> /// <param name="crossValidation">Cross validation method</param> /// <param name="ensembleStrategy">Strategy for ensembling models</param> /// <param name="metric">Metric to minimize</param> /// <param name="numberOfModelsFromStart">Number of models from start of the search. /// The top n models will be selected based in their solo performance</param> /// <param name="selectWithReplacement">If true the same model can be selected multiple times. /// This will correspond to weighting the models. If false each model can only be selected once. Default is true</param> public ClassificationForwardSearchModelSelectingEnsembleLearner( IIndexedLearner <ProbabilityPrediction>[] learners, int numberOfModelsToSelect, ICrossValidation <ProbabilityPrediction> crossValidation, IClassificationEnsembleStrategy ensembleStrategy, IMetric <double, ProbabilityPrediction> metric, int numberOfModelsFromStart = 1, bool selectWithReplacement = true) : base(learners, crossValidation, ensembleStrategy, new ForwardSearchClassificationEnsembleSelection( metric, ensembleStrategy, numberOfModelsToSelect, numberOfModelsFromStart, selectWithReplacement)) { }
/// <summary> /// Classification model selecting EnsembleLearner. /// Trains several models and selects the best subset of models for the ensemble using iterative random selection. /// The selection of the best set of models is based on cross validation. /// http://www.cs.cornell.edu/~alexn/papers/shotgun.icml04.revised.rev2.pdf /// </summary> /// <param name="learners">Learners in the ensemble</param> /// <param name="numberOfModelsToSelect">Number of models to select</param> /// <param name="crossValidation">Cross validation method</param> /// <param name="ensembleStrategy">Strategy for ensembling models</param> /// <param name="metric">Metric to minimize</param> /// <param name="iterations">Number of iterations to random select model combinations.</param> /// <param name="selectWithReplacement">If true the same model can be selected multiple times.</param> /// <param name="seed"></param> public ClassificationRandomModelSelectingEnsembleLearner( IIndexedLearner <ProbabilityPrediction>[] learners, int numberOfModelsToSelect, ICrossValidation <ProbabilityPrediction> crossValidation, IClassificationEnsembleStrategy ensembleStrategy, IMetric <double, ProbabilityPrediction> metric, int iterations = 50, bool selectWithReplacement = true, int seed = 42) : base(learners, crossValidation, ensembleStrategy, new RandomClassificationEnsembleSelection( metric, ensembleStrategy, numberOfModelsToSelect, iterations, selectWithReplacement, seed)) { }
/// <summary> /// Classification ensemble model /// </summary> /// <param name="ensembleModels">Models included in the ensemble</param> /// <param name="ensembleStrategy">Strategy on how to combine the models</param> public ClassificationEnsembleModel(IPredictorModel <ProbabilityPrediction>[] ensembleModels, IClassificationEnsembleStrategy ensembleStrategy) { m_ensembleModels = ensembleModels ?? throw new ArgumentNullException(nameof(ensembleModels)); m_ensembleStrategy = ensembleStrategy ?? throw new ArgumentNullException(nameof(ensembleStrategy)); }