使用 OneVsRestClassifier 时 sklearn.svm.SVC 的哪个 decision_function_shape? [英] Which decision_function_shape for sklearn.svm.SVC when using OneVsRestClassifier?

查看:54
本文介绍了使用 OneVsRestClassifier 时 sklearn.svm.SVC 的哪个 decision_function_shape?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做多标签分类,我试图预测问题的正确标签:

I am doing multi-label classification where I am trying to predict correct tags to questions:

(X = 问题,y = X 中每个问题的标签列表).

(X = questions, y = list of tags for each question from X).

我想知道 sklearn.svm.SVC 应与 OneVsRestClassifier?

I am wondering, which decision_function_shape for sklearn.svm.SVC should be be used with OneVsRestClassifier?

从文档我们可以读到 decision_function_shape 可以有两个值 'ovo''ovr':

From docs we can read that decision_function_shape can have two values 'ovo' and 'ovr':

decision_function_shape : ‘ovo’, ‘ovr’ 或 None, default=None

decision_function_shape : ‘ovo’, ‘ovr’ or None, default=None

是否像所有其他分类器一样返回形状为 (n_samples, n_classes) 的 one-vs-rest ('ovr') 决策函数,还是原始具有形状的 libsvm 的一对一('ovo')决策函数(n_samples, n_classes * (n_classes - 1)/2).默认的 None 将当前表现为ovo"以实现向后兼容性并引发弃用警告,但会在 0.19 中更改ovr".

Whether to return a one-vs-rest (‘ovr’) decision function of shape (n_samples, n_classes) as all other classifiers, or the original one-vs-one (‘ovo’) decision function of libsvm which has shape (n_samples, n_classes * (n_classes - 1) / 2). The default of None will currently behave as ‘ovo’ for backward compatibility and raise a deprecation warning, but will change ‘ovr’ in 0.19.

但我还是不明白两者有什么区别:

But I still don't understand what is the difference between:

# First decision_function_shape set to 'ovo'
estim = OneVsRestClassifier(SVC(kernel='linear', decision_function_shape ='ovo'))

# Second decision_function_shape set to 'ovr'
estim = OneVsRestClassifier(SVC(kernel='linear', decision_function_shape ='ovr'))

哪个 decision_function_shape 应该用于 多标签分类问题?

Which decision_function_shape should be used for multi-label classification problem?

问题 提出类似问题但没有答案.

Question asking a similar thing with no answer.

推荐答案

我认为应该使用哪个的问题最好由具体情况决定.这很容易成为您的 GridSearch 的一部分.但凭直觉我会觉得,就差异而言,你会做同样的事情.这是我的推理:

I think the question of which should be used is best left up to a situational. That could easily be a part of your GridSearch. But just intuitively I would feel that as far as differences go you are going to be doing the same thing. Here is my reasoning:

OneVsRestClassifier 旨在针对所有其他类独立地为每个类建模,并为每种情况创建一个分类器.我理解这个过程的方式是 OneVsRestClassifier 抓取一个类,并为一个点是否属于该类创建一个二进制标签.然后这个标签被输入到你选择使用的任何估计器中.我相信混淆来自 SVC 也允许您做出相同的选择,但实际上对于此实现,选择无关紧要,因为您将始终只将两个类输入 SVC.

OneVsRestClassifier is designed to model each class against all of the other classes independently, and create a classifier for each situation. The way I understand this process is that OneVsRestClassifier grabs a class, and creates a binary label for whether a point is or isn't that class. Then this labelling gets fed into whatever estimator you have chosen to use. I believe the confusion comes in in that SVC also allows you to make this same choice, but in effect with this implementation the choice will not matter because you will always only be feeding two classes into the SVC.

这是一个例子:

from sklearn.datasets import load_iris
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC

data = load_iris()

X, y = data.data, data.target
estim1 = OneVsRestClassifier(SVC(kernel='linear', decision_function_shape='ovo'))
estim1.fit(X,y)

estim2 = OneVsRestClassifier(SVC(kernel='linear', decision_function_shape='ovr'))
estim2.fit(X,y)

print(estim1.coef_ == estim2.coef_)
array([[ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True]], dtype=bool)

因此,您可以看到两个模型构建的所有三个估计量的系数都相等.假设这个数据集只有 150 个样本和 3 个类,因此对于更复杂的数据集,这些结果可能会有所不同,但这是一个简单的概念证明.

So you can see the coefficients are all equal for all three estimators built by the two models. Granted this dataset only has 150 samples and 3 classes so it is possible these results could be different for a more complex dataset, but it's a simple proof of concept.

这篇关于使用 OneVsRestClassifier 时 sklearn.svm.SVC 的哪个 decision_function_shape?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