UFJF - Machine Learning Toolkit  0.51.8
PrimalClassifier.hpp
Go to the documentation of this file.
1 
6 #ifndef PRIMALCLASSIFIER__HPP
7 #define PRIMALCLASSIFIER__HPP
8 
9 #include "Classifier.hpp"
10 
11 namespace mltk{
12  namespace classifier {
13  template<typename T>
14  class PrimalClassifier : virtual public Classifier<T> {
15  // Attributes
16  protected :
18  std::vector<double> w;
20  double q = 2, p = 2;
21 
22  protected:
24  double flexible = 0.0;
26  double alpha_aprox = 0.0;
27  public:
28 
30  PrimalClassifier<T>(DataPointer<T> samples) : Classifier<T>(samples) {}
31  PrimalClassifier<T>(mltk::Data<T>& samples) {
32  this->samples = mltk::make_data<T>(samples);
33  }
34  PrimalClassifier<T>(const PrimalClassifier<T> &primal_learner)
35  : Classifier<T>(primal_learner) {
36  this->q = primal_learner.q;
37  this->p = primal_learner.p;
38  this->flexible = primal_learner.flexible;
39  this->alpha_aprox = primal_learner.alpha_aprox;
40  }
41 
42  double evaluate(const Point<T> &p, bool raw_value = false) override {
43  double func = 0.0;
44  size_t i, dim = this->solution.w.size();
45 
46  if (p.X().size() != dim) {
47  std::cerr << "The point must have the same dimension of the feature set! (" << p.X().size()
48  << ", " << dim << ")" << std::endl;
49  return 0;
50  }
51 
52  for (func = this->solution.bias, i = 0; i < dim; i++) {
53  func += this->solution.w[i] * p[i];
54  }
55 
56  return (func >= 0) ? 1 : -1;
57  }
58 
59  /*********************************************
60  * Getters *
61  *********************************************/
62 
63  std::string getFormulationString() override { return "Primal"; }
64 
69  double getP() const { return p; }
70 
75  double getQ() const { return q; }
76 
77  /*********************************************
78  * Setters *
79  *********************************************/
80 
85  void setqNorm(double q) { this->q = q; }
86 
91  void setpNorm(double p) { this->p = p; }
92 
97  void setFlexible(double flexible) { this->flexible = flexible; }
98 
103  void setAlphaAprox(double alpha_aprox) { this->alpha_aprox = alpha_aprox; }
104  };
105  }
106 }
107 #endif
std::shared_ptr< Data< T > > samples
Samples used in the model training.
Definition: Learner.hpp:21
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
double bias
Bias of the solution.
Definition: Solution.hpp:23
mltk::Point< double > w
Weights vector.
Definition: Solution.hpp:17
Definition: classifier/Classifier.hpp:17
Solution solution
Classifier solution.
Definition: classifier/Classifier.hpp:25
Definition: PrimalClassifier.hpp:14
double getQ() const
GetQ Return the value of the q norm.
Definition: PrimalClassifier.hpp:75
void setpNorm(double p)
setpNorm Set the p norm used by the classifier. (Euclidean norm is the default)
Definition: PrimalClassifier.hpp:91
void setqNorm(double q)
setqNorm Set the q norm used by the classifier. (Euclidean norm is the default)
Definition: PrimalClassifier.hpp:85
double q
Norm used in the classification. (Euclidean Norm is the default)
Definition: PrimalClassifier.hpp:20
double evaluate(const Point< T > &p, bool raw_value=false) override
Returns the class of a feature point based on the trained Learner.
Definition: PrimalClassifier.hpp:42
void setAlphaAprox(double alpha_aprox)
Set the percentage of the aproximation.
Definition: PrimalClassifier.hpp:103
std::string getFormulationString() override
getFormulationString Returns a string that represents the formulation of the learner (Primal or Dual)...
Definition: PrimalClassifier.hpp:63
void setFlexible(double flexible)
Set flexibity of the classifier.
Definition: PrimalClassifier.hpp:97
double getP() const
GetP Return the value of the p norm.
Definition: PrimalClassifier.hpp:69
double alpha_aprox
Percentage of aproximation of the result.
Definition: PrimalClassifier.hpp:26
std::vector< double > w
Weights vector.
Definition: PrimalClassifier.hpp:18
double flexible
Flexibility.
Definition: PrimalClassifier.hpp:24
UFJF-MLTK main namespace for core functionalities.
Definition: classifier/Classifier.hpp:11