from sklearn.datasets import make_circles
from sklearn.svm import SVC
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.linear_model import LogisticRegression
import numpy as np
#创建样本点
'''
n_samples:创建多少啊个样本
noise :噪音
factor :方差
'''
X,y=make_circles(noise=.1,factor=.1)
plt.scatter(X[:,0],X[:,1],c=y)
plt.axis('equal')
#画轮廓图
x1_min,x1_max = X[:,0].min()-1,X[:,0].max()+1
x2_min,x2_max = X[:,1].min()-1,X[:,1].max()+1
x1 = np.linspace(x1_min,x1_max,50)
x2 = np.linspace(x2_min,x2_max,50)
#生成网格
xx,yy = np.meshgrid(x1,x2)
#合并成数据
xy = np.c_[xx.ravel(),yy.ravel()]
逻辑回归
logs = LogisticRegression().fit(X,y)
y_pred = logs.predict(xy)
zz = y_pred.reshape(xx.shape)
#轮廓图要求的数据是二维的,等高线图
plt.contour(xx,yy,zz)
plt.scatter(X[:,0],X[:,1],c=y)
plt.axis('equal')
knn
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier().fit(X,y)
y_pred = knn.predict(xy)
zz = y_pred.reshape(xx.shape)
plt.contour(xx,yy,zz)
plt.scatter(X[:,0],X[:,1],c=y)
plt.axis('equal')
svc = SVC(kernel='rbf').fit(X,y)
y_pred = svc.predict(xy)
zz = svc.decision_function(xy).reshape(xx.shape)
plt.contour(xx,yy,zz,colors='k',levels=[-1,0,1],alpha=.5,linestyles=['--','-','--'])
plt.scatter(X[:,0],X[:,1],c=y)
plt.axis('equal')
#投影到三维
fig=plt.figure(figsize=(10,8))
#RBF的核心
z = np.exp(-(X**2).sum(axis=1))
ax3d = Axes3D(fig=fig)
ax3d.scatter3D(X[:,0],X[:,1],z,c=y,s=50,cmap='autumn')
ax3d.view_init(elev=0)
来源:CSDN
作者:♚木思风
链接:https://blog.csdn.net/weixin_44727383/article/details/104639262