Skip to content

Instantly share code, notes, and snippets.

@dniku
Forked from blackball/libsvm-predict-probability.c
Last active August 29, 2015 14:18
Show Gist options
  • Save dniku/e6ec0fd54813d5a7d3c5 to your computer and use it in GitHub Desktop.
Save dniku/e6ec0fd54813d5a7d3c5 to your computer and use it in GitHub Desktop.
double svm_predict_probability(
const svm_model *model, const svm_node *x, double *prob_estimates)
{
if ((model->param.svm_type == C_SVC || model->param.svm_type == NU_SVC) &&
model->probA!=NULL && model->probB!=NULL)
{
int i;
int nr_class = model->nr_class;
double *dec_values = Malloc(double, nr_class*(nr_class-1)/2);
svm_predict_values(model, x, dec_values);
double min_prob=1e-7;
double **pairwise_prob=Malloc(double *,nr_class);
for(i=0;i<nr_class;i++)
pairwise_prob[i]=Malloc(double,nr_class);
int k=0;
for(i=0;i<nr_class;i++)
for(int j=i+1;j<nr_class;j++)
{
pairwise_prob[i][j]=min(max(sigmoid_predict(dec_values[k],model->probA[k],model->probB[k]),min_prob),1-min_prob);
pairwise_prob[j][i]=1-pairwise_prob[i][j];
k++;
}
multiclass_probability(nr_class,pairwise_prob,prob_estimates);
int prob_max_idx = 0;
for(i=1;i<nr_class;i++)
if(prob_estimates[i] > prob_estimates[prob_max_idx])
prob_max_idx = i;
for(i=0;i<nr_class;i++)
free(pairwise_prob[i]);
free(dec_values);
free(pairwise_prob);
return model->label[prob_max_idx];
}
else
return svm_predict(model, x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment