(prob *SVM_Problem, param *SVM_Parameter, nr_fold int, target []float64)
| 2119 | } |
| 2120 | |
| 2121 | func (this *SVM) SVM_cross_validation(prob *SVM_Problem, param *SVM_Parameter, nr_fold int, target []float64) { |
| 2122 | var i int |
| 2123 | fold_start := make([]int, nr_fold+1) |
| 2124 | l := prob.l |
| 2125 | perm := make([]int, l) |
| 2126 | |
| 2127 | // stratified cv may not give leave-one-out rate |
| 2128 | // Each class to l folds -> some folds may have zero elements |
| 2129 | if (param.svm_type == C_SVC || |
| 2130 | param.svm_type == NU_SVC) && nr_fold < l { |
| 2131 | tmp_nr_class := make([]int, 1) |
| 2132 | tmp_label := make([][]int, 1) |
| 2133 | tmp_start := make([][]int, 1) |
| 2134 | tmp_count := make([][]int, 1) |
| 2135 | |
| 2136 | this.SVM_group_classes(prob, tmp_nr_class, tmp_label, tmp_start, tmp_count, perm) |
| 2137 | |
| 2138 | nr_class := tmp_nr_class[0] |
| 2139 | start := tmp_start[0] |
| 2140 | count := tmp_count[0] |
| 2141 | |
| 2142 | // random shuffle and then data grouped by fold using the array perm |
| 2143 | fold_count := make([]int, nr_fold) |
| 2144 | var c int |
| 2145 | index := make([]int, l) |
| 2146 | for i = 0; i < l; i++ { |
| 2147 | index[i] = perm[i] |
| 2148 | } |
| 2149 | for c = 0; c < nr_class; c++ { |
| 2150 | for i = 0; i < count[c]; i++ { |
| 2151 | j := i + int(rand.Int31n(int32(count[c]-i))) |
| 2152 | //do {int _=index[start[c]+j]; index[start[c]+j]=index[start[c]+i]; index[start[c]+i]=_;} while(false); |
| 2153 | index[start[c]+i], index[start[c]+j] = index[start[c]+j], index[start[c]+i] |
| 2154 | } |
| 2155 | } |
| 2156 | for i = 0; i < nr_fold; i++ { |
| 2157 | fold_count[i] = 0 |
| 2158 | for c = 0; c < nr_class; c++ { |
| 2159 | fold_count[i] += (i+1)*count[c]/nr_fold - i*count[c]/nr_fold |
| 2160 | } |
| 2161 | } |
| 2162 | fold_start[0] = 0 |
| 2163 | for i = 1; i <= nr_fold; i++ { |
| 2164 | fold_start[i] = fold_start[i-1] + fold_count[i-1] |
| 2165 | } |
| 2166 | for c = 0; c < nr_class; c++ { |
| 2167 | for i = 0; i < nr_fold; i++ { |
| 2168 | begin := start[c] + i*count[c]/nr_fold |
| 2169 | end := start[c] + (i+1)*count[c]/nr_fold |
| 2170 | for j := begin; j < end; j++ { |
| 2171 | perm[fold_start[i]] = index[j] |
| 2172 | fold_start[i]++ |
| 2173 | } |
| 2174 | } |
| 2175 | } |
| 2176 | fold_start[0] = 0 |
| 2177 | for i = 1; i <= nr_fold; i++ { |
| 2178 | fold_start[i] = fold_start[i-1] + fold_count[i-1] |
no test coverage detected