KSquare Utilities
svm2.cpp File Reference
#include "FirstIncludes.h"
#include <ctype.h>
#include <float.h>
#include <fstream>
#include <iostream>
#include <istream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <stdarg.h>
#include <vector>
#include <string.h>
#include "MemoryDebug.h"
#include "GlobalGoalKeeper.h"
#include "KKException.h"
#include "KKStr.h"
#include "KKStrParser.h"
#include "OSservices.h"
#include "FeatureVector.h"
#include "svm2.h"

Go to the source code of this file.

Classes

class  SVM289_MFS::Cache
 
struct  SVM289_MFS::decision_function
 
class  SVM289_MFS::Kernel
 
class  SVM289_MFS::ONE_CLASS_Q
 
class  SVM289_MFS::QMatrix
 
class  SVM289_MFS::Solver
 
struct  SVM289_MFS::Solver::SolutionInfo
 
class  SVM289_MFS::Solver_NU
 
class  SVM289_MFS::SVC_Q
 
class  SVM289_MFS::SVR_Q
 

Namespaces

 SVM289_MFS
 Namespce used to wrap implementation of libSVM version 2.89.
 

Macros

#define INF   HUGE_VAL
 
#define TAU   1e-12
 

Functions

template<class T >
T * SVM289_MFS::GrowAllocation (T *src, kkint32 origSize, kkint32 newSize)
 
static void info (const char *fmt,...)
 
void SVM289_MFS::multiclass_probability (kkint32 numClasses, double **pairwiseProbs, double *classProb)
 
double SVM289_MFS::powi (double base, kkint32 times)
 
static void print_string_stdout (const char *s)
 
double SVM289_MFS::sigmoid_predict (double decision_value, double A, double B)
 
void SVM289_MFS::sigmoid_train (kkint32 numExamples, const double *dec_values, const double *labels, double &A, double &B)
 
void SVM289_MFS::solve_c_svc (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, double Cp, double Cn, RunLog &_log)
 
void SVM289_MFS::solve_epsilon_svr (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, RunLog &_log)
 
void SVM289_MFS::solve_nu_svc (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, RunLog &_log)
 
static void solve_nu_svr (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, RunLog &_log)
 
void SVM289_MFS::solve_one_class (const svm_problem *prob, const svm_parameter *param, double *alpha, Solver::SolutionInfo *si, RunLog &_log)
 
void svm_binary_svc_probability (const svm_problem *prob, const svm_parameter *param, double Cp, double Cn, double &probA, double &probB, RunLog &log)
 
const char * svm_check_parameter (const svm_problem *prob, const svm_parameter *param)
 
kkint32 svm_check_probability_model (const Svm_Model *model)
 
void SVM289_MFS::svm_cross_validation (const svm_problem &prob, const svm_parameter &param, kkint32 nr_fold, double *target, RunLog &log)
 
void svm_destroy_param (svm_parameter *&param)
 
void svm_get_labels (const Svm_Model *model, kkint32 *label)
 
kkint32 svm_get_svm_type (const Svm_Model *model)
 
double svm_get_svr_probability (const Svm_Model *model)
 
void svm_group_classes (const svm_problem *prob, kkint32 *nr_class_ret, kkint32 **label_ret, kkint32 **start_ret, kkint32 **count_ret, kkint32 *perm)
 
double svm_svr_probability (const svm_problem &prob, const svm_parameter &param, RunLog &log)
 
decision_function SVM289_MFS::svm_train_one (const svm_problem &prob, const svm_parameter &param, double Cp, double Cn, RunLog &_log)
 

Macro Definition Documentation

#define INF   HUGE_VAL

Definition at line 110 of file svm2.cpp.

#define TAU   1e-12

Definition at line 111 of file svm2.cpp.

Function Documentation

static void info ( const char *  fmt,
  ... 
)
static

Definition at line 606 of file svm2.cpp.

