MCPcopy Create free account
hub / github.com/PointCloudLibrary/pcl / svm_cross_validation

Function svm_cross_validation

ml/src/svm.cpp:2677–2810  ·  view source on GitHub ↗

Stratified cross validation

Source from the content-addressed store, hash-verified

2675
2676// Stratified cross validation
2677void
2678svm_cross_validation(const svm_problem* prob,
2679 const svm_parameter* param,
2680 int nr_fold,
2681 double* target)
2682{
2683 int* fold_start = Malloc(int, nr_fold + 1);
2684 int l = prob->l;
2685 int* perm = Malloc(int, l);
2686 int nr_class;
2687
2688 // stratified cv may not give leave-one-out rate
2689 // Each class to l folds -> some folds may have zero elements
2690
2691 if ((param->svm_type == C_SVC || param->svm_type == NU_SVC) && nr_fold < l) {
2692 int* start = nullptr;
2693 int* label = nullptr;
2694 int* count = nullptr;
2695 svm_group_classes(prob, &nr_class, &label, &start, &count, perm);
2696
2697 // random shuffle and then data grouped by fold using the array perm
2698 int* fold_count = Malloc(int, nr_fold);
2699 int* index = Malloc(int, l);
2700
2701 for (int i = 0; i < l; i++)
2702 index[i] = perm[i];
2703
2704 for (int c = 0; c < nr_class; c++)
2705 for (int i = 0; i < count[c]; i++) {
2706 int j = i + rand() % (count[c] - i);
2707 swap(index[start[c] + j], index[start[c] + i]);
2708 }
2709
2710 for (int i = 0; i < nr_fold; i++) {
2711 fold_count[i] = 0;
2712
2713 for (int c = 0; c < nr_class; c++)
2714 fold_count[i] += (i + 1) * count[c] / nr_fold - i * count[c] / nr_fold;
2715 }
2716
2717 fold_start[0] = 0;
2718
2719 for (int i = 1; i <= nr_fold; i++)
2720 fold_start[i] = fold_start[i - 1] + fold_count[i - 1];
2721
2722 for (int c = 0; c < nr_class; c++)
2723 for (int i = 0; i < nr_fold; i++) {
2724 int begin = start[c] + i * count[c] / nr_fold;
2725 int end = start[c] + (i + 1) * count[c] / nr_fold;
2726
2727 for (int j = begin; j < end; j++) {
2728 perm[fold_start[i]] = index[j];
2729 fold_start[i]++;
2730 }
2731 }
2732
2733 fold_start[0] = 0;
2734

Callers 2

doCrossValidationMethod · 0.85
svm_svr_probabilityFunction · 0.85

Calls 7

svm_group_classesFunction · 0.85
svm_trainFunction · 0.85
svm_get_nr_classFunction · 0.85
svm_predict_probabilityFunction · 0.85
svm_predictFunction · 0.85
swapFunction · 0.70

Tested by

no test coverage detected