加载中...
SVM
发表于:2021-11-08 | 分类: 机器学习课程(魏)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

xs=np.arange(-5,5,0.2)
ys1=np.sqrt(25-xs*xs)+np.random.uniform(-0.5,0.5,(xs.shape[0]))
ys2=-1*np.sqrt(25-xs*xs)+np.random.uniform(-0.5,0.5,(xs.shape[0]))

xys1=np.c_[xs,ys1]

xys2=np.c_[xs,ys2]

xy1=np.r_[xys1,xys2]


xh=np.arange(-8,8,0.2)
yh1=np.sqrt(64-xh*xh)+np.random.uniform(-0.5,0.5,(xh.shape[0]))
yh2=-1*np.sqrt(64-xh*xh)+np.random.uniform(-0.5,0.5,(xh.shape[0]))

xyh1=np.c_[xh,yh1]

xyh2=np.c_[xh,yh2]

xy2=np.r_[xyh1,xyh2]

xy3=np.r_[xy1,xy2]
plt.figure()

plt.scatter(xy3[0:100,0],xy3[0:100,1],c='k',marker='*')
plt.scatter(xy3[100:,0],xy3[100:,1],c='r',marker='o')

fig=plt.figure()
ax=Axes3D(fig)

z1=xy3[:,0]**2
z2=xy3[:,1]**2
z3=xy3[:,1]
ax.scatter(z1[0:100],z2[0:100],z3[0:100],c='k',marker='*')
ax.scatter(z1[100:],z2[100:],z3[100:],c='r',marker='o')

plt.show()

c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages\ipykernel_launcher.py:34: MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure is deprecated since 3.4. Pass the keyword argument auto_add_to_figure=False and use fig.add_axes(ax) to suppress this warning. The default value of auto_add_to_figure will change to False in mpl3.5 and True values will no longer work in 3.6.  This is consistent with other Axes classes.

png

png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import numpy as np
import pandas as pd
from sklearn.datasets import make_moons
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.model_selection import cross_val_score as cr#调用交叉检验函数
n_samples = 600
x,y=make_moons(n_samples=n_samples, noise=0.1,random_state=3)
clf=svm.SVC(kernel='rbf',gamma=0.5,C=100)#poly代表分类线是曲线,degree是曲线的最高次幂
clf.fit(x,y)
#clf.predict([x])
xmin,xmax=x[:,0].min()-1,x[:,0].max()+1
ymin,ymax=x[:,1].min()-1,x[:,1].max()+1

xx,yy=np.meshgrid(np.arange(xmin,xmax,0.02),np.arange(ymin,ymax,0.02))

xf=np.c_[xx.ravel(),yy.ravel()];
z=clf.predict(xf)
z=z.reshape(xx.shape)
plt.pcolormesh(xx,yy,z,cmap=plt.cm.Pastel1)
plt.scatter(x[:,0],x[:,1],c=y)

print(cr(clf,x,y,cv=5,scoring="accuracy"))

plt.show()

[1.         0.99166667 1.         1.         1.        ]


c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages\ipykernel_launcher.py:20: MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3.  Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading'].  This will become an error two minor releases later.

png

1

上一篇:
KNN
下一篇:
主成分分析
本文目录
本文目录