3 SVMNonLin::SVMNonLin(
string className) {
5 this->className=className;
10 SVMNonLin::~SVMNonLin()
14 void SVMNonLin::trainModel(std::vector<std::vector<double> > &features, vector<double> &labels, svm_parameter ¶m) {
17 SVMProblem.l=features.size();
19 SVMProblem.y = Malloc(
double,SVMProblem.l);
20 SVMProblem.x = Malloc(
struct svm_node *,SVMProblem.l);
23 for (
int i=0; i<features.size(); i++)
25 SVMProblem.y[i]=labels[i];
26 vector<double> x=features[i];
28 for (
int j=0; j<x.size(); j++)
35 SVMProblem.x[i]=Malloc(
struct svm_node,sparsity+1);
37 for (
int j=0; j<x.size(); j++)
42 SVMProblem.x[i][cnt].index=j+1;
43 SVMProblem.x[i][cnt].value=value;
48 SVMProblem.x[i][cnt].index=-1;
54 modelSVM=svm_train(&SVMProblem, ¶m);
64 void SVMNonLin::freeModel()
66 svm_free_and_destroy_model(&modelSVM);
69 void SVMNonLin::saveModel(
string pathFile)
71 svm_save_model(pathFile.c_str(), modelSVM);
74 void SVMNonLin::loadModel(
string pathFile)
76 modelSVM=svm_load_model(pathFile.c_str());
81 svm_parameter SVMNonLin::initialiseParam(
int solverTYPE,
double C,
double eps,
int kernelType,
double gamma)
84 param.svm_type=solverTYPE;
88 param.kernel_type=kernelType;
95 double SVMNonLin::predictModel(vector<double> features)
100 fprintf(stdout,
"Error, Train Model First \n");
103 int nr_class=svm_get_nr_class(modelSVM);
106 for (
int i=0; i<features.size(); i++)
110 svm_node *x=Malloc(
struct svm_node,sparsity+1);
113 for (
int i=0; i<features.size(); i++)
118 x[cnt].value=features[i];
125 svm_predict_values(modelSVM,x,&val);
131 vector<vector<double> > SVMNonLin::readFeatures(
string filePath)
133 vector<vector<double> > featuresMat;
137 infile.open (filePath.c_str());
138 while(!infile.eof() && infile.is_open())
141 getline(infile,line);
143 char * val= strtok((
char*) line.c_str(),
" ");
148 double value=atof(val);
150 val=strtok(NULL,
" ");
153 featuresMat.push_back(f);