| 2943 | } |
| 2944 | |
| 2945 | double |
| 2946 | svm_predict_probability(const svm_model* model, |
| 2947 | const svm_node* x, |
| 2948 | double* prob_estimates) |
| 2949 | { |
| 2950 | if ((model->param.svm_type == C_SVC || model->param.svm_type == NU_SVC) && |
| 2951 | model->probA != nullptr && model->probB != nullptr) { |
| 2952 | int nr_class = model->nr_class; |
| 2953 | double* dec_values = Malloc(double, nr_class*(nr_class - 1) / 2); |
| 2954 | svm_predict_values(model, x, dec_values); |
| 2955 | |
| 2956 | double min_prob = 1e-7; |
| 2957 | double** pairwise_prob = Malloc(double*, nr_class); |
| 2958 | |
| 2959 | for (int i = 0; i < nr_class; i++) |
| 2960 | pairwise_prob[i] = Malloc(double, nr_class); |
| 2961 | |
| 2962 | int k = 0; |
| 2963 | |
| 2964 | for (int i = 0; i < nr_class; i++) |
| 2965 | for (int j = i + 1; j < nr_class; j++) { |
| 2966 | pairwise_prob[i][j] = |
| 2967 | min(max(sigmoid_predict(dec_values[k], model->probA[k], model->probB[k]), |
| 2968 | min_prob), |
| 2969 | 1 - min_prob); |
| 2970 | pairwise_prob[j][i] = 1 - pairwise_prob[i][j]; |
| 2971 | k++; |
| 2972 | } |
| 2973 | |
| 2974 | multiclass_probability(nr_class, pairwise_prob, prob_estimates); |
| 2975 | |
| 2976 | int prob_max_idx = 0; |
| 2977 | |
| 2978 | for (int i = 1; i < nr_class; i++) |
| 2979 | if (prob_estimates[i] > prob_estimates[prob_max_idx]) |
| 2980 | prob_max_idx = i; |
| 2981 | |
| 2982 | for (int i = 0; i < nr_class; i++) |
| 2983 | free(pairwise_prob[i]); |
| 2984 | |
| 2985 | free(dec_values); |
| 2986 | |
| 2987 | free(pairwise_prob); |
| 2988 | |
| 2989 | return model->label[prob_max_idx]; |
| 2990 | } |
| 2991 | return svm_predict(model, x); |
| 2992 | } |
| 2993 | |
| 2994 | static const char* svm_type_table[] = { |
| 2995 | "c_svc", "nu_svc", "one_class", "epsilon_svr", "nu_svr", nullptr}; |
no test coverage detected