5 #ifndef UFJF_MLTK_ONEVSONE_HPP
6 #define UFJF_MLTK_ONEVSONE_HPP
11 #include "ufjfmltk/core/Sampling.hpp"
14 namespace classifier {
21 using LearnerPointer = std::shared_ptr<Learner<T> >;
23 std::vector<std::vector<LearnerPointer> > base_learners;
30 template<
template<
typename>
class ClassifierType>
33 this->samples = mltk::make_data<double>(
samples);
35 this->samp_method = samp_method;
37 auto classes = this->samples->
classes();
39 if (base_learners.size() == 0) {
40 base_learners.resize(classes.size());
42 for (
size_t i = 0; i < classes.size(); ++i) {
43 base_learners[i].resize(classes.size());
45 for (
size_t j = 0; j < base_learners[i].size(); ++j) {
46 if (classes[i] != classes[j]) {
47 base_learners[i][j] = std::make_shared<ClassifierType<T> >(classifier);
54 template<
template<
typename>
class ClassifierType>
57 this->samples = classifier.getSamples();
59 this->samp_method = samp_method;
61 auto classes = this->samples->
classes();
63 if (base_learners.size() == 0) {
64 base_learners.resize(classes.size());
66 for (
size_t i = 0; i < classes.size(); ++i) {
67 base_learners[i].resize(classes.size());
69 for (
size_t j = 0; j < base_learners[i].size(); ++j) {
70 if (classes[i] != classes[j]) {
71 base_learners[i][j] = std::make_shared<ClassifierType<T> >(classifier);
78 bool train()
override;
87 auto classes = this->samples->classes();
88 size_t n_classes = classes.size();
90 for (
size_t i = 0; i < n_classes; ++i) {
91 for (
size_t j = 0; j < n_classes; ++j) {
92 if (classes[i] != classes[j]) {
94 auto learner = base_learners[i][j];
95 std::vector<int> current_classes = {classes[i], classes[j]};
98 temp_samples.
classesCopy(*this->samples, current_classes);
101 for (
size_t k = 0; k < temp_samples.
size(); k++) {
102 temp_samples[k]->Y() = (temp_samples[k]->Y() == classes[i]) ? 1 : -1;
108 (*samp_method)(temp_samples);
112 learner->setSamples(temp_samples);
123 auto classes = this->samples->classes();
124 std::vector<size_t> class_votes(classes.size(), 0);
127 for (
size_t i = 0; i < base_learners.size(); ++i) {
128 for (
size_t j = 0; j < base_learners[i].size(); ++j) {
129 if (classes[i] != classes[j]) {
130 if (base_learners[i][j]->evaluate(p) == 1) {
139 auto max_index = std::max_element(class_votes.begin(), class_votes.end()) - class_votes.begin();
141 return classes[max_index];
146 return this->base_learners[0][1]->getFormulationString();
void classesCopy(const Data< T > &_data, std::vector< int > &classes)
Makes a deep copy from another data object.
Definition: Data.hpp:1520
size_t size() const
Returns the size of the dataset.
Definition: Data.hpp:208
const std::vector< int > classes() const
Returns a vector containing the numeric values of the classes.
Definition: Data.hpp:1831
void setClasses(const std::vector< int > &classes)
Set the classes to use in the dataset.
Definition: Data.hpp:1836
void computeClassesDistribution()
Compute the frequency of each class in the dataset.
Definition: Data.hpp:1843
std::shared_ptr< Data< T > > samples
Samples used in the model training.
Definition: Learner.hpp:21
int verbose
Verbose level of the output.
Definition: Learner.hpp:42
Base class for the implementation of over sampling methods.
Definition: Sampling.hpp:110
Definition: DualClassifier.hpp:16
Wrapper for the implementation of the one vs one multi class classification algorithm.
Definition: OneVsOne.hpp:19
std::string getFormulationString() override
getFormulationString Returns a string that represents the formulation of the learner (Primal or Dual)...
Definition: OneVsOne.hpp:145
double evaluate(const Point< T > &p, bool raw_value=false) override
Returns the class of a feature point based on the trained Learner.
Definition: OneVsOne.hpp:122
bool train() override
Function that execute the training phase of a Learner.
Definition: OneVsOne.hpp:86
Definition: PrimalClassifier.hpp:14
UFJF-MLTK main namespace for core functionalities.
Definition: classifier/Classifier.hpp:11