SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
LibSVMOneClass.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2006 Christian Gehl
8  * Written (W) 2006-2009 Soeren Sonnenburg
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
13 #include <shogun/io/SGIO.h>
14 
15 using namespace shogun;
16 
18 : CSVM()
19 {
20 }
21 
23 : CSVM(C, k, NULL)
24 {
25 }
26 
28 {
29 }
30 
32 {
33  svm_problem problem;
34  svm_parameter param;
35  struct svm_model* model = nullptr;
36 
37  ASSERT(kernel)
38  if (data)
39  kernel->init(data, data);
40 
41  problem.l=kernel->get_num_vec_lhs();
42 
43  struct svm_node* x_space;
44  SG_INFO("%d train data points\n", problem.l)
45 
46  problem.y=NULL;
47  problem.x=SG_MALLOC(struct svm_node*, problem.l);
48  x_space=SG_MALLOC(struct svm_node, 2*problem.l);
49 
50  for (int32_t i=0; i<problem.l; i++)
51  {
52  problem.x[i]=&x_space[2*i];
53  x_space[2*i].index=i;
54  x_space[2*i+1].index=-1;
55  }
56 
57  int32_t weights_label[2]={-1,+1};
58  float64_t weights[2]={1.0,get_C2()/get_C1()};
59 
60  param.svm_type=ONE_CLASS; // C SVM
61  param.kernel_type = LINEAR;
62  param.degree = 3;
63  param.gamma = 0; // 1/k
64  param.coef0 = 0;
65  param.nu = get_nu();
66  param.kernel=kernel;
67  param.cache_size = kernel->get_cache_size();
68  param.max_train_time = m_max_train_time;
69  param.C = get_C1();
70  param.eps = epsilon;
71  param.p = 0.1;
72  param.shrinking = 1;
73  param.nr_weight = 2;
74  param.weight_label = weights_label;
75  param.weight = weights;
76  param.use_bias = get_bias_enabled();
77 
78  const char* error_msg = svm_check_parameter(&problem,&param);
79 
80  if(error_msg)
81  SG_ERROR("Error: %s\n",error_msg)
82 
83  model = svm_train(&problem, &param);
84 
85  if (model)
86  {
87  ASSERT(model->nr_class==2)
88  ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef[0]))
89 
90  int32_t num_sv=model->l;
91 
92  create_new_model(num_sv);
93  CSVM::set_objective(model->objective);
94 
95  set_bias(-model->rho[0]);
96  for (int32_t i=0; i<num_sv; i++)
97  {
98  set_support_vector(i, (model->SV[i])->index);
99  set_alpha(i, model->sv_coef[0][i]);
100  }
101 
102  SG_FREE(problem.x);
103  SG_FREE(x_space);
104  svm_destroy_model(model);
105  model=NULL;
106 
107  return true;
108  }
109  else
110  return false;
111 }
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:96
#define SG_INFO(...)
Definition: SGIO.h:117
float64_t get_C2()
Definition: SVM.h:167
float64_t m_max_train_time
Definition: Machine.h:362
#define SG_ERROR(...)
Definition: SGIO.h:128
float64_t get_nu()
Definition: SVM.h:155
float64_t epsilon
Definition: SVM.h:266
virtual int32_t get_num_vec_lhs()
int32_t cache_size
cache_size in MB
float64_t get_C1()
Definition: SVM.h:161
#define ASSERT(x)
Definition: SGIO.h:200
void set_bias(float64_t bias)
double float64_t
Definition: common.h:60
bool set_alpha(int32_t idx, float64_t val)
void set_objective(float64_t v)
Definition: SVM.h:209
bool set_support_vector(int32_t idx, int32_t val)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
A generic Support Vector Machine Interface.
Definition: SVM.h:49
The Kernel base class.
int32_t get_cache_size()
virtual bool train_machine(CFeatures *data=NULL)
bool create_new_model(int32_t num)

SHOGUN Machine Learning Toolbox - Documentation