【机器学习】SVM支持向量机

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)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值