LinearSVC 和 SVC(kernel="linear") 有什么区别?
What is the difference between LinearSVC and SVC(kernel="linear")?
我找到了 sklearn.svm.LinearSVC
and sklearn.svm.SVC(kernel='linear')
,他们看起来和我很相似,但我在路透社上得到的结果却截然不同。
sklearn.svm.LinearSVC: 81.05% in 28.87s train / 9.71s test
sklearn.svm.SVC : 33.55% in 6536.53s train / 2418.62s test
两者都有一个线性内核。 LinearSVC 的公差比 SVC 高:
LinearSVC(C=1.0, tol=0.0001, max_iter=1000, penalty='l2', loss='squared_hinge', dual=True, multi_class='ovr', fit_intercept=True, intercept_scaling=1)
SVC (C=1.0, tol=0.001, max_iter=-1, shrinking=True, probability=False, cache_size=200, decision_function_shape=None)
这两个函数有何不同?即使我设置了kernel='linear
、tol=0.0001
、max_iter=1000 and
decision_function_shape='ovr'the
SVCtakes much longer than
LinearSVC`。为什么?
我使用 sklearn 0.18
,两者都包含在 OneVsRestClassifier
中。我不确定这是否与 multi_class='ovr'
/ decision_function_shape='ovr'
.
相同
确实,LinearSVC
和 SVC(kernel='linear')
会产生不同的结果,即。 e.指标得分和决策边界,因为它们使用不同的方法。下面的玩具示例证明了这一点:
from sklearn.datasets import load_iris
from sklearn.svm import LinearSVC, SVC
X, y = load_iris(return_X_y=True)
clf_1 = LinearSVC().fit(X, y) # possible to state loss='hinge'
clf_2 = SVC(kernel='linear').fit(X, y)
score_1 = clf_1.score(X, y)
score_2 = clf_2.score(X, y)
print('LinearSVC score %s' % score_1)
print('SVC score %s' % score_2)
--------------------------
>>> 0.96666666666666667
>>> 0.98666666666666669
这种差异的主要原则如下:
- 默认缩放,
LinearSVC
最小化平方铰链损失,而 SVC
最小化常规铰链损失。可以在 LinearSVC
. 中为 loss
参数手动定义 'hinge' 字符串
LinearSVC
使用一对多(也称为 One-vs-Rest) multiclass reduction while SVC
uses the One-vs-One multiclass reduction. It is also noted here。此外,对于多 class class 化问题 SVC
适合N * (N - 1) / 2
模型,其中 N
是 classes 的数量。相比之下,LinearSVC
仅适合 N
模型。如果 class 化问题是二元的,那么只有一个模型适用于两种情况。multi_class
和 decision_function_shape
参数没有任何共同点。第二个是聚合器,它将决策函数的结果转换为方便的形状 (n_features, n_samples)
。multi_class
是一种建立解决方案的算法方法。
LinearSVC
的基础估计量是 liblinear,它实际上会惩罚截距。 SVC
使用 libsvm 估计器,但不使用。 liblinear 估计器针对线性(特殊)情况进行了优化,因此在大量数据上收敛速度比 libsvm 更快。这就是 LinearSVC
解决问题所需时间更少的原因。
事实上,LinearSVC
在截距缩放后实际上并不是线性的,正如评论部分所述。
它们之间的主要区别是 linearsvc 只允许您选择线性分类器,而 svc 允许您从各种非线性分类器中进行选择。但是不建议将 svc 用于非线性问题,因为它们非常慢。尝试导入其他库来进行非线性分类。
现在,即使在定义 kernel='linear' 之后,我们也没有得到相同的输出,这是因为 linearsvc 和 svc 在进行背景数学计算时尝试了不同的方法。 linearsvc 的工作原理也是一对一,svc 的工作原理是一对一。
我希望这能回答你的问题。
我找到了 sklearn.svm.LinearSVC
and sklearn.svm.SVC(kernel='linear')
,他们看起来和我很相似,但我在路透社上得到的结果却截然不同。
sklearn.svm.LinearSVC: 81.05% in 28.87s train / 9.71s test
sklearn.svm.SVC : 33.55% in 6536.53s train / 2418.62s test
两者都有一个线性内核。 LinearSVC 的公差比 SVC 高:
LinearSVC(C=1.0, tol=0.0001, max_iter=1000, penalty='l2', loss='squared_hinge', dual=True, multi_class='ovr', fit_intercept=True, intercept_scaling=1)
SVC (C=1.0, tol=0.001, max_iter=-1, shrinking=True, probability=False, cache_size=200, decision_function_shape=None)
这两个函数有何不同?即使我设置了kernel='linear
、tol=0.0001
、max_iter=1000 and
decision_function_shape='ovr'the
SVCtakes much longer than
LinearSVC`。为什么?
我使用 sklearn 0.18
,两者都包含在 OneVsRestClassifier
中。我不确定这是否与 multi_class='ovr'
/ decision_function_shape='ovr'
.
确实,LinearSVC
和 SVC(kernel='linear')
会产生不同的结果,即。 e.指标得分和决策边界,因为它们使用不同的方法。下面的玩具示例证明了这一点:
from sklearn.datasets import load_iris
from sklearn.svm import LinearSVC, SVC
X, y = load_iris(return_X_y=True)
clf_1 = LinearSVC().fit(X, y) # possible to state loss='hinge'
clf_2 = SVC(kernel='linear').fit(X, y)
score_1 = clf_1.score(X, y)
score_2 = clf_2.score(X, y)
print('LinearSVC score %s' % score_1)
print('SVC score %s' % score_2)
--------------------------
>>> 0.96666666666666667
>>> 0.98666666666666669
这种差异的主要原则如下:
- 默认缩放,
LinearSVC
最小化平方铰链损失,而SVC
最小化常规铰链损失。可以在LinearSVC
. 中为 LinearSVC
使用一对多(也称为 One-vs-Rest) multiclass reduction whileSVC
uses the One-vs-One multiclass reduction. It is also noted here。此外,对于多 class class 化问题SVC
适合N * (N - 1) / 2
模型,其中N
是 classes 的数量。相比之下,LinearSVC
仅适合N
模型。如果 class 化问题是二元的,那么只有一个模型适用于两种情况。multi_class
和decision_function_shape
参数没有任何共同点。第二个是聚合器,它将决策函数的结果转换为方便的形状(n_features, n_samples)
。multi_class
是一种建立解决方案的算法方法。LinearSVC
的基础估计量是 liblinear,它实际上会惩罚截距。SVC
使用 libsvm 估计器,但不使用。 liblinear 估计器针对线性(特殊)情况进行了优化,因此在大量数据上收敛速度比 libsvm 更快。这就是LinearSVC
解决问题所需时间更少的原因。
loss
参数手动定义 'hinge' 字符串
事实上,LinearSVC
在截距缩放后实际上并不是线性的,正如评论部分所述。
它们之间的主要区别是 linearsvc 只允许您选择线性分类器,而 svc 允许您从各种非线性分类器中进行选择。但是不建议将 svc 用于非线性问题,因为它们非常慢。尝试导入其他库来进行非线性分类。
现在,即使在定义 kernel='linear' 之后,我们也没有得到相同的输出,这是因为 linearsvc 和 svc 在进行背景数学计算时尝试了不同的方法。 linearsvc 的工作原理也是一对一,svc 的工作原理是一对一。
我希望这能回答你的问题。