LinearSVC () is different from SVC (kernel = 'linear')

When the data is offset (not centered to zero), LinearSVC() and SVC(kernel='linear') give very different results. (EDIT: The problem may be that it is not processing abnormal 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() 

As a result, I get:

bug

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

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

I thought about how to report an error, but it seems too obvious that this is an error. What am I missing?

+4
source share
1 answer

Reading the documentation, they use various basic implementations. LinearSVC uses liblinear, where SVC uses libsvm.

LinearSVC looking carefully at the coefficients and interception, it seems that LinearSVC applies regularization to interception, where SVC does not work.

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

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

Matching after intercept scaling

0
source

Source: https://habr.com/ru/post/1241773/


All Articles