| 250 | } |
| 251 | |
| 252 | vector<float_type> SVC::predict_label(const SyncArray<float_type> &dec_values, int n_instances) { |
| 253 | vector<float_type> predict_y; |
| 254 | const float_type *dec_values_data = dec_values.host_data(); |
| 255 | if (0 == param.probability) { |
| 256 | //predict y by voting among k(k-1)/2 models |
| 257 | for (int l = 0; l < n_instances; ++l) { |
| 258 | vector<int> votes(n_classes, 0); |
| 259 | int k = 0; |
| 260 | for (int i = 0; i < n_classes; ++i) { |
| 261 | for (int j = i + 1; j < n_classes; ++j) { |
| 262 | if (dec_values_data[l * n_binary_models + k] > 0) |
| 263 | votes[i]++; |
| 264 | else |
| 265 | votes[j]++; |
| 266 | k++; |
| 267 | } |
| 268 | } |
| 269 | int maxVoteClass = 0; |
| 270 | for (int i = 1; i < n_classes; ++i) { |
| 271 | if (votes[i] > votes[maxVoteClass]) |
| 272 | maxVoteClass = i; |
| 273 | } |
| 274 | predict_y.push_back((float) this->label[maxVoteClass]); |
| 275 | } |
| 276 | } else { |
| 277 | LOG(INFO) << "predict with probability"; |
| 278 | this->prob_predict.clear(); |
| 279 | for (int l = 0; l < n_instances; ++l) { |
| 280 | vector<vector<float_type> > r(n_classes, vector<float_type>(n_classes)); |
| 281 | float_type min_prob = 1e-7; |
| 282 | int k = 0; |
| 283 | for (int i = 0; i < n_classes; i++) |
| 284 | for (int j = i + 1; j < n_classes; j++) { |
| 285 | r[i][j] = min( |
| 286 | max(sigmoidPredict(dec_values_data[l * n_binary_models + k], probA[k], probB[k]), min_prob), |
| 287 | 1 - min_prob); |
| 288 | r[j][i] = 1 - r[i][j]; |
| 289 | k++; |
| 290 | } |
| 291 | vector<float_type> p(n_classes); |
| 292 | multiclass_probability(r, p); |
| 293 | this->prob_predict.insert(prob_predict.end(), p.begin(), p.end()); |
| 294 | int max_prob_class = 0; |
| 295 | for (int j = 0; j < n_classes; ++j) { |
| 296 | if (p[j] > p[max_prob_class]) |
| 297 | max_prob_class = j; |
| 298 | } |
| 299 | predict_y.push_back((float) this->label[max_prob_class]); |
| 300 | } |
| 301 | } |
| 302 | return predict_y; |
| 303 | } |
| 304 | |
| 305 | void sigmoidTrain(const float_type *decValues, const int l, const vector<int> &labels, float_type &A, |
| 306 | float_type &B) { |