/
ofxSvm.h
81 lines (61 loc) · 2.8 KB
/
ofxSvm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once
#include "ofMain.h"
#include "svm.h"
class ofxSvm
{
public:
class Data
{
friend class ofxSvm;
multimap<double, vector<double> > mData;
int mDimension;
struct ScaleParameter
{
bool isEnable;
double x_lower, x_upper, y_lower, y_upper;
double y_min, y_max;
vector<double> feature_max, feature_min;
};
struct ScaleParameter mScaleParameter;
void checkDimension(int length);
public:
Data();
int add(double label, vector<double>& vec);
int add(double label, double* vec, int length);
void clear();
bool scale(double lower, double upper, double y_lower = 0, double y_upper = 0);
bool hasScaleParameter() { return mScaleParameter.isEnable; }
};
// including namespace
enum { C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR }; /* svm_type */
enum { LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED }; /* kernel_type */
public:
ofxSvm();
virtual ~ofxSvm();
void train(const Data& data);
vector<double> predict(const Data& data);
void saveModel(const string& filename);
void loadModel(const string& filename);
void clear();
vector<int> getSupportVectorIndex();
inline void setSvmType(const int type) { mParam.svm_type = type; }
inline void setKernelType(const int type) { mParam.kernel_type = type; }
inline void setDegree(const double v) { mParam.degree = v; }
inline void setGamma(const double v) { mParam.gamma = v; }
inline void setCoef0(const double v) { mParam.coef0 = v; }
inline void setCost(const double v) { mParam.C = v; }
inline void setNu(const double v) { mParam.nu = v; }
inline void setCachssize(const double v) { mParam.cache_size = v; }
inline void setEpsilon(const double v) { mParam.eps = v; }
inline void setP(const double v) { mParam.p = v; }
inline void setNrWeight(const int v) { mParam.nr_weight = v; }
inline void setShrinking(const bool v) { mParam.shrinking = v ? 1 : 0; }
inline void setProbability(const bool v) { mParam.probability = v ? 1 : 0; }
inline void setWeightLabel(int* v) { mParam.weight_label = v; }
inline void setWeight(double* v) { mParam.weight = v; }
void defaultParams();
protected:
svm_parameter mParam;
shared_ptr<svm_model> mModel;
static void printStdOut(const char *s);
};