LibSVMOneClass.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2006 Christian Gehl
00008  * Written (W) 2006-2008 Soeren Sonnenburg
00009  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #include "classifier/svm/LibSVMOneClass.h"
00013 #include "lib/io.h"
00014 
00015 CLibSVMOneClass::CLibSVMOneClass()
00016 : CSVM(), model(NULL)
00017 {
00018 }
00019 
00020 CLibSVMOneClass::CLibSVMOneClass(float64_t C, CKernel* k)
00021 : CSVM(C, k, NULL), model(NULL)
00022 {
00023 }
00024 
00025 CLibSVMOneClass::~CLibSVMOneClass()
00026 {
00027     free(model);
00028 }
00029 
00030 bool CLibSVMOneClass::train()
00031 {
00032     ASSERT(kernel);
00033     problem.l=kernel->get_num_vec_lhs();
00034 
00035     struct svm_node* x_space;
00036     SG_INFO("%d train data points\n", problem.l);
00037 
00038     problem.y=NULL;
00039     problem.x=new struct svm_node*[problem.l];
00040     x_space=new struct svm_node[2*problem.l];
00041 
00042     for (int32_t i=0; i<problem.l; i++)
00043     {
00044         problem.x[i]=&x_space[2*i];
00045         x_space[2*i].index=i;
00046         x_space[2*i+1].index=-1;
00047     }
00048 
00049     int32_t weights_label[2]={-1,+1};
00050     float64_t weights[2]={1.0,get_C2()/get_C1()};
00051 
00052     param.svm_type=ONE_CLASS; // C SVM
00053     param.kernel_type = LINEAR;
00054     param.degree = 3;
00055     param.gamma = 0;    // 1/k
00056     param.coef0 = 0;
00057     param.nu = get_nu();
00058     param.kernel=kernel;
00059     param.cache_size = kernel->get_cache_size();
00060     param.C = get_C1();
00061     param.eps = epsilon;
00062     param.p = 0.1;
00063     param.shrinking = 1;
00064     param.nr_weight = 2;
00065     param.weight_label = weights_label;
00066     param.weight = weights;
00067     
00068     const char* error_msg = svm_check_parameter(&problem,&param);
00069 
00070     if(error_msg)
00071     {
00072         fprintf(stderr,"Error: %s\n",error_msg);
00073         exit(1);
00074     }
00075     
00076     model = svm_train(&problem, &param);
00077 
00078     if (model)
00079     {
00080         ASSERT(model->nr_class==2);
00081         ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef[0]));
00082 
00083         int32_t num_sv=model->l;
00084 
00085         create_new_model(num_sv);
00086         CSVM::set_objective(model->objective);
00087 
00088         set_bias(-model->rho[0]);
00089         for (int32_t i=0; i<num_sv; i++)
00090         {
00091             set_support_vector(i, (model->SV[i])->index);
00092             set_alpha(i, model->sv_coef[0][i]);
00093         }
00094 
00095         delete[] problem.x;
00096         delete[] x_space;
00097         svm_destroy_model(model);
00098         model=NULL;
00099 
00100         return true;
00101     }
00102     else
00103         return false;
00104 }

SHOGUN Machine Learning Toolbox - Documentation