支持向量机实践
支持向量机调参示例
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 =