Tôi đã đọc số this thread về sự khác biệt giữa SVC()
và LinearSVC()
trong việc tìm hiểu.Theo các tham số nào là SVC và LinearSVC trong tương đương scikit-learn?
Bây giờ tôi có một tập hợp dữ liệu về vấn đề phân loại nhị phân (Đối với một vấn đề như vậy, sự khác biệt chiến lược one-to-one/one-to-nghỉ ngơi giữa hai chức năng có thể được bỏ qua.)
tôi muốn thử theo những thông số nào thì 2 hàm này sẽ cho tôi kết quả tương tự. Trước hết, tất nhiên, chúng ta nên đặt kernel='linear'
cho SVC()
Tuy nhiên, tôi không thể nhận được kết quả tương tự từ cả hai hàm. Tôi không thể tìm thấy câu trả lời từ các tài liệu, bất cứ ai có thể giúp tôi để tìm các thiết lập tham số tương đương tôi đang tìm?
Cập nhật: tôi sửa đổi đoạn mã sau từ một ví dụ của trang web scikit-học, và dường như họ không giống nhau:
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()
Kết quả: Output Figure from previous code
Có, tôi cũng đã thử tham số 'loss = 'hinge'' này, nhưng chúng vẫn không cho tôi kết quả giống nhau (hoặc thậm chí gần) .... – Sidney
xem câu trả lời được cập nhật, phức tạp hơn – lejlot