UFJF - Machine Learning Toolkit  0.51.8
VotingClassifier.hpp
1 #pragma once
2 
3 #include "Ensemble.hpp"
5 #include <memory>
6 #include <vector>
7 #include <string>
8 #include <ufjfmltk/core/Learner.hpp>
9 
10 namespace mltk{
11  namespace ensemble {
12  template<typename T>
13  class VotingClassifier : public Ensemble<T>, public classifier::Classifier<T> {
14  private:
15  mltk::Point<double> weights;
16  std::string voting_type;
17 
18  public:
19  VotingClassifier() = default;
20 
21  template<class... Learners>
22  VotingClassifier(Data<T> &samples, const std::string &voting_type,
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))...};
26  }
27 
28  bool train() override {
29  // train each one of the given weak learners
30  for (size_t i = 0; i < this->m_learners.size(); i++) {
31  this->m_learners[i]->setSeed(this->seed);
32  this->m_learners[i]->setSamples(this->samples);
33  this->m_learners[i]->train();
34  }
35  return true;
36  }
37 
38  double evaluate(const Point<T> &p, bool raw_value = false) override {
39  auto _classes = this->samples->classes();
40  mltk::Point<double> votes(_classes.size(), 0.0);
41  if (voting_type == "soft") {
42  assert(this->weights.size() > 0);
43  } else {
44  this->weights = Point<double>(this->m_learners.size(), 1);
45  }
46  for (size_t i = 0; i < this->m_learners.size(); i++) {
47  auto pred = this->m_learners[i]->evaluate(p);
48  // get prediction position
49  size_t pred_pos = std::find_if(_classes.begin(), _classes.end(), [&pred](const auto &a) {
50  return (a == pred);
51  }) - _classes.begin();
52  // count prediction as a vote
53  votes[pred_pos] += this->weights[i];
54  }
55  size_t max_votes = std::max_element(votes.X().begin(), votes.X().end()) - votes.X().begin();
56  return _classes[max_votes];
57  }
58 
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;
63  }
64 
65  Point<double> getWeights(){ return this->weights; }
66 
67  void setVotingType(const std::string& vote_type){ this->voting_type = vote_type; }
68 
69  void setLearners(std::vector<LearnerPointer<T>>& m_learners){
70  this->m_learners = m_learners;
71  }
72 
73  std::string getFormulationString() override {
74  return "Primal";
75  }
76 
77  VotingClassifier<T>& operator=(VotingClassifier<T> const& voter){
78  this->m_learners = voter.m_learners;
79  this->weights = voter.weights;
80  this->samples = voter.samples;
81  }
82  };
83  }
84 }
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