gpt4 book ai didi

python - 为什么支持向量机二元分类中的决策边界在我的图上重叠?

转载 作者:行者123 更新时间:2023-11-30 09:15:26 25 4
gpt4 key购买 nike

我正在使用 scikit 执行二元分类。就预测而言,一切似乎都是有序的,但是当我绘制决策边界时,决策边界是重叠的(参见绘图)。现在我意识到 MULTICLASS SVM 将不可避免地导致决策边界重叠,但为什么二元 SVM 分类会出现这种情况呢?据我所知,它们永远不应该重叠,因为空间被分成两部分。那么知道为什么我的绘图看起来如此困惑并且有如此多不同的颜色,而应该只有两种颜色吗?是我的阴谋吗?谢谢。

Updated Picture with subplots

def createSVMandPlot(X,y,x_name,y_name):

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) #1 vs 1
rbf_svc = svm.SVC(kernel='rbf', gamma='scale', C=C).fit(X, y) #1v1
poly_svc = svm.SVC(kernel='poly', degree=3, gamma='scale',C=C).fit(X, y) #1v1
lin_svc = svm.LinearSVC(C=C).fit(X, y) #1 vs rest

print(str(x_name)+' vs. '+str(y_name))
for i, clf in enumerate((svc, lin_svc, rbf_svc, poly_svc)):

X_pred=clf.predict(X)
X_pred1=np.asarray(X_pred).reshape(len(X_pred),1)
A=confusion_matrix(X_pred1, y)
print(A)
c=0
for r in range(len(X_pred)):
if X_pred[r]==y[r]:
c+=1

print(str(c)+' out of 34 predicted correctly (true positives)')


=============================================================================
with warnings.catch_warnings():

warnings.filterwarnings("ignore")
=============================================================================


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 w/ linear kernel',
'LinearSVC (w/ linear kernel)',
'SVM w/ RBF kernel',
'SVM w/ poly(degree 3) kernel']

plt.pause(7)
for i, clf in enumerate((svc, lin_svc, rbf_svc, poly_svc)):
# point in the mesh [x_min, x_max]x[y_min, y_max].
plt.subplot(2, 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, alpha=.5)

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], s=13,c=y)
plt.xlabel(x_name)
plt.ylabel(y_name)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.title(titles[i])

plt.show()

最佳答案

因为你有四个不同的支持向量机:

svc、rbf_svc、poly_svc 和 lin_svc

并且您正在迭代地绘制所有这些。这就是您看到重叠边界的原因,因为同一个图中显示了 4 个不同的边界

关于python - 为什么支持向量机二元分类中的决策边界在我的图上重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57296728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com