himrep
SVMLinear.cpp
1 #include "SVMLinear.h"
2 
3 SVMLinear::SVMLinear(string className) {
4 
5  this->className=className;
6  modelLinearSVM=NULL;
7 
8 
9 }
10 
11 void SVMLinear::trainModel(std::vector<std::vector<double> > &features, vector<double> &labels, parameter &param,int bias) {
12 
13 
14  SVMProblem.bias=bias;
15  SVMProblem.l=features.size();
16  SVMProblem.n=features[0].size()+bias; //+1 bias term
17 
18  SVMProblem.y = Malloc(double,SVMProblem.l);
19  SVMProblem.x = Malloc(struct feature_node *,SVMProblem.l);
20 
21 
22  for (int i=0; i<features.size(); i++)
23  {
24  SVMProblem.y[i]=labels[i];
25  vector<double> x=features[i];
26  int sparsity=0;
27  for (int j=0; j<x.size(); j++)
28  {
29  double value=x[j];
30  if(value!=0)
31  sparsity++;
32  }
33 
34  SVMProblem.x[i]=Malloc(struct feature_node,sparsity+bias+1); //bias and -1 index
35  int cnt=0;
36  for (int j=0; j<x.size(); j++)
37  {
38  double value=x[j];
39  if(value==0)
40  continue;
41  SVMProblem.x[i][cnt].index=j+1;
42  SVMProblem.x[i][cnt].value=value;
43  cnt++;
44  }
45 
46  if(bias)
47  {
48  SVMProblem.x[i][cnt].index=SVMProblem.n,
49  SVMProblem.x[i][cnt].value=1;
50  cnt++;
51  }
52  SVMProblem.x[i][cnt].index=-1;
53 
54 
55  }
56 
57 
58  modelLinearSVM=train(&SVMProblem, &param);
59 
60 
61  free(SVMProblem.y);
62  for (int i=0; i<SVMProblem.l; i++)
63  free(SVMProblem.x[i]);
64  free(SVMProblem.x);
65 
66  if(param.nr_weight>0)
67  {
68  free(param.weight);
69  free(param.weight_label);
70  }
71 
72 }
73 
74 
75 void SVMLinear::saveModel(string pathFile)
76 {
77  save_model(pathFile.c_str(), modelLinearSVM);
78 }
79 
80 void SVMLinear::loadModel(string pathFile)
81 {
82  modelLinearSVM=load_model(pathFile.c_str());
83 }
84 
85 
86 /*solver_type can be one of L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL, L2R_L2LOSS_SVR, L2R_L2LOSS_SVR_DUAL, L2R_L1LOSS_SVR_DUAL.
87 
88  L2R_LR L2-regularized logistic regression (primal)
89  L2R_L2LOSS_SVC_DUAL L2-regularized L2-loss support vector classification (dual)
90  L2R_L2LOSS_SVC L2-regularized L2-loss support vector classification (primal)
91  L2R_L1LOSS_SVC_DUAL L2-regularized L1-loss support vector classification (dual)
92  MCSVM_CS multi-class support vector classification by Crammer and Singer
93  L1R_L2LOSS_SVC L1-regularized L2-loss support vector classification
94  L1R_LR L1-regularized logistic regression
95  L2R_LR_DUAL L2-regularized logistic regression (dual)
96  L2R_L2LOSS_SVR L2-regularized L2-loss support vector regression (primal)
97  L2R_L2LOSS_SVR_DUAL L2-regularized L2-loss support vector regression (dual)
98  L2R_L1LOSS_SVR_DUAL L2-regularized L1-loss support vector regression (dual)
99 
100  C= contraints violation Default 1
101 
102  eps is the stopping criterion:
103 
104  L2R_LR and L2R_L2LOSS_SVC
105  |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,
106  where f is the primal function and pos/neg are # of
107  positive/negative data (default 0.01)
108 
109  L2R_L2LOSS_SVC_DUAL, L2R_L1LOSS_SVC_DUAL, MCSVM_CS and L2R_LR_DUAL
110 
111  Dual maximal violation <= eps; similar to libsvm (default 0.1)
112  L1R_L2LOSS_SVC and L1R_LR
113  |f'(w)|_1 <= eps*min(pos,neg)/l*|f'(w0)|_1,
114  where f is the primal function (default 0.01)
115 
116 
117  p is the sensitiveness of loss of support vector regression.
118  */
119 parameter SVMLinear::initialiseParam(int solverTYPE, double C, double eps, int nClass, int nr_Positive, int nr_Negative)
120 {
121  parameter param;
122  param.solver_type=solverTYPE;
123  param.C=C;
124  param.eps=eps;
125  if(nr_Negative==0 || nr_Negative<nr_Positive)
126  {
127  param.nr_weight=0;
128  return param;
129  }
130 
131  param.nr_weight=(nClass);
132  param.weight_label = Malloc(int,nClass);
133  param.weight= Malloc(double,nClass);
134 
135 
136  param.weight_label[0]=1;
137  param.weight[0]=nr_Negative/nr_Positive;
138 
139 
140  for (int i=1; i<nClass; i++)
141  {
142  param.weight_label[i]=-1;
143  param.weight[i]=1.0;
144  }
145 
146  return param;
147 
148 }
149 
150 double SVMLinear::predictModel(vector<double> features)
151 {
152 
153  if(modelLinearSVM==NULL)
154  {
155  fprintf(stdout,"Error, Train Model First \n");
156  return 0.0;
157  }
158  int nr_class=get_nr_class(modelLinearSVM);
159  int bias=modelLinearSVM->bias;
160 
161  int sparsity=0.0;
162  for (int i=0; i<features.size(); i++)
163  if(features[i]!=0.0)
164  sparsity++;
165 
166  feature_node *x=Malloc(struct feature_node,sparsity+bias+1); //bias and -1 index
167 
168  int cnt=0;
169  for (int i=0; i<features.size(); i++)
170  {
171  if(features[i]!=0.0)
172  {
173  x[cnt].index=i+1;
174  x[cnt].value=features[i];
175  cnt++;
176  }
177  }
178  if(bias)
179  {
180  x[cnt].index=modelLinearSVM->nr_feature+1,
181  x[cnt].value=1;
182  cnt++;
183  }
184  x[cnt].index=-1;
185 
186  double val=0;
187  predict_values(modelLinearSVM,x,&val);
188  free(x);
189  return val;
190 
191 }
192 
193 
194 vector<vector<double> > SVMLinear::readFeatures(string filePath)
195 {
196  vector<vector<double> > featuresMat;
197 
198  string line;
199  ifstream infile;
200  infile.open (filePath.c_str());
201  while(!infile.eof() && infile.is_open()) // To get you all the lines.
202  {
203  vector<double> f;
204  getline(infile,line); // Saves the line in STRING.
205 
206  char * val= strtok((char*) line.c_str()," ");
207 
208  while(val!=NULL)
209  {
210 
211  double value=atof(val);
212  f.push_back(value);
213  val=strtok(NULL," ");
214  }
215  if(f.size()>0)
216  featuresMat.push_back(f);
217  }
218  infile.close();
219 
220 
221  return featuresMat;
222 }
223 
224 
225