What is the difference between OneVsRestClassifier with SVC and SVC with decision_function_shape='ovr'?
Asked Answered
K

2

17

I thought it should be the same, but for method decision_function() I get different results. And SVC with only decision_function_shape='ovr' is really faster.

Related: Scikit learn multi-class classification for support vector machines

Kingcraft answered 20/9, 2016 at 22:6 Comment(2)
I have found these issues on github repo github.com/scikit-learn/scikit-learn/issues/10752 and github.com/scikit-learn/scikit-learn/issues/5495 which are maybe related, but I still don't really understand and it doesn't provide an explicit answer for OP's question.Contentious
Documentation provides some insight for OvO case, where it says that sklearn.svm.SVC supports Multiclass as One-Vs-One without need of using any meta-estimators (i.e. OneVsOneClassifier). However it is still not clear how should be SVC used in combination with OneVsRestClassifier when we do want to use meta-estimator and for example do OneVsRest multi-class classification. Also it is not clear what role plays decision_function_shape : ‘ovo’ / ‘ovr’ in all of this.Contentious
A
6

I got some clarification on the documentation of LinearSVC in the See also heading, where SVC is mentioned.

SVC

Implementation of Support Vector Machine classifier using libsvm:

....

....

Furthermore SVC multi-class mode is implemented using one vs one scheme while LinearSVC uses one vs the rest. It is possible to implement one vs the rest with SVC by using the sklearn.multiclass.OneVsRestClassifier wrapper.

....

Also, SVC delegates all the training to the underlying libsvm library, which handles the multi-class case as 'OvO' (even if the decision_function_shape = 'ovr').

Its mentioned in the issue @delusionX mentioned that decision_function_shape is just for compatibility with scikit API. Its most probably, that all other estimators handle the multi-class as OvR and so when SVC is used in combination with other things, (Like for example in a Pipeline, GridSearchCV, Or wrappers like OneVsRestClassifier) returning a OvO decision function breaks the working of others. But I could not find that written explicitly anywhere.

Fun fact: OneVsOneClassifier also returns a decision function which confirms with the shape of OvR.

Adrieneadrienne answered 10/4, 2018 at 7:34 Comment(0)
A
0

However, note that internally, one-vs-one (‘ovo’) is always used as a multi-class strategy to train models; an ovr matrix is only constructed from the ovo matrix. The parameter is ignored for binary classification.

From sklearn SVC official document https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html, it seems that the ovr strategy is not really implemented. The ovr results are inferred from the ovo output matrixes.

So I guess if you want to follow ovr strategy strictly, OneVsRestClassifier is a better choice.

Aniakudo answered 2/2 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.