支持向量机学习与实践

支持向量机调参示例

1.导入支持向量机

from sklearn.svm import SVC

SVC(
C = 1.0,
kernel = 'rbf',  #径向基核函数
degree = 3,
gamma = 'auto_deprecated',
coef0 = 0.0,
shrinking = True,
proobability = False,    #概率,返回这条数据属于哪一个类别的概率
tol = 0.001,
cache_size = 200,
class_weight = None,
verbose = False,
max_iter = -1,
decision_function_shape = 'ovr',
random_state = None
)

2.生成训练测试所用训练集

2.1生成数据集

#导入numpy库与绘图库
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

#生成100个具有两个特征的两分类点
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=100,
                  n_features = 2,
                  centers = 2,
                  random_state = 6
                 )
plt.scatter(X[:, 0], X[:, 1], s=120, c=y, cmap = plt.cm.spring, edgecolors = 'k')

运行结果如下:
在这里插入图片描述

2.2拆分训练集与测试集

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 6)

3模型导入与训练

from sklearn.svm import SVC

#实例化
clf = SVC(kernel = 'linear', C = 1000)

#模型训练
clf.fit(X_train, y_train)

#查看模型准确率
clf.score(X_test, y_test)

#查看支持向量个数(每类分别由多少个)
clf.n_support_

#查看支持向量的坐标
clf.support_vectors_

4结果可视化

plt.scatter(X_train[:, 0], X_train[:, 1], s = 120, 
           c = y_train,   #按照标签着色
            cmap = plt.cm.spring, 
            edgecolors = 'k'
           )
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()

xx = np.linspace(xlim[0], xlim[1], 30)   #在两个点中间等间隔的取30个点
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy,xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T     #垂直的堆
Z = clf.decision_function(xy).reshape(XX.shape)#把每个数据点属于每个类别的分数打出来

#把分类的决定边界画出来
ax.contour(XX, YY, Z, colors = 'k', levels = [-1, 0, 1], alpha = 0.5,
          linestyles=['--', '-', '--'])    #虚线,实线, 虚线
ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s = 100,
          linewidth = 1, facecolors = 'none')


之后陆续调整参数观察图中所显示的情况:

C= 0.1:

clf = SVC(kernel = 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值