DAkbariSE
DAkbariSE

Reputation: 133

Extracting 3 best matches using sklearn module

I'm working on sample code which uses face_recognition python library:

everything's fine except i want to fetch 3 best candidate instead of only the best one which is the result of

clf.predict([test_image_enc])

I expect output of [4411, 4455, 5566] instead of 4411

Upvotes: 1

Views: 49

Answers (1)

Venkatachalam
Venkatachalam

Reputation: 16966

Use the decision_function method to get the likelihood measure for each class. This computes the distance from the separating hyperplanes (higher this value, more likely the datapoint belonging to the corresponding class). Then you can pick the top_n number of classes as you wish.

top_n = 3
sort_inds =  clf.decision_function([test_image_enc]).argsort()
clf.classes_[sort_inds[0][-top_n:][::-1]]

Upvotes: 1

Related Questions