非线性【SVM】的创建和使用

先来绘制散点图:

from sklearn.datasets import make_circles
X,y = make_circles(100, factor=0.1, noise=.1)  # 100个样本,factor:内圈和外圈的距离之比,noise:噪声
X.shape
y.shape
plt.scatter(X[:,0],X[:,1],c=y,s=50,cmap="rainbow")
plt.show()

非线性【SVM】的创建和使用_第1张图片

接下来绘制决策边界:

clf = SVC(kernel = "rbf").fit(X,y) # 高斯核
plt.scatter(X[:,0],X[:,1],c=y,s=50,cmap="rainbow")
def plot_svc_decision_function(model,ax=None):
    if ax is None:
        ax = plt.gca()
    xlim = ax.get_xlim()
    ylim = ax.get_ylim()
    
    x = np.linspace(xlim[0],xlim[1],30)
    y = np.linspace(ylim[0],ylim[1],30)
    Y,X = np.meshgrid(y,x) 
    xy = np.vstack([X.ravel(), Y.ravel()]).T
    P = model.decision_function(xy).reshape(X.shape)
    
    ax.contour(X, Y, P,colors="k",levels=[-1,0,1],alpha=0.5,linestyles=["--","-","--"])
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
plot_svc_decision_function(clf)

非线性【SVM】的创建和使用_第2张图片

你可能感兴趣的:(支持向量机,机器学习,人工智能)