00001 #include "OneClassSVM.h" 00002 #include <cstdio> 00003 00004 namespace damina 00005 { 00006 00011 OneClassSVM::OneClassSVM() { 00012 this->params = (struct svm_parameter *)malloc(sizeof(struct svm_parameter)); 00013 this->params->svm_type = ONE_CLASS; 00014 } 00015 00021 OneClassSVM::OneClassSVM(DataSet *train) { 00022 this->params = (struct svm_parameter *)malloc(sizeof(struct svm_parameter)); 00023 this->params->svm_type = ONE_CLASS; 00024 this->trainingSetToProblem(); 00025 } 00026 00032 OneClassSVM::OneClassSVM(struct svm_problem *prob) { 00033 this->params = (struct svm_parameter *)malloc(sizeof(struct svm_parameter)); 00034 this->params->svm_type = ONE_CLASS; 00035 this->problem = prob; 00036 } 00037 00041 OneClassSVM::~OneClassSVM() { 00042 free(this->params); 00043 free(this->problem); 00044 free(this->model); 00045 } 00046 00047 00057 void OneClassSVM::setNU(double nu) { 00058 this->params->nu = nu; 00059 } 00060 00070 double OneClassSVM::getNU() { 00071 return this->params->nu; 00072 } 00073 00074 00079 void OneClassSVM::learn() { 00080 this->model = svm_train(this->problem, this->params); 00081 } 00082 00088 void OneClassSVM::learn(DataSet *ts) { 00089 this->setTrainingSet(ts); 00090 this->trainingSetToProblem(); 00091 this->learn(); 00092 } 00093 00099 void OneClassSVM::learn(struct svm_problem *p) { 00100 this->setProblem(p); 00101 this->learn(); 00102 } 00103 00109 void OneClassSVM::setProblem(struct svm_problem *p) { 00110 this->problem = p; 00111 } 00112 00113 00119 struct svm_problem *OneClassSVM::getProblem() { 00120 return this->problem; 00121 } 00122 00123 00129 void OneClassSVM::setTrainingSet(DataSet *ts) { 00130 AbstractSVM::setTrainingSet(ts); 00131 this->trainingSetToProblem(); 00132 } 00133 00134 00139 void OneClassSVM::classify() { 00140 // TODO to implement 00141 // it acts on test set 00142 } 00143 00149 void OneClassSVM::classify(DataSet *test) { 00150 // TODO to implement 00151 // it acts on test set 00152 } 00153 00154 00160 struct svm_model *OneClassSVM::getModel() { 00161 return this->model; 00162 } 00163 00164 00165 // private methods 00166 00167 00172 void OneClassSVM::trainingSetToProblem() { 00173 DataSet *ts = this->getTrainingSet(); 00174 Point *temp; 00175 00176 unsigned long int i, j; 00177 00178 this->problem->l = ts->getSize(); 00179 this->problem->y = (double *) malloc(this->problem->l * sizeof(double)); 00180 this->problem->x = (struct svm_node **) malloc(this->problem->l * sizeof(struct svm_node*)); 00181 00182 // iteration on Points 00183 for (i = 0; i < ts->getSize(); i++) { 00184 temp = ts->get(i); 00185 00186 this->problem->x[i] = (struct svm_node*) malloc((temp->getSize() + 1) * sizeof(struct svm_node)); 00187 this->problem->y[i] = temp->getLabel(); 00188 00189 //iteration on Features 00190 for (j = 0; j < temp->getSize(); j++) { 00191 this->problem->x[i][j].index = temp->get(j)->getIndex(); 00192 this->problem->x[i][j].value = temp->get(j)->getValue(); 00193 } 00194 00195 this->problem->x[i][j].index = -1; 00196 this->problem->x[i][j].value = 0; 00197 } 00198 } 00199 }
1.5.2