def draw_line(coef,intercept, mi, ma):
# for the separating hyper plane ax+by+c=0, the weights are [a, b] and the intercept is c
# to draw the hyper plane we are creating two points
# 1. ((b*min-c)/a, min) i.e ax+by+c=0 ==> ax = (-by-c) ==> x = (-by-c)/a here in place of y we are keeping the minimum value of y
# 2. ((b*max-c)/a, max) i.e ax+by+c=0 ==> ax = (-by-c) ==> x = (-by-c)/a here in place of y we are keeping the maximum value of y
points=np.array([[((-coef[1]*mi - intercept)/coef[0]), mi],[((-coef[1]*ma - intercept)/coef[0]), ma]])
plt.plot(points[:,0], points[:,1])
def svm_margin(c):
ratios = [(100,2), (100, 20), (100, 40), (100, 80)]
plt.figure(figsize=(20,5))
for j,i in enumerate(ratios):
plt.subplot(1, 4, j+1)
X_p=np.random.normal(0,0.05,size=(i[0],2))
X_n=np.random.normal(0.13,0.02,size=(i[1],2))
y_p=np.array([1]*i[0]).reshape(-1,1)
y_n=np.array([0]*i[1]).reshape(-1,1)
X=np.vstack((X_p,X_n))
y=np.vstack((y_p,y_n))
plt.scatter(X_p[:,0],X_p[:,1],color='yellow')
plt.scatter(X_n[:,0],X_n[:,1],color='red')
###SVM
clf = SVC(kernel='linear',C=c)
clf.fit(X,y)
coefficient = clf.coef_[0]
intercept = clf.intercept_
margin = 1 / (np.sqrt(np.sum(clf.coef_ ** 2)))
draw_line(coefficient,intercept,min(X[:,1]),max(X[:,1]))
### Intercept for parallel hyper place is (intercept +/- 1)
draw_line(coefficient,intercept - margin * np.sqrt(np.sum(clf.coef_ ** 2)) ,min(X[:,1]),max(X[:,1]))
draw_line(coefficient,intercept + margin * np.sqrt(np.sum(clf.coef_ ** 2)) ,min(X[:,1]),max(X[:,1]))
###https://scikit-learn.org/stable/auto_examples/svm/plot_svm_margin.html
plt.scatter(X[clf.support_][:,0],X[clf.support_][:,1],facecolors='none',edgecolors='k')
plt.suptitle('SVM Margin Hyperplane For C = ' + str(c))
plt.show()
svm_margin(0.001)
svm_margin(1)
svm_margin(100)
尝试将其扩展到 3 维系统,输出由上述代码生成以供参考: