Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | List of all members
SVMClassifier Class Reference

#include <svm_classifier.h>

Collaboration diagram for SVMClassifier:
Collaboration graph
[legend]

Public Member Functions

 SVMClassifier ()=default
 
 SVMClassifier (const SVMClassifier &other)
 
 ~SVMClassifier ()
 
SVMClassifieroperator= (const SVMClassifier &other)
 
void SVMTrain (MultidimArray< double > &trainSet, MultidimArray< double > &lable)
 
double predict (MultidimArray< double > &featVec, double &score)
 
void SaveModel (const FileName &fnModel)
 
void LoadModel (const FileName &fnModel)
 
void setParameters (double c, double gamma)
 

Public Attributes

svm_parameter param
 
svm_problem prob
 
svm_modelmodel =nullptr
 

Detailed Description

SVM classifier class. This class use the SVMLIB Library in order to classify the data using the svm method

Definition at line 45 of file svm_classifier.h.

Constructor & Destructor Documentation

◆ SVMClassifier() [1/2]

SVMClassifier::SVMClassifier ( )
default

◆ SVMClassifier() [2/2]

SVMClassifier::SVMClassifier ( const SVMClassifier other)
inline

Definition at line 54 of file svm_classifier.h.

55  {
56  *this=other;
57  }

◆ ~SVMClassifier()

SVMClassifier::~SVMClassifier ( )

Definition at line 70 of file svm_classifier.cpp.

71 {
74  if (prob.y!=NULL)
75  delete [] prob.y;
76  if (prob.x!=NULL)
77  {
78  for(int i=0;i<prob.l;i++)
79  delete [] prob.x[i];
80  delete [] prob.x;
81  }
82 }
svm_parameter param
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
Definition: svm.cpp:3036
#define i
svm_problem prob
void svm_destroy_param(svm_parameter *param)
Definition: svm.cpp:3046
struct svm_node ** x
Definition: svm.h:26
svm_model * model
double * y
Definition: svm.h:25
int l
Definition: svm.h:24

Member Function Documentation

◆ LoadModel()

void SVMClassifier::LoadModel ( const FileName fnModel)

Definition at line 153 of file svm_classifier.cpp.

154 {
155  model=svm_load_model(fnModel.c_str());
156 }
svm_model * model
svm_model * svm_load_model(const char *model_file_name)
Definition: svm.cpp:2893

◆ operator=()

SVMClassifier & SVMClassifier::operator= ( const SVMClassifier other)

Definition at line 61 of file svm_classifier.cpp.

62 {
63  param=other.param;
64  prob=other.prob;
65  delete model;
66  model=new svm_model(*other.model);
67  return *this;
68 }
svm_parameter param
svm_problem prob
Definition: svm.h:58
svm_model * model

◆ predict()

double SVMClassifier::predict ( MultidimArray< double > &  featVec,
double &  score 
)

Definition at line 118 of file svm_classifier.cpp.

119 {
120  svm_node *x_space;
121  int cnt=0;
122  int nr_class=svm_get_nr_class(model);
123  double *prob_estimates=new double[nr_class];
124  x_space=new svm_node[XSIZE(featVec)+1];
125 
126  for (size_t i=0;i<XSIZE(featVec);i++)
127  {
128  if (DIRECT_A1D_ELEM(featVec,i)==0)
129  continue;
130  else
131  {
132  x_space[cnt].value=DIRECT_A1D_ELEM(featVec,i);
133  x_space[cnt].index=i+1;
134  cnt++;
135  }
136  }
137  x_space[cnt].index=-1;
138  double label=svm_predict_probability(model,x_space,prob_estimates);
139  // Extracting the probability of the selected class
140  score=prob_estimates[0];
141  for (int i=1;i<nr_class;++i)
142  if (prob_estimates[i]>score)
143  score=prob_estimates[i];
144  delete [] prob_estimates;
145  delete [] x_space;
146  return label;
147 }
int svm_get_nr_class(const svm_model *model)
Definition: svm.cpp:2471
double svm_predict_probability(const svm_model *model, const svm_node *x, double *prob_estimates)
Definition: svm.cpp:2598
double value
Definition: svm.h:19
#define i
#define DIRECT_A1D_ELEM(v, i)
#define XSIZE(v)
int index
Definition: svm.h:18
svm_model * model
Definition: svm.h:16

◆ SaveModel()

void SVMClassifier::SaveModel ( const FileName fnModel)

Definition at line 148 of file svm_classifier.cpp.

149 {
150  if (model->l!=0)
151  svm_save_model(fnModel.c_str(),model);
152 }
int l
Definition: svm.h:62
int svm_save_model(const char *model_file_name, const svm_model *model)
Definition: svm.cpp:2653
svm_model * model

◆ setParameters()

void SVMClassifier::setParameters ( double  c,
double  gamma 
)

Definition at line 39 of file svm_classifier.cpp.

40 {
43  param.degree = 2;
44  param.gamma = gamma;
45  param.coef0 = 0;
46  param.nu = 0.1;
47  param.cache_size = 1000;
48  param.C = c;
49  param.eps = 0.001;
50  param.p = 0.1;
51  param.shrinking = 1;
52  param.probability = 1;
53  param.nr_weight = 0;
54  param.weight_label = NULL;
55  param.weight = NULL;
56  model=NULL;
57  prob.y=NULL;
58  prob.x=NULL;
59 }
doublereal * c
svm_parameter param
int nr_weight
Definition: svm.h:46
int * weight_label
Definition: svm.h:47
double * gamma
svm_problem prob
double p
Definition: svm.h:50
double cache_size
Definition: svm.h:43
double eps
Definition: svm.h:44
int shrinking
Definition: svm.h:51
struct svm_node ** x
Definition: svm.h:26
svm_model * model
int probability
Definition: svm.h:52
int degree
Definition: svm.h:38
double * y
Definition: svm.h:25
double gamma
Definition: svm.h:39
double * weight
Definition: svm.h:48
double C
Definition: svm.h:45
int svm_type
Definition: svm.h:36
double nu
Definition: svm.h:49
double coef0
Definition: svm.h:40
int kernel_type
Definition: svm.h:37

◆ SVMTrain()

void SVMClassifier::SVMTrain ( MultidimArray< double > &  trainSet,
MultidimArray< double > &  lable 
)

Definition at line 84 of file svm_classifier.cpp.

85 {
86 
87  prob.l = YSIZE(trainSet);
88  prob.y = new double[prob.l];
89  prob.x = new svm_node *[prob.l+1];
90  const char *error_msg;
91  for (size_t i=0;i<YSIZE(trainSet);i++)
92  {
93  prob.x[i]=new svm_node[XSIZE(trainSet)+1];
94  int cnt = 0;
95  for (size_t j=0;j<XSIZE(trainSet);j++)
96  {
97  if (trainSet(i,j)==0)
98  continue;
99  else
100  {
101  prob.x[i][cnt].value=DIRECT_A2D_ELEM(trainSet,i,j);
102  prob.x[i][cnt].index=j+1;
103  cnt++;
104  }
105  }
106  prob.x[i][cnt].index=-1;
107  prob.x[i][cnt].value=2;
108  prob.y[i] = DIRECT_A1D_ELEM(label,i);
109  }
110  error_msg = svm_check_parameter(&prob,&param);
111  if(error_msg)
112  {
113  fprintf(stderr,"ERROR: %s\n",error_msg);
114  exit(1);
115  }
117 }
#define YSIZE(v)
svm_model * svm_train(const svm_problem *prob, const svm_parameter *param)
Definition: svm.cpp:2098
svm_parameter param
#define DIRECT_A2D_ELEM(v, i, j)
double value
Definition: svm.h:19
#define i
svm_problem prob
#define DIRECT_A1D_ELEM(v, i)
#define XSIZE(v)
const char * svm_check_parameter(const svm_problem *prob, const svm_parameter *param)
Definition: svm.cpp:3052
struct svm_node ** x
Definition: svm.h:26
#define j
int index
Definition: svm.h:18
svm_model * model
Definition: svm.h:16
double * y
Definition: svm.h:25
fprintf(glob_prnt.io, "\)
int l
Definition: svm.h:24

Member Data Documentation

◆ model

svm_model* SVMClassifier::model =nullptr

Definition at line 50 of file svm_classifier.h.

◆ param

svm_parameter SVMClassifier::param

Definition at line 48 of file svm_classifier.h.

◆ prob

svm_problem SVMClassifier::prob

Definition at line 49 of file svm_classifier.h.


The documentation for this class was generated from the following files: