SVM_Multi_class_classification

import numpy as np 
#产生正态分布的数据100组,中心点(0,0),其标准差σ为1
p=np.random.randn(100,2)
#将中心点移动到(5,0),作为第0类
for i in range(100):
    p[i][0]+=5
    p[i][1]+=0

#产生正态分布的数据100组,中心点(0,0),其标准差σ为1,作为第1类
f=np.random.randn(100,2)
#产生正态分布的数据100组,中心点(0,0),其标准差σ为1
t=np.random.randn(100,2)
#将中心点移动到(3.5,3.5),作为第2类
for i in range(100):
    t[i][0]+=3.5
    t[i][1]+=3.5
import pandas as pd 

#将np数组转换成dataframe
df_p=pd.DataFrame(p,columns=['x','y'])
#加上标签z,1类标签1
df_p['z']=0

#将np数组转换成dataframe
df_f=pd.DataFrame(f,columns=['x','y'])
#加上标签z,0类标签0
df_f['z']=1


#将np数组转换成dataframe
df_t=pd.DataFrame(t,columns=['x','y'])
#加上标签z,2类标签2
df_t['z']=2

#将正负类合并成一个dataframe
res = pd.concat([df_p, df_f,df_t], axis=0)
res
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
xyz
06.078669-0.5172530
15.3029861.1299880
25.0096131.2258570
33.758161-0.3157860
45.360210-0.3751850
55.710155-0.4967860
64.579099-0.5316890
72.6496971.8354990
85.276772-1.7177790
94.168785-0.9013160
103.744492-0.2818520
114.795925-1.4386460
124.3955461.4700290
136.5048950.1077170
144.669277-1.8024860
155.177591-0.7156110
166.100084-0.8030930
174.1730740.8490820
184.6468880.0505250
195.725629-0.3457510
203.8985790.5871480
216.102218-2.2506280
224.7799401.6487520
232.945206-0.1560920
243.804919-1.2263930
253.148943-0.8536850
264.4805892.0140210
275.5602752.1377620
286.8879211.9439660
296.2275690.0283830
............
704.1434352.5467192
712.6044953.2929012
724.1218992.6664322
734.0442383.7754742
742.6036284.1731382
753.4753923.3774592
762.9862264.4870692
773.5822204.4753102
782.4366924.9180582
794.9170403.6065412
803.1482973.0484532
814.4731444.6192932
825.1544844.3729032
833.7073973.6683512
844.4425232.4973382
852.1012593.2251322
863.7876364.1481012
873.3192003.0411852
883.4162342.5222392
893.4066663.0706932
904.1422042.9089482
914.9550182.4516652
924.2495492.1854922
935.7284653.3433372
943.2415532.2286392
952.2389723.6395552
962.0750773.6507592
973.9082562.8552012
983.3049402.7217212
993.3520083.7687982

300 rows × 3 columns

import matplotlib.pyplot as plt
#绘制出数据集的散点图
plt.scatter(res['x'], res['y'], c=res['z'],cmap=plt.cm.Paired)

plt.xlabel('x')
plt.ylabel('y')
plt.title('random data')
plt.show()

在这里插入图片描述

#重置数据集索引,应为合并后数据索引重复
res.reset_index(inplace=True, drop=True)
#取索引是4的整数倍的的数据做为测试集
test=res[(res.index%4==0)]
#取索引不是4的整数倍的的数据做为训练集
train=res[(res.index%4!=0)]
from sklearn import svm
#新建SVC分类器,核函数是线性核,C将决定间隔的大小C越大间隔越小

#训练数据
X=train[['x','y']]
#选择训练集的标签
y = train['z']
#svm分类器,线性核
clf = svm.SVC(kernel='linear', C=1)
#训练
clf.fit(X, y)
SVC(C=1, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)
plt.scatter(X['x'], X['y'], c=y,cmap=plt.cm.Paired)
# plot the decision function
ax = plt.gca()
#获得坐标系边界
xlim = ax.get_xlim()
ylim = ax.get_ylim()


# 0-1生成300个点
xx = np.linspace(xlim[0], xlim[1], 300)
yy = np.linspace(ylim[0], ylim[1], 300)
#生成网格坐标
YY, XX = np.meshgrid(yy, xx)
#将网格坐标组成样本
xy = np.vstack([XX.ravel(), YY.ravel()]).T
#求xy到分界线的函数距离
Z = clf.predict(xy).reshape(XX.shape)
# 绘制等高线线
ax.contour(XX, YY, Z, colors='k')
# 绘制出支持向量
ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=100,linewidth=1, facecolors='none', edgecolors='k')

plt.show()

在这里插入图片描述

#预测点
clf.predict([[2,0],[2.5,0],[3,0]])
array([1, 0, 0], dtype=int64)
#训练集得分
clf.score(X,y)
0.9866666666666667
#测试集
clf.score(test[['x','y']],test[['z']])
0.96
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值