本文介绍了LinearSVC()与SVC(kernel ='linear')不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据偏移(不居中于零)时,LinearSVC()SVC(kernel='linear')给出的结果完全不同. (问题可能是它不处理非规范化数据.)

When data is offset (not centered in zero), LinearSVC() and SVC(kernel='linear') are giving awfully different results. ( the problem might be it does not handle non-normalized data.)

import matplotlib.pyplot as plot
plot.ioff()
import numpy as np
from sklearn.datasets.samples_generator import make_blobs
from sklearn.svm import LinearSVC, SVC


def plot_hyperplane(m, X):
    w = m.coef_[0]
    a = -w[0] / w[1]
    xx = np.linspace(np.min(X[:, 0]), np.max(X[:, 0]))
    yy = a*xx - (m.intercept_[0]) / w[1]
    plot.plot(xx, yy, 'k-')

X, y = make_blobs(n_samples=100, centers=2, n_features=2,
                  center_box=(0, 1))
X[y == 0] = X[y == 0] + 100
X[y == 1] = X[y == 1] + 110

for i, m in enumerate((LinearSVC(), SVC(kernel='linear'))):
    m.fit(X, y)
    plot.subplot(1, 2, i+1)
    plot_hyperplane(m, X)

    plot.plot(X[y == 0, 0], X[y == 0, 1], 'r.')
    plot.plot(X[y == 1, 0], X[y == 1, 1], 'b.')

    xv, yv = np.meshgrid(np.linspace(98, 114, 10), np.linspace(98, 114, 10))
    _X = np.c_[xv.reshape((xv.size, 1)), yv.reshape((yv.size, 1))]
    _y = m.predict(_X)

    plot.plot(_X[_y == 0, 0], _X[_y == 0, 1], 'r.', alpha=0.4)
    plot.plot(_X[_y == 1, 0], _X[_y == 1, 1], 'b.', alpha=0.4)

plot.show()

这是我得到的结果:

(left = LinearSVC(),right = SVC(kernel ='linear'))

(left=LinearSVC(), right=SVC(kernel='linear'))

sklearn.__version__ = 0.17.但是我也在0.15随附的Ubuntu 14.04中进行了测试.

sklearn.__version__ = 0.17. But I also tested in Ubuntu 14.04, which comes with 0.15.

我曾考虑过要报告该错误,但似乎太过明显,无法成为一个错误.我想念什么?

I thought about reporting the bug, but it seems too evident to be a bug. What am I missing?

推荐答案

在阅读文档时,他们正在使用不同的基础实现. LinearSVC使用liblinear,而SVC使用libsvm.

Reading the documentation, they are using different underlying implementations. LinearSVC is using liblinear where SVC is using libsvm.

仔细查看系数和截距,似乎LinearSVC将正则化应用于截距,而SVC则没有.

Looking closely at the coefficients and intercept, it seems LinearSVC applies regularization to the intercept where SVC does not.

通过添加intercept_scaling,我能够对两者获得相同的结果.

By adding intercept_scaling, I was able to obtain the same results to both.

LinearSVC(loss='hinge', intercept_scaling=1000)

这篇关于LinearSVC()与SVC(kernel ='linear')不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 07:06