RBF核函数中的gamma

gamma越大,高斯分布越窄。gamma越小,高斯分布越宽,gamma相当于调整模型的复杂度,gamma值越小模型复杂度越低,gamma值越高,模型复杂度越大

#!/usr/bin/python
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from sklearn import datasets
import pandas as pd
import numpy as np
x,y = datasets.make_moons()
print(x.shape)
print(y.shape)
plt.scatter(x[y==0,0],x[y==0,1],color="red")
plt.scatter(x[y==1,0],x[y==1,1],color="blue")
plt.show()

   

#为数据添加随机的噪音

x,y = datasets.make_moons(noise=0.15,random_state=666)
plt.scatter(x[y==0,0],x[y==0,1],color="red")
plt.scatter(x[y==1,0],x[y==1,1],color="blue")
plt.show()

用SVM算法使用多项式特征的方法处理不规则的数据集,由于上面的几步都需要顺序的执行,因此引入pipline函数

from sklearn.pipeline import Pipeline
def RBFKernelSVC(gamma):
    from  sklearn.svm import SVC

    std_scaler = StandardScaler()
    SVC = SVC(kernel = "rbf",gamma = gamma)
    pipeline = Pipeline([('std_scaler',std_scaler),('SVC', SVC)])
    return pipeline
poly_svc = RBFKernelSVC(gamma=1.0)
poly_svc.fit(x,y)
def plot_decision_boundary(model,axis):
    x0,x1 = np.meshgrid(
        np.linspace(axis[0],axis[1],int((axis[1]-axis[0])*100)).reshape(-1,1),
        np.linspace(axis[0], axis[1], int((axis[1] - axis[0]) * 100)).reshape(-1,1))
    x_new = np.c_[x0.ravel(),x1.ravel()]

    y_predict = model.predict(x_new)
    zz = y_predict.reshape(x0.shape)

    from matplotlib.colors import ListedColormap
    custom_cmap = ListedColormap(["#EF9A9A","#FFF59D","#90CAF9"])
    plt.contourf(x0,x1,zz,linewidth=5,cmap=custom_cmap)

当gamma=1.0时

plot_decision_boundary(poly_svc,axis=[-1.5,2.5,-1.0,1.5])
plt.scatter(x[y == 0, 0], x[y == 0, 1], color="red")
plt.scatter(x[y == 1, 0], x[y == 1, 1], color="blue")
plt.show()

当gamma=100时

SVC_gamma_100 = RBFKernelSVC(gamma=100)
SVC_gamma_100.fit(x,y)
plot_decision_boundary(SVC_gamma_100,axis=[-1.5,2.5,-1.0,1.5])
plt.scatter(x[y == 0, 0], x[y == 0, 1], color="red")
plt.scatter(x[y == 1, 0], x[y == 1, 1], color="blue")
plt.show()

当gamma=10时

SVC_gamma_10 = RBFKernelSVC(gamma=10)
SVC_gamma_10.fit(x,y)
plot_decision_boundary(SVC_gamma_10,axis=[-1.5,2.5,-1.0,1.5])
plt.scatter(x[y == 0, 0], x[y == 0, 1], color="red")
plt.scatter(x[y == 1, 0], x[y == 1, 1], color="blue")
plt.show()

猜你喜欢

转载自blog.csdn.net/ITpfzl/article/details/82831301
rbf