ext/libsvm/svm.cpp in rb-libsvm-1.1.3 vs ext/libsvm/svm.cpp in rb-libsvm-1.1.4

- old
+ new

@@ -2046,10 +2046,28 @@ count[nr_class] = 1; ++nr_class; } } + // + // Labels are ordered by their first occurrence in the training set. + // However, for two-class sets with -1/+1 labels and -1 appears first, + // we swap labels to ensure that internally the binary SVM has positive data corresponding to the +1 instances. + // + if (nr_class == 2 && label[0] == -1 && label[1] == 1) + { + swap(label[0],label[1]); + swap(count[0],count[1]); + for(i=0;i<l;i++) + { + if(data_label[i] == 0) + data_label[i] = 1; + else + data_label[i] = 0; + } + } + int *start = Malloc(int,nr_class); start[0] = 0; for(i=1;i<nr_class;i++) start[i] = start[i-1]+count[i-1]; for(i=0;i<l;i++) @@ -2319,15 +2337,20 @@ // Stratified cross validation void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, int nr_fold, double *target) { int i; - int *fold_start = Malloc(int,nr_fold+1); + int *fold_start; int l = prob->l; int *perm = Malloc(int,l); int nr_class; - + if (nr_fold > l) + { + nr_fold = l; + fprintf(stderr,"WARNING: # folds > # data. Will use # folds = # data instead (i.e., leave-one-out cross validation)\n"); + } + fold_start = Malloc(int,nr_fold+1); // stratified cv may not give leave-one-out rate // Each class to l folds -> some folds may have zero elements if((param->svm_type == C_SVC || param->svm_type == NU_SVC) && nr_fold < l) { @@ -2743,12 +2766,12 @@ svm_model *model = Malloc(svm_model,1); svm_parameter& param = model->param; model->rho = NULL; model->probA = NULL; model->probB = NULL; + model->sv_indices = NULL; model->label = NULL; model->nSV = NULL; - model->sv_indices = NULL; char cmd[81]; while(1) { fscanf(fp,"%80s",cmd);