RapidLib  v2.2.0
A simple library for interactive machine learning
svmClassification.h
Go to the documentation of this file.
1 
10 #ifndef SVMCLASSIFICATION_H
11 #define SVMCLASSIFICATION_H
12 
13 #include <vector>
14 #include "baseModel.h"
15 #include "../dependencies/libsvm/libsvm.h"
16 
17 template<typename T>
18 class svmClassification final : public baseModel<T>
19 {
20 public:
23 
44  KernelType kernelType = LINEAR_KERNEL,
45  SVMType svmType = C_SVC,
46  bool useScaling = true,
47  bool useNullRejection = false,
48  bool useAutoGamma = true,
49  float gamma = 0.1,
50  unsigned int degree = 3,
51  float coef0 = 0,
52  float nu = 0.5,
53  float C = 1,
54  bool useCrossValidation = false,
55  unsigned int kFoldValue = 10
56  );
57 
58  svmClassification(int numInputs);
59 
62 
68  void train(const std::vector<trainingExampleTemplate<T> > &trainingSet) override;
69 
76  void train(const std::vector<trainingExampleTemplate<T> >& trainingSet, const std::size_t whichOutput) override;
77 
82  T run(const std::vector<T> &inputVector) override;
83 
84  void reset() override;
85 
103  bool init(KernelType kernelType,SVMType svmType,bool useScaling,bool useNullRejection,bool useAutoGamma,
104  float gamma,
105  unsigned int degree,
106  float coef0,
107  float nu,
108  float C,
109  bool useCrossValidation,
110  unsigned int kFoldValue
111  );
112 
113  size_t getNumInputs() const override;
114  std::vector<size_t> getWhichInputs() const override;
115 
116 #ifndef EMSCRIPTEN
117  void getJSONDescription(Json::Value &currentModel) override;
118 #endif
119 
120 private:
121  bool problemSet;
122  struct LIBSVM::svm_model *model;
123  struct LIBSVM::svm_parameter param;
124  struct LIBSVM::svm_problem problem;
125 
126  int numInputs;
127 
129  std::vector<double> inRanges;
130  std::vector<double> inBases;
131 
132  bool trained;
133 };
134 
135 #endif
Definition: baseModel.h:42
Definition: svmClassification.h:19
T run(const std::vector< T > &inputVector) override
Definition: svmClassification.cpp:231
void train(const std::vector< trainingExampleTemplate< T > > &trainingSet) override
Definition: svmClassification.cpp:172
std::vector< size_t > getWhichInputs() const override
Definition: svmClassification.cpp:259
~svmClassification()
Definition: svmClassification.cpp:103
svmClassification(KernelType kernelType=LINEAR_KERNEL, SVMType svmType=C_SVC, bool useScaling=true, bool useNullRejection=false, bool useAutoGamma=true, float gamma=0.1, unsigned int degree=3, float coef0=0, float nu=0.5, float C=1, bool useCrossValidation=false, unsigned int kFoldValue=10)
Definition: svmClassification.cpp:17
bool init(KernelType kernelType, SVMType svmType, bool useScaling, bool useNullRejection, bool useAutoGamma, float gamma, unsigned int degree, float coef0, float nu, float C, bool useCrossValidation, unsigned int kFoldValue)
Definition: svmClassification.cpp:113
SVMType
Definition: svmClassification.h:21
@ C_SVC
Definition: svmClassification.h:21
@ NU_SVR
Definition: svmClassification.h:21
@ ONE_CLASS
Definition: svmClassification.h:21
@ NU_SVC
Definition: svmClassification.h:21
@ EPSILON_SVR
Definition: svmClassification.h:21
void reset() override
Definition: svmClassification.cpp:108
KernelType
Definition: svmClassification.h:22
@ LINEAR_KERNEL
Definition: svmClassification.h:22
@ PRECOMPUTED_KERNEL
Definition: svmClassification.h:22
@ SIGMOID_KERNEL
Definition: svmClassification.h:22
@ RBF_KERNEL
Definition: svmClassification.h:22
@ POLY_KERNEL
Definition: svmClassification.h:22
void getJSONDescription(Json::Value &currentModel) override
Definition: svmClassification.cpp:266
size_t getNumInputs() const override
Definition: svmClassification.cpp:254
Definition: trainingExample.h:19