First time here? Check out the FAQ!

Ask Your Question
0

Confidence value for svm model

asked Dec 2 '16

Akash Garg gravatar image

I am using SVM algorithm to recognize Facial Expression using opencv library. I want to get prediction confidence of my test image but the syntax of predict is something like float response = svm->predict(TestDataMat). How can get the confidence value?? Thanks in advance!

Preview: (hide)

1 answer

Sort by » oldest newest most voted
5

answered Dec 2 '16

updated Dec 20 '16

Add the second parameter to return the prediction value

decision = svm.predict(testData, true);
confidence = 1.0 / (1.0 + exp(-decision));

The distance itself is pretty ok, but if you want to have a confidence value in a range (0, 1), you can apply a sigmoidal function to the result, which could be a logistic function.

ABOVE WORKS FOR 2.4


THIS WORKS FOR 3.x

svm->predict(data_test, labels_SVM, ml::StatModel::RAW_OUTPUT);

Preview: (hide)

Comments

1

Thanks a lot!

Akash Garg gravatar imageAkash Garg (Dec 2 '16)edit

You are welcome!

StevenPuttemans gravatar imageStevenPuttemans (Dec 5 '16)edit

Does this work in OpenCV 3.x? The StatModel docs suggest that the signature of predict() has changed, and I can't find whether it's still possible to get the confidence for SVM.

Joseph Howse gravatar imageJoseph Howse (Dec 20 '16)edit

Actually, you might be on to something ... just tried float conf = svm.predict(data_test, labels_SVM, true); cerr << conf << endl; and the value is always 0 ... because the function with parameters described above is indeed missing ...

StevenPuttemans gravatar imageStevenPuttemans (Dec 20 '16)edit
1

Just added a solution for 3.x ;)

StevenPuttemans gravatar imageStevenPuttemans (Dec 20 '16)edit
1

Thanks, you are correct. Also, it's worth noting that the distance is only returned in the case of 2-class classification, and only when the SVM type is C_SVC or NU_SVC. (This limitation is the same in 2.4 and 3.x.) Otherwise, just the classification is returned.

Joseph Howse gravatar imageJoseph Howse (Dec 20 '16)edit

Hi, do you have an Idea of how to get the probability of a sample belonging to each class? A bit like (confidence_i for all i in classes) ? Thank you in advance :)

DRomeroG gravatar imageDRomeroG (May 11 '18)edit

@berak might be able to hop in here, been some time since I worked on this

StevenPuttemans gravatar imageStevenPuttemans (May 14 '18)edit
1

only idea i have here is: instead of a multi-class SVM, use N one-against-all-others svm's

berak gravatar imageberak (May 14 '18)edit

Question Tools

3 followers

Stats

Asked: Dec 2 '16

Seen: 2,882 times

Last updated: Dec 20 '16