Referenced by SVM289_MFS::Solver::do_shrinking(), SVM289_MFS::multiclass_probability(), SVM289_MFS::Solver::reconstruct_gradient(), SVM289_MFS::sigmoid_train(), SVM289_MFS::Solver::Solve(), SVM289_MFS::solve_epsilon_svr(), SVM289_MFS::solve_nu_svc(), solve_nu_svr(), svm_svr_probability(), and SVM289_MFS::svm_train().

607 {
608  char buf[BUFSIZ];
609  va_list ap;
610  va_start(ap,fmt);
611 
612 #ifdef WIN32
613  vsprintf_s(buf, BUFSIZ, fmt, ap);
614 #else
615  vsprintf(buf,fmt,ap);
616 #endif
617 
618  va_end(ap);
619  (*SVM289_MFS::svm_print_string)(buf);
620 }
static void print_string_stdout ( const char *  s)
static

Definition at line 597 of file svm2.cpp.

598 {
599  fputs(s,stdout);
600  fflush(stdout);
601 }
static void solve_nu_svr ( const svm_problem prob,
const svm_parameter param,
double *  alpha,
Solver::SolutionInfo si,
RunLog _log 
)
static

Definition at line 2672 of file svm2.cpp.

References SVM289_MFS::svm_parameter::C, SVM289_MFS::svm_parameter::eps, info(), SVM289_MFS::svm_parameter::nu, SVM289_MFS::svm_problem::numTrainExamples, SVM289_MFS::Solver::SolutionInfo::r, SVM289_MFS::svm_parameter::shrinking, SVM289_MFS::Solver_NU::Solve(), SVM289_MFS::SVR_Q::SVR_Q(), and SVM289_MFS::svm_problem::y.

Referenced by SVM289_MFS::svm_train_one().

2678 {
2679  kkint32 numTrainExamples = prob->numTrainExamples;
2680  double C = param->C;
2681 
2682  double* alpha2 = new double [2 * numTrainExamples];
2683  double* linear_term = new double [2 * numTrainExamples];
2684  schar* y = new schar [2 * numTrainExamples];
2685  kkint32 i;
2686 
2687  double sum = C * param->nu * numTrainExamples / 2;
2688 
2689  for (i = 0; i < numTrainExamples; i++)
2690  {
2691  alpha2[i] = alpha2[i + numTrainExamples] = Min (sum, C);
2692  sum -= alpha2[i];
2693 
2694  linear_term[i] = - prob->y[i];
2695  y [i] = 1;
2696 
2697  linear_term[i + numTrainExamples] = prob->y[i];
2698  y [i + numTrainExamples] = -1;
2699  }
2700 
2701 
2702  SVR_Q* jester = new SVR_Q (*prob, *param, _log);
2703  Solver_NU s;
2704  s.Solve (2 * numTrainExamples,
2705  *jester,
2706  linear_term,
2707  y,
2708  alpha2,
2709  C,
2710  C,
2711  param->eps,
2712  si,
2713  param->shrinking
2714  );
2715  delete jester;
2716  jester = NULL;
2717 
2718  info ("epsilon = %f\n", -si->r);
2719 
2720  for (i = 0; i < numTrainExamples; i++)
2721  alpha[i] = alpha2[i] - alpha2[i + numTrainExamples];
2722 
2723  delete[] alpha2;
2724  delete[] linear_term;
2725  delete[] y;
2726 } /* solve_nu_svr */
__int32 kkint32
Definition: KKBaseTypes.h:88
void Solve(kkint32 l, QMatrix &Q, const double *p, const schar *y, double *alpha, double Cp, double Cn, double eps, SolutionInfo *si, kkint32 shrinking)
Definition: svm2.cpp:1842
static void info(const char *fmt,...)
Definition: svm2.cpp:606
signed char schar
Definition: svm2.h:289
kkint32 numTrainExamples
Definition: svm2.h:63
kkint32 Min(kkint32 x1, kkint32 x2)
Definition: Raster.cpp:229
void svm_binary_svc_probability ( const svm_problem prob,
const svm_parameter param,
double  Cp,
double  Cn,
double &  probA,
double &  probB,
RunLog log 
)

Definition at line 3092 of file svm2.cpp.

References SVM289_MFS::svm_parameter::C, SVM289_MFS::Svm_Model::label, SVM289_MFS::svm_parameter::nr_weight, SVM289_MFS::svm_problem::numTrainExamples, SVM289_MFS::svm_parameter::probability, KKMLL::FeatureVectorList::PushOnBack(), SVM289_MFS::svm_problem::selFeatures, SVM289_MFS::sigmoid_train(), SVM289_MFS::svm_problem::svm_problem(), SVM289_MFS::svm_train(), SVM289_MFS::svm_parameter::weight, SVM289_MFS::svm_parameter::weight_label, and SVM289_MFS::svm_problem::y.

Referenced by SVM289_MFS::svm_train().

3100 {
3101  kkint32 i = 0;
3102  kkint32 nr_fold = 5;
3103  kkint32* perm = new kkint32[prob->numTrainExamples];
3104 
3105  FeatureVectorPtr* subX = NULL;
3106  svm_problem* subProb = NULL;
3107 
3108  double* dec_values = new double[prob->numTrainExamples];
3109 
3110  // random shuffle
3111  for (i = 0; i < prob->numTrainExamples; ++i)
3112  perm[i]=i;
3113 
3114  for (i = 0; i < prob->numTrainExamples; ++i)
3115  {
3116  kkint32 j = i + rand() % (prob->numTrainExamples - i);
3117  SVM289_MFS::swap (perm[i], perm[j]);
3118  }
3119 
3120  for (i = 0; i < nr_fold; i++)
3121  {
3122  kkint32 begin = i * prob->numTrainExamples / nr_fold;
3123  kkint32 end = (i + 1) * prob->numTrainExamples / nr_fold;
3124  kkint32 j, k;
3125 
3126  kkint32 subL = prob->numTrainExamples - (end - begin);
3127  subX = new FeatureVectorPtr[subL];
3128  for (j = 0; j < subL; j++)
3129  subX[j] = NULL;
3130  float* subY = new float[subL];
3131 
3132  k = 0;
3133  for (j = 0; j < begin; j++)
3134  {
3135  subX[k] = prob->x.IdxToPtr (perm[j]);
3136  subY[k] = (float)prob->y[perm[j]];
3137  ++k;
3138  }
3139 
3140  for (j = end; j < prob->numTrainExamples; ++j)
3141  {
3142  subX[k] = prob->x.IdxToPtr (perm[j]);
3143  subY[k] = (float)prob->y[perm[j]];
3144  ++k;
3145  }
3146 
3147  {
3148  FeatureVectorListPtr subXX = new FeatureVectorList (prob->x.FileDesc (), false);
3149  for (j = 0; j < k; j++)
3150  subXX->PushOnBack (subX[j]);
3151  subProb = new svm_problem (*subXX, subY, prob->selFeatures);
3152  delete subXX;
3153  }
3154 
3155  kkint32 p_count=0, n_count = 0;
3156 
3157  for (j = 0; j < k; j++)
3158  {
3159  if (subY[j] > 0)
3160  p_count++;
3161  else
3162  n_count++;
3163  }
3164 
3165 
3166  if ((p_count == 0) && (n_count == 0))
3167  {
3168  for (j = begin; j < end; j++)
3169  dec_values[perm[j]] = 0;
3170  }
3171 
3172  else if ((p_count > 0) && (n_count == 0))
3173  {
3174  for (j = begin; j < end; j++)
3175  dec_values[perm[j]] = 1;
3176  }
3177 
3178  else if ((p_count == 0) && (n_count > 0))
3179  {
3180  for (j = begin; j < end; j++)
3181  dec_values[perm[j]] = -1;
3182  }
3183 
3184  else
3185  {
3186  svm_parameter subparam = *param;
3187  subparam.probability=0;
3188  subparam.C=1.0;
3189  subparam.nr_weight=2;
3190  subparam.weight_label = new kkint32[2];
3191  subparam.weight = new double[2];
3192  subparam.weight_label[0]=+1;
3193  subparam.weight_label[1]=-1;
3194  subparam.weight[0]=Cp;
3195  subparam.weight[1]=Cn;
3196  Svm_Model* submodel = svm_train (*subProb, subparam, log);
3197 
3198  for (j = begin; j < end; j++)
3199  {
3200  svm_predict_values (submodel, prob->x[perm[j]], &(dec_values[perm[j]]));
3201  // ensure +1 -1 order; reason not using CV subroutine
3202  dec_values[perm[j]] *= submodel->label[0];
3203  }
3204 
3205  delete submodel; submodel = NULL;
3206  //svm_destroy_param (&subparam);
3207  }
3208 
3209  delete subProb; subProb = NULL;
3210  delete[] subX; subX = NULL;
3211  delete[] subY; subY = NULL;
3212  }
3213 
3214  sigmoid_train (prob->numTrainExamples, dec_values, prob->y, probA, probB);
3215  delete[] dec_values; dec_values = NULL;
3216  delete[] perm; perm = NULL;
3217 } /* svm_binary_svc_probability */
FeatureVectorList x
Definition: svm2.h:65
void PushOnBack(FeatureVectorPtr image)
Overloading the PushOnBack function in KKQueue so we can monitor the Version and Sort Order...
__int32 kkint32
Definition: KKBaseTypes.h:88
const FileDescPtr FileDesc() const
EntryPtr IdxToPtr(kkuint32 idx) const
Definition: KKQueue.h:732
struct SvmModel233 * svm_train(const struct svm_problem *prob, const struct svm_parameter *param)
Container class for FeatureVector derived objects.
FeatureNumList selFeatures
Definition: svm2.h:64
void swap(T &x, T &y)
Definition: svm2.h:283
void sigmoid_train(kkint32 numExamples, const double *dec_values, const double *labels, double &A, double &B)
Definition: svm2.cpp:2850
kkint32 * label
Definition: svm2.h:216
kkint32 * weight_label
Definition: svm2.h:142
kkint32 numTrainExamples
Definition: svm2.h:63
Represents a Feature Vector of a single example, labeled or unlabeled.
Definition: FeatureVector.h:59
void svm_predict_values(const Svm_Model *model, const FeatureVector &x, double *dec_values)
Definition: svm2.cpp:3856
const char* svm_check_parameter ( const svm_problem prob,
const svm_parameter param 
)

Definition at line 4636 of file svm2.cpp.

References SVM289_MFS::svm_parameter::C, SVM289_MFS::C_SVC, SVM289_MFS::svm_parameter::cache_size, SVM289_MFS::svm_parameter::degree, SVM289_MFS::svm_parameter::eps, SVM289_MFS::EPSILON_SVR, SVM289_MFS::svm_parameter::kernel_type, SVM289_MFS::LINEAR, SVM289_MFS::svm_parameter::nu, SVM289_MFS::NU_SVC, SVM289_MFS::NU_SVR, SVM289_MFS::svm_problem::numTrainExamples, SVM289_MFS::ONE_CLASS, SVM289_MFS::svm_parameter::p, SVM289_MFS::POLY, SVM289_MFS::PRECOMPUTED, SVM289_MFS::svm_parameter::probability, SVM289_MFS::RBF, SVM289_MFS::svm_parameter::shrinking, SVM289_MFS::SIGMOID, SVM289_MFS::svm_parameter::svm_type, and SVM289_MFS::svm_problem::y.

4639 {
4640  // svm_type
4641 
4642  SVM_Type svm_type = param->svm_type;
4643 
4644  if (svm_type != SVM_Type::C_SVC &&
4645  svm_type != SVM_Type::NU_SVC &&
4646  svm_type != SVM_Type::ONE_CLASS &&
4647  svm_type != SVM_Type::EPSILON_SVR &&
4648  svm_type != SVM_Type::NU_SVR
4649  )
4650  return "unknown svm type";
4651 
4652  // kernel_type, degree
4653 
4654  Kernel_Type kernel_type = param->kernel_type;
4655 
4656  if (kernel_type != Kernel_Type::LINEAR &&
4657  kernel_type != Kernel_Type::POLY &&
4658  kernel_type != Kernel_Type::RBF &&
4659  kernel_type != Kernel_Type::SIGMOID &&
4660  kernel_type != Kernel_Type::PRECOMPUTED
4661  )
4662  return "unknown kernel type";
4663 
4664  if (param->degree < 0)
4665  return "degree of polynomial kernel < 0";
4666 
4667  // cache_size,eps,C,nu,p,shrinking
4668 
4669  if (param->cache_size <= 0)
4670  return "cache_size <= 0";
4671 
4672  if (param->eps <= 0)
4673  return "eps <= 0";
4674 
4675  if (svm_type == SVM_Type::C_SVC ||
4676  svm_type == SVM_Type::EPSILON_SVR ||
4677  svm_type ==SVM_Type:: NU_SVR
4678  )
4679  if (param->C <= 0)
4680  return "C <= 0";
4681 
4682  if (svm_type == SVM_Type::NU_SVC ||
4683  svm_type == SVM_Type::ONE_CLASS ||
4684  svm_type == SVM_Type::NU_SVR
4685  )
4686  if ((param->nu <= 0) || (param->nu > 1))
4687  return "nu <= 0 or nu > 1";
4688 
4689  if (svm_type == SVM_Type::EPSILON_SVR)
4690  {
4691  if (param->p < 0)
4692  return "p < 0";
4693  }
4694 
4695  if (param->shrinking != 0 && param->shrinking != 1)
4696  return "shrinking != 0 and shrinking != 1";
4697 
4698  if ((param->probability != 0) && (param->probability != 1))
4699  return "probability != 0 and probability != 1";
4700 
4701  if ((param->probability == 1) && (svm_type == SVM_Type::ONE_CLASS))
4702  return "one-class SVM probability output not supported yet";
4703 
4704 
4705  // check whether nu-svc is feasible
4706 
4707  if (svm_type == SVM_Type::NU_SVC)
4708  {
4709  kkint32 l = prob->numTrainExamples;
4710  kkint32 max_nr_class = 16;
4711  kkint32 nr_class = 0;
4712  kkint32* label = new kkint32[max_nr_class];
4713  kkint32* count = new kkint32[max_nr_class];
4714 
4715  kkint32 i;
4716  for (i = 0; i < l; i++)
4717  {
4718  kkint32 this_label = (kkint32)prob->y[i];
4719  kkint32 j;
4720  for (j = 0; j < nr_class; j++)
4721  {
4722  if (this_label == label[j])
4723  {
4724  ++count[j];
4725  break;
4726  }
4727  }
4728 
4729  if (j == nr_class)
4730  {
4731  if (nr_class == max_nr_class)
4732  {
4733  kkint32 oldMaxNrClass = max_nr_class;
4734  max_nr_class *= 2;
4735  label = GrowAllocation (label, oldMaxNrClass, max_nr_class);
4736  count = GrowAllocation (count, oldMaxNrClass, max_nr_class);
4737  }
4738  label[nr_class] = this_label;
4739  count[nr_class] = 1;
4740  ++nr_class;
4741  }
4742  }
4743 
4744 
4745  for (i = 0; i < nr_class; i++)
4746  {
4747  kkint32 n1 = count[i];
4748  for (kkint32 j = i + 1; j < nr_class; j++)
4749  {
4750  kkint32 n2 = count[j];
4751  if ((param->nu * (n1 + n2) / 2) > Min (n1, n2))
4752  {
4753  delete[] label; label = NULL;
4754  delete[] count; count = NULL;
4755  return "specified nu is infeasible";
4756  }
4757  }
4758  }
4759 
4760  delete[] label; label = NULL;
4761  delete[] count; count = NULL;
4762  }
4763 
4764  return NULL;
4765 } /* svm_check_parameter */
__int32 kkint32
Definition: KKBaseTypes.h:88
Kernel_Type kernel_type
Definition: svm2.h:132
SVM_Type
Definition: svm2.h:72
#define LINEAR
Definition: UsfCasCor.h:221
kkint32 numTrainExamples
Definition: svm2.h:63
T * GrowAllocation(T *src, kkint32 origSize, kkint32 newSize)
Definition: svm2.cpp:80
Kernel_Type
Definition: svm2.h:83
kkint32 Min(kkint32 x1, kkint32 x2)
Definition: Raster.cpp:229
#define SIGMOID
Definition: UsfCasCor.h:219
kkint32 svm_check_probability_model ( const Svm_Model model)
void svm_destroy_param ( svm_parameter *&  param)

Definition at line 4627 of file svm2.cpp.

4628 {
4629  delete param;
4630  param = NULL;
4631 }
void svm_get_labels ( const Svm_Model model,
kkint32 label 
)

Definition at line 3832 of file svm2.cpp.

References SVM289_MFS::Svm_Model::label, and SVM289_MFS::Svm_Model::nr_class.

3833 {
3834  if (model->label != NULL)
3835  for(kkint32 i=0;i<model->nr_class;i++)
3836  label[i] = model->label[i];
3837 }
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 nr_class
Definition: svm2.h:205
kkint32 * label
Definition: svm2.h:216
kkint32 svm_get_svm_type ( const Svm_Model model)

Definition at line 3817 of file svm2.cpp.

References SVM289_MFS::Svm_Model::param, and SVM289_MFS::svm_parameter::svm_type.

3818 {
3819  return (int)model->param.svm_type;
3820 }
svm_parameter param
Definition: svm2.h:204
double svm_get_svr_probability ( const Svm_Model model)

Definition at line 3841 of file svm2.cpp.

References SVM289_MFS::EPSILON_SVR, SVM289_MFS::NU_SVR, SVM289_MFS::Svm_Model::param, SVM289_MFS::Svm_Model::probA, and SVM289_MFS::svm_parameter::svm_type.

3842 {
3843  if ((model->param.svm_type == SVM_Type::EPSILON_SVR || model->param.svm_type == SVM_Type::NU_SVR) &&
3844  model->probA!=NULL)
3845  return model->probA[0];
3846  else
3847  {
3848  fprintf(stderr,"Model doesn't contain information for SVR probability inference\n");
3849  return 0;
3850  }
3851 }
svm_parameter param
Definition: svm2.h:204
double * probA
Definition: svm2.h:210
void svm_group_classes ( const svm_problem prob,
kkint32 nr_class_ret,
kkint32 **  label_ret,
kkint32 **  start_ret,
kkint32 **  count_ret,
kkint32 perm 
)

Definition at line 3271 of file svm2.cpp.

References SVM289_MFS::svm_problem::numTrainExamples, and SVM289_MFS::svm_problem::y.

Referenced by SVM289_MFS::svm_cross_validation(), and SVM289_MFS::svm_train().

3278 {
3279  kkint32 l = prob->numTrainExamples;
3280  kkint32 max_nr_class = 16;
3281  kkint32 nr_class = 0;
3282  kkint32 *label = new kkint32[max_nr_class];
3283  kkint32 *count = new kkint32[max_nr_class];
3284  kkint32 *data_label = new kkint32[l];
3285  kkint32 i;
3286 
3287 
3288  // Count number of examples in each class
3289  for (i = 0; i < l; i++)
3290  {
3291  kkint32 this_label = (kkint32)prob->y[i];
3292  kkint32 j;
3293  for (j = 0; j < nr_class; j++)
3294  {
3295  if (this_label == label[j])
3296  {
3297  ++count[j];
3298  break;
3299  }
3300  }
3301 
3302  data_label[i] = j;
3303  if (j == nr_class)
3304  {
3305  if (nr_class == max_nr_class)
3306  {
3307  max_nr_class *= 2;
3308  label = (kkint32 *)realloc (label, max_nr_class * sizeof(kkint32));
3309  count = (kkint32 *)realloc (count, max_nr_class * sizeof(kkint32));
3310  }
3311  label[nr_class] = this_label;
3312  count[nr_class] = 1;
3313  ++nr_class;
3314  }
3315  }
3316 
3317  kkint32 *start = new kkint32[nr_class];
3318  start[0] = 0;
3319  for (i = 1; i < nr_class; i++)
3320  start[i] = start[i - 1] + count[i - 1];
3321 
3322  for (i = 0; i < l; i++)
3323  {
3324  perm[start[data_label[i]]] = i;
3325  ++start[data_label[i]];
3326  }
3327 
3328  start[0] = 0;
3329  for (i = 1; i < nr_class; i++)
3330  start[i] = start[i - 1] + count[i - 1];
3331 
3332  *nr_class_ret = nr_class;
3333  *label_ret = label;
3334  *start_ret = start;
3335  *count_ret = count;
3336  delete data_label; data_label = NULL;
3337 } /* svm_group_classes*/
__int32 kkint32
Definition: KKBaseTypes.h:88
kkint32 numTrainExamples
Definition: svm2.h:63
double svm_svr_probability ( const svm_problem prob,
const svm_parameter param,
RunLog log 
)

Definition at line 3226 of file svm2.cpp.

References info(), SVM289_MFS::svm_problem::numTrainExamples, SVM289_MFS::svm_parameter::probability, SVM289_MFS::svm_cross_validation(), and SVM289_MFS::svm_problem::y.

Referenced by SVM289_MFS::svm_train().

3230 {
3231  kkint32 i;
3232  kkint32 nr_fold = 5;
3233  double *ymv = new double[prob.numTrainExamples];
3234  double mae = 0;
3235 
3236  svm_parameter newparam = param;
3237  newparam.probability = 0;
3238  svm_cross_validation (prob, newparam, nr_fold, ymv, log);
3239 
3240  for (i = 0; i < prob.numTrainExamples; ++i)
3241  {
3242  ymv[i] = prob.y[i] - ymv[i];
3243  mae += fabs (ymv[i]);
3244  }
3245 
3246  mae /= prob.numTrainExamples;
3247  double std = sqrt (2 * mae * mae);
3248  kkint32 count = 0;
3249  mae = 0;
3250  for (i = 0; i < prob.numTrainExamples; ++i)
3251  {
3252  if (fabs(ymv[i]) > (5 * std))
3253  count = count + 1;
3254  else
3255  mae += fabs (ymv[i]);
3256  }
3257 
3258  mae /= (prob.numTrainExamples - count);
3259 
3260  info("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma= %g\n", mae);
3261  delete ymv; ymv = NULL;
3262  return mae;
3263 } /* svm_svr_probability */
__int32 kkint32
Definition: KKBaseTypes.h:88
void svm_cross_validation(const svm_problem &prob, const svm_parameter &param, kkint32 nr_fold, double *target, RunLog &log)
Definition: svm2.cpp:3661
static void info(const char *fmt,...)
Definition: svm2.cpp:606
kkint32 numTrainExamples
Definition: svm2.h:63