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(DREAL 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 (int 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     int weights_label[2]={-1,+1};
00050     double weights[2]={1.0,get_C2()/get_C1()};
00051 
00052 
00053     param.svm_type=ONE_CLASS; // C SVM
00054     param.kernel_type = LINEAR;
00055     param.degree = 3;
00056     param.gamma = 0;    // 1/k
00057     param.coef0 = 0;
00058     param.nu = get_nu();
00059     param.kernel=kernel;
00060     param.cache_size = kernel->get_cache_size();
00061     param.C = get_C1();
00062     param.eps = epsilon;
00063     param.p = 0.1;
00064     param.shrinking = 1;
00065     param.nr_weight = 2;
00066     param.weight_label = weights_label;
00067     param.weight = weights;
00068     
00069     const char* error_msg = svm_check_parameter(&problem,&param);
00070 
00071     if(error_msg)
00072     {
00073         fprintf(stderr,"Error: %s\n",error_msg);
00074         exit(1);
00075     }
00076     
00077     model = svm_train(&problem, &param);
00078 
00079     if (model)
00080     {
00081         ASSERT(model->nr_class==2);
00082         ASSERT((model->l==0) || (model->l>0 && model->SV && model->sv_coef && model->sv_coef[0]));
00083 
00084         int num_sv=model->l;
00085 
00086         create_new_model(num_sv);
00087         CSVM::set_objective(model->objective);
00088 
00089         set_bias(-model->rho[0]);
00090         for (int i=0; i<num_sv; i++)
00091         {
00092             set_support_vector(i, (model->SV[i])->index);
00093             set_alpha(i, model->sv_coef[0][i]);
00094         }
00095 
00096         delete[] problem.x;
00097         delete[] x_space;
00098         svm_destroy_model(model);
00099         model=NULL;
00100 
00101         return true;
00102     }
00103     else
00104         return false;
00105 }

SHOGUN Machine Learning Toolbox - Documentation