himrep
SVMNonLin.cpp
1 #include "SVMNonLin.h"
2 
3 SVMNonLin::SVMNonLin(string className) {
4 
5  this->className=className;
6  modelSVM=NULL;
7 
8 
9 }
10 SVMNonLin::~SVMNonLin()
11 {
12 }
13 
14 void SVMNonLin::trainModel(std::vector<std::vector<double> > &features, vector<double> &labels, svm_parameter &param) {
15 
16 
17  SVMProblem.l=features.size();
18 
19  SVMProblem.y = Malloc(double,SVMProblem.l);
20  SVMProblem.x = Malloc(struct svm_node *,SVMProblem.l);
21 
22 
23  for (int i=0; i<features.size(); i++)
24  {
25  SVMProblem.y[i]=labels[i];
26  vector<double> x=features[i];
27  int sparsity=0;
28  for (int j=0; j<x.size(); j++)
29  {
30  double value=x[j];
31  if(value!=0)
32  sparsity++;
33  }
34 
35  SVMProblem.x[i]=Malloc(struct svm_node,sparsity+1); //-1 index
36  int cnt=0;
37  for (int j=0; j<x.size(); j++)
38  {
39  double value=x[j];
40  if(value==0)
41  continue;
42  SVMProblem.x[i][cnt].index=j+1;
43  SVMProblem.x[i][cnt].value=value;
44  cnt++;
45  }
46 
47 
48  SVMProblem.x[i][cnt].index=-1;
49 
50 
51  }
52 
53 
54  modelSVM=svm_train(&SVMProblem, &param);
55 
56 
57  /*free(SVMProblem.y);
58  for (int i=0; i<SVMProblem.l; i++)
59  free(SVMProblem.x[i]);
60  free(SVMProblem.x);*/
61 
62 }
63 
64 void SVMNonLin::freeModel()
65 {
66  svm_free_and_destroy_model(&modelSVM);
67 }
68 
69 void SVMNonLin::saveModel(string pathFile)
70 {
71  svm_save_model(pathFile.c_str(), modelSVM);
72 }
73 
74 void SVMNonLin::loadModel(string pathFile)
75 {
76  modelSVM=svm_load_model(pathFile.c_str());
77 }
78 
79 
80 
81 svm_parameter SVMNonLin::initialiseParam(int solverTYPE, double C, double eps, int kernelType, double gamma)
82 {
83  svm_parameter param;
84  param.svm_type=solverTYPE;
85  param.C=C;
86  param.eps=eps;
87  param.nr_weight=0;
88  param.kernel_type=kernelType;
89  param.gamma=gamma;
90 
91  return param;
92 
93 }
94 
95 double SVMNonLin::predictModel(vector<double> features)
96 {
97 
98  if(modelSVM==NULL)
99  {
100  fprintf(stdout,"Error, Train Model First \n");
101  return 0.0;
102  }
103  int nr_class=svm_get_nr_class(modelSVM);
104 
105  int sparsity=0.0;
106  for (int i=0; i<features.size(); i++)
107  if(features[i]!=0.0)
108  sparsity++;
109 
110  svm_node *x=Malloc(struct svm_node,sparsity+1); //bias and -1 index
111 
112  int cnt=0;
113  for (int i=0; i<features.size(); i++)
114  {
115  if(features[i]!=0.0)
116  {
117  x[cnt].index=i+1;
118  x[cnt].value=features[i];
119  cnt++;
120  }
121  }
122  x[cnt].index=-1;
123 
124  double val=0;
125  svm_predict_values(modelSVM,x,&val);
126  return val;
127 
128 }
129 
130 
131 vector<vector<double> > SVMNonLin::readFeatures(string filePath)
132 {
133  vector<vector<double> > featuresMat;
134 
135  string line;
136  ifstream infile;
137  infile.open (filePath.c_str());
138  while(!infile.eof() && infile.is_open()) // To get you all the lines.
139  {
140  vector<double> f;
141  getline(infile,line); // Saves the line in STRING.
142 
143  char * val= strtok((char*) line.c_str()," ");
144 
145  while(val!=NULL)
146  {
147 
148  double value=atof(val);
149  f.push_back(value);
150  val=strtok(NULL," ");
151  }
152  if(f.size()>0)
153  featuresMat.push_back(f);
154  }
155  infile.close();
156 
157 
158  return featuresMat;
159 }
160 
161 
162