#导入库
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import make_circles #环形数据
from sklearn.datasets import make_moons #月亮形数据
def contourf(x1,model):
# 要画决策边界,必须要有网格
axis = np.linspace(min(x1[:,0]),max(x1[:,0]),100)
ayis = np.linspace(min(x1[:,1]),max(x1[:,1]),100)
azis = np.linspace(min(-(x1**2).sum(1)),max(-(x1**2).sum(1)),100)
# 将特征向量转换为特征矩阵的函数
# 核心是将两个特征向量广播,以便获取x.shape*y.shape这么多点的横坐标和纵坐标
# 获取x.shape*y.shape这么多个坐标点
x,y = np.meshgrid(axis,ayis)#做成x行y列的网格
# xy就是形成的网络,遍及在画布上密集的点
XY = np.vstack([x.ravel(),y.ravel()]).T# ravel()降维函数,把每一行都放在第一行,vstack能够将多个结构一致的一维数组按行堆叠起来
y_predict = model.predict(XY)
pre=y_predict.reshape(x.shape)#预测结果
z=np.vstack([np.exp(-(x**2)-(y**2)).ravel()]).T.reshape(x.shape)
h= model.decision_function(XY).reshape(x.shape)#每个点到超平面的距离
# plt.scatter(x1[:,0],x1[:,1],marker='o',c=y1)
return x,y,z,pre,h
%matplotlib notebook
plt.figure(figsize=(12,6))#长宽比例
# model=svm.SVC(C=1.0,kernel='linear').fit(x1,y1)
model1=svm.SVC(C=0.5,kernel='rbf').fit(x1,y1)
plt.subplot(241)#将画板分为一行两列 取第一个
x1,y1=make_circles(n_samples=100,factor=0.5,noise=0.1)
plt.title('make_circles function')
plt.scatter(x1[:,0],x1[:,1],marker='o',c=y1,s=5)
plt.subplot(242)#将画板分为一行两列 取第一个
plt.scatter(x1[:,0],np.exp(-(x1**2).sum(1)),marker='o',c=y1,s=5)
ax1 = plt.subplot(243,projection="3d")
ax1.scatter3D(x1[:,0],x1[:,1],np.exp(-(x1**2).sum(1)),c=y1,s=10,cmap='rainbow')
ax1.view_init(elev=30,azim=30)
plt.title('make_circles 3D')
ax1 = plt.subplot(244,projection="3d")
x,y,z,pre,h=contourf(x1,model1)
ax1.contour(x,y,h,colors="k",levels=[-1,0,1] #画三条等高线,分别是Z为-1,Z为0和Z为1的三条线
,alpha=0.5#透明度
,linestyles=["--","-","--"])
ax1.plot_surface(x,y,pre,rstride=1, cstride=1, cmap='rainbow',alpha=0.5) # 画面
ax1.scatter3D(x1[:,0],x1[:,1],np.exp(-(x1**2).sum(1)),c=y1,s=10,cmap='rainbow')
ax1.view_init(elev=90,azim=90)#elev上下旋转 azim左右旋转
plt.show()
from sklearn.preprocessing import PolynomialFeatures #导入多项式回归
from sklearn.pipeline import Pipeline #导入python里的管道
from sklearn.preprocessing import StandardScaler
def RBFKernelSVC(gamma=1.0):
return Pipeline([
('std_scaler',StandardScaler()),
('svc',svm.SVC(kernel='rbf',gamma=gamma))
])
model2=RBFKernelSVC(gamma=4)#gamma参数很重要,gamma参数越大,支持向量越小
model2.fit(x2,y2)
plt.subplot(245)#将画板分为一行两列 取第一个
x2,y2=make_moons(n_samples=100,noise=0.1)
plt.title('make_moons function')
plt.scatter(x2[:,0],x2[:,1],marker='o',c=y2,s=5)
plt.subplot(246)#将画板分为一行两列 取第一个
plt.scatter(x2[:,1],np.exp(-(x2**2).sum(1)),marker='o',c=y2,s=5)
ax2 = plt.subplot(247,projection="3d")
ax2.scatter3D(x2[:,0],x2[:,1],np.exp(-(x2**2).sum(1)),c=y2,s=10,cmap='rainbow')
ax2.view_init(elev=90,azim=0)#elev上下旋转 azim左右旋转
plt.title('make_moons 3D')
ax2 = plt.subplot(248,projection="3d")
x,y,z,pre,h=contourf(x2,model2)
ax2.contour(x,y,h,colors="k",levels=[-1,0,1] #画三条等高线,分别是Z为-1,Z为0和Z为1的三条线
,alpha=0.5#透明度
,linestyles=["--","-","--"])
ax2.plot_surface(x,y,pre,rstride=1, cstride=1, cmap='rainbow',alpha=0.5) # 画面
ax2.scatter3D(x2[:,0],x2[:,1],np.exp(-(x2**2).sum(1)),c=y2,s=10,cmap='rainbow')
ax2.view_init(elev=90,azim=0)#elev上下旋转 azim左右旋转
plt.show()
SVC对环形数据和月亮形数据分类
最新推荐文章于 2023-10-24 23:24:04 发布