machine-learning - scikit-learn 中的 SVC 和 LinearSVC 在什么参数下等效?

标签 machine-learning scikit-learn svm libsvm

我读到this thread关于 scikit-learn 中 SVC()LinearSVC() 之间的区别。

现在我有一个二元分类问题的数据集(对于这样的问题,两个函数之间的一对一/一对休息策略差异可以忽略。)

我想尝试在什么参数下这两个函数会给出相同的结果。首先,我们当然应该为SVC()设置kernel='linear' 但是,我无法从这两个函数中获得相同的结果。我无法从文档中找到答案,有人可以帮我找到我正在寻找的等效参数集吗?

更新: 我根据 scikit-learn 网站的示例修改了以下代码,显然它们不一样:

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2]  # we only take the first two features. We could
                      # avoid this ugly slicing by using a two-dim dataset
y = iris.target

for i in range(len(y)):
    if (y[i]==2):
        y[i] = 1

h = .02  # step size in the mesh

# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0  # SVM regularization parameter
svc = svm.SVC(kernel='linear', C=C).fit(X, y)
lin_svc = svm.LinearSVC(C=C, dual = True, loss = 'hinge').fit(X, y)

# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))

# title for the plots
titles = ['SVC with linear kernel',
          'LinearSVC (linear kernel)']

for i, clf in enumerate((svc, lin_svc)):
    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, m_max]x[y_min, y_max].
    plt.subplot(1, 2, i + 1)
    plt.subplots_adjust(wspace=0.4, hspace=0.4)

    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)

    # Plot also the training points
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
    plt.xlabel('Sepal length')
    plt.ylabel('Sepal width')
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.xticks(())
    plt.yticks(())
    plt.title(titles[i])

plt.show()

结果: Output Figure from previous code

最佳答案

从数学意义上来说,你需要设置:

SVC(kernel='linear', **kwargs) # by default it uses RBF kernel

LinearSVC(loss='hinge', **kwargs) # by default it uses squared hinge loss

另一个无法轻易修复的元素是在 LinearSVC 中增加 intercept_scaling,因为在此实现中偏差被正则化(这在 SVC 中不成立,也不应该在SVM - 因此这不是 SVM) - 因此它们将永远完全相等(除非您的问题的偏差=0),因为它们假设两个不同的模型

  • SVC:1/2||w||^2 + C SUM xi_i
  • LinearSVC:1/2||[w b]||^2 + C SUM xi_i

我个人认为 LinearSVC 是 sklearn 开发人员犯的错误之一 - 这个类根本不是线性 SVM

增加截距缩放后(至10.0)

SVMs

但是,如果你将其放大太多 - 它也会失败,因为现在容错性和迭代次数至关重要。

综上所述:LinearSVC不是线性SVM,非必要不要使用。

关于machine-learning - scikit-learn 中的 SVC 和 LinearSVC 在什么参数下等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33843981/

相关文章:

c# - 如何解决使用主成分分析引发的 OutOfMemoryException

python - 标准 1 误差的差距统计

machine-learning - libsvm 收缩启发法

r - 绘制 SVM 分类图时出错

machine-learning - 顺序模型中层的附加参数

TensorFlow - MNIST 数据中的训练准确性没有提高

scikit-learn - 当 y_score 不是数字时,sklearn roc_auc_score 失败并出现 ValueError

Python共享读内存

opencv - 用于上半身检测的经过训练的 HOG 描述符

python - 这个梯度下降算法有什么问题呢?