3 #include "Ensemble.hpp"
8 #include <ufjfmltk/core/Learner.hpp>
16 std::string voting_type;
21 template<
class... Learners>
23 Learners... weak_learners): voting_type(voting_type) {
24 this->samples = std::make_shared<Data<T> >(
samples);
25 this->
m_learners = {std::make_shared<Learners>(std::forward<Learners>(weak_learners))...};
30 for (
size_t i = 0; i < this->
m_learners.size(); i++) {
32 this->
m_learners[i]->setSamples(this->samples);
39 auto _classes = this->samples->
classes();
41 if (voting_type ==
"soft") {
42 assert(this->weights.
size() > 0);
46 for (
size_t i = 0; i < this->
m_learners.size(); i++) {
49 size_t pred_pos = std::find_if(_classes.begin(), _classes.end(), [&pred](
const auto &a) {
51 }) - _classes.begin();
53 votes[pred_pos] += this->weights[i];
55 size_t max_votes = std::max_element(votes.
X().begin(), votes.
X().end()) - votes.
X().begin();
56 return _classes[max_votes];
59 void setWeights(
const std::vector<double> weights) {
60 assert(weights.size() == this->m_learners.size());
61 this->weights.X().resize(weights.size());
62 this->weights = weights;
67 void setVotingType(
const std::string& vote_type){ this->voting_type = vote_type; }
69 void setLearners(std::vector<LearnerPointer<T>>&
m_learners){
79 this->weights = voter.weights;
const std::vector< int > classes() const
Returns a vector containing the numeric values of the classes.
Definition: Data.hpp:1831
std::shared_ptr< Data< T > > samples
Samples used in the model training.
Definition: Learner.hpp:21
size_t seed
seed for random operations.
Definition: Learner.hpp:46
Rep const & X() const
Returns the attributes representation of the point (std::vector by default).
Definition: Point.hpp:139
std::size_t size() const
Returns the dimension of the point.
Definition: Point.hpp:133
Definition: classifier/Classifier.hpp:17
Namespace for ensemble methods.
Definition: ensemble/Ensemble.hpp:16
std::vector< LearnerPointer< T > > m_learners
Pointer to base learner used by the ensemble method.
Definition: ensemble/Ensemble.hpp:22
Definition: VotingClassifier.hpp:13
bool train() override
Function that execute the training phase of a Learner.
Definition: VotingClassifier.hpp:28
double evaluate(const Point< T > &p, bool raw_value=false) override
Returns the class of a feature point based on the trained Learner.
Definition: VotingClassifier.hpp:38
std::string getFormulationString() override
getFormulationString Returns a string that represents the formulation of the learner (Primal or Dual)...
Definition: VotingClassifier.hpp:73
UFJF-MLTK main namespace for core functionalities.
Definition: classifier/Classifier.hpp:11