Jack Simpson
Jack Simpson

Reputation: 1711

OpenCV SVM prediction confidence with 3 classes

I really need to know the confidence of my prediction, and OpenCV's SVM predict method does give me the option of "returnDFVal":

returnDFVal – Specifies a type of the return value. If true and the problem is 2-class classification then the method returns the decision function value that is signed distance to the margin, else the function returns a class label (classification) or estimated function value (regression).

Unfortunately, I have 3 classes, so this doesn't work for me. Is there any way I can get around this or another method I can call to determine the confidence of my prediction?

Upvotes: 3

Views: 1304

Answers (2)

Milind Deore
Milind Deore

Reputation: 3063

Prediction probabilities are not possible yet, but there is a way to get it from under the hood libsvm, please find my answer here.

Upvotes: 2

Cynichniy Bandera
Cynichniy Bandera

Reputation: 6103

With opencv 3.x:

float distanceSample(cv::Mat &sample)
{
        assert(svm != NULL && svm->isTrained());
        assert(!sample.empty());

        cv::Mat result;
        svm->predict(sample, result, cv::ml::StatModel::Flags::RAW_OUTPUT);
        float dist = result.at<float>(0, 0);
        return dist;
}

...

float dist = distanceSample(yourSample);
float confidence = (1.0 / (1.0 + exp(-dist)));

PS. This works only for 2-classes classification.

Upvotes: 1

Related Questions