Definition at line 1729 of file svm.cpp. References svm_problem::l, svm_model::label, Malloc, svm_parameter::probability, svm_destroy_model(), svm_destroy_param(), svm_predict_values(), svm_train(), swap(), svm_problem::x, and svm_problem::y. 01732 { 01733 int i; 01734 int nr_fold = 5; 01735 int *perm = Malloc(int,prob->l); 01736 double *dec_values = Malloc(double,prob->l); 01737 01738 // random shuffle 01739 for(i=0;i<prob->l;i++) perm[i]=i; 01740 for(i=0;i<prob->l;i++) 01741 { 01742 int j = i+rand()%(prob->l-i); 01743 std::swap(perm[i],perm[j]); 01744 } 01745 for(i=0;i<nr_fold;i++) 01746 { 01747 int begin = i*prob->l/nr_fold; 01748 int end = (i+1)*prob->l/nr_fold; 01749 int j,k; 01750 struct svm_problem subprob; 01751 01752 subprob.l = prob->l-(end-begin); 01753 subprob.x = Malloc(struct svm_node*,subprob.l); 01754 subprob.y = Malloc(double,subprob.l); 01755 01756 k=0; 01757 for(j=0;j<begin;j++) 01758 { 01759 subprob.x[k] = prob->x[perm[j]]; 01760 subprob.y[k] = prob->y[perm[j]]; 01761 ++k; 01762 } 01763 for(j=end;j<prob->l;j++) 01764 { 01765 subprob.x[k] = prob->x[perm[j]]; 01766 subprob.y[k] = prob->y[perm[j]]; 01767 ++k; 01768 } 01769 int p_count=0,n_count=0; 01770 for(j=0;j<k;j++) 01771 if(subprob.y[j]>0) 01772 p_count++; 01773 else 01774 n_count++; 01775 01776 if(p_count==0 && n_count==0) 01777 for(j=begin;j<end;j++) 01778 dec_values[perm[j]] = 0; 01779 else if(p_count > 0 && n_count == 0) 01780 for(j=begin;j<end;j++) 01781 dec_values[perm[j]] = 1; 01782 else if(p_count == 0 && n_count > 0) 01783 for(j=begin;j<end;j++) 01784 dec_values[perm[j]] = -1; 01785 else 01786 { 01787 svm_parameter subparam = *param; 01788 subparam.probability=0; 01789 subparam.C=1.0; 01790 subparam.nr_weight=2; 01791 subparam.weight_label = Malloc(int,2); 01792 subparam.weight = Malloc(double,2); 01793 subparam.weight_label[0]=+1; 01794 subparam.weight_label[1]=-1; 01795 subparam.weight[0]=Cp; 01796 subparam.weight[1]=Cn; 01797 struct svm_model *submodel = svm_train(&subprob,&subparam); 01798 for(j=begin;j<end;j++) 01799 { 01800 svm_predict_values(submodel,prob->x[perm[j]],&(dec_values[perm[j]])); 01801 // ensure +1 -1 order; reason not using CV subroutine 01802 dec_values[perm[j]] *= submodel->label[0]; 01803 } 01804 svm_destroy_model(submodel); 01805 svm_destroy_param(&subparam); 01806 free(subprob.x); 01807 free(subprob.y); 01808 } 01809 } 01810 sigmoid_train(prob->l,dec_values,prob->y,probA,probB); 01811 free(dec_values); 01812 free(perm); 01813 }
Here is the call graph for this function:
|