一、原理:
以一个二分类为例(y = -1,1):希望支持向量间的距离尽可能远。
我们可以看到上面三个方法的效果:
分类 | 效果 |
---|---|
H1 | 无法完成分类 |
H2 | robost性较差(在新的数据集上健壮性较差) |
H3 | 最稳健 |
1.SVM与其他分类器不同:
其他分类器将所有样本视为同样作用,而支持向量机仅重视几个支持向量(很少的样本)。
2.利用凸优化原理:
3.维数超过样本样本数是ok的
SVM支持高维分类
4.训练多个label的原理:
比如有1,2,3个类别,那么可以训练3个SVM模型
二、当两个类看似不一定可分时:
高维数据或者线性不可分数据利用核函数映射到高维,直观理解如图:
三、代码示例
1.简单示例
from sklearn import svm
x = [[0, 0], [1, 1], [3, 3], [4, 4]]
y = [0, 1, 1, 1]
clf = svm.SVC() # 构造模型
clf.fit(x, y) # 训练模型
print(clf.predict([[2., 2.]])) # 打印出预测结果
print(clf.support_vectors_) # 打印出支持向量
2.SVM可视化
# SVM可视化
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets import make_blobs
# we create 40 separable points x为40*2的矩阵
x, y = make_blobs(n_samples=40, centers=2, random_state=6) # random_state=6设置后相当于得到了同样的随机数据
print(np.concatenate((x, y.reshape(-1, 1)), axis=1)) # 按列拼接,np.concatenate是最快的函数
# fit the model, don't regularize for illustration purposes
clf = svm.SVC(kernel="linear", C=1000) # 设置了一个核函数
clf.fit(x, y)
# 画出超平面,支持向量
plt.scatter(x[:, 0], x[:, 1], c=y, s=30, cmap=plt.cm.Spectral)
# c=y设置不同label有不同颜色,s=30设置点的大小
# plot the decision function
ax = plt.gca() # 得到当前坐标轴
xlim = ax.get_xlim() # 得到x轴最小、最大值
print(xlim)
ylim = ax.get_ylim() # 得到y轴最小、最大值
print(ylim)
# create grid to evaluate model
# np.linspace在指定的区间返回均匀间隔的数字
# 帮助计算每个点到超平面的距离 来评估这个模型的好坏
xx = np.linspace(xlim[0], xlim[1], 30) # 从最大值到最小值平均切分30份
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx) # 拓展矩阵 得到两个30*30的矩阵
# np .vstack按照竖直方向拼接,即行增加,列不变
# ravel将多维矩阵变成1维向量
xy = np.vstack([XX.ravel(), YY.ravel()]).T
# 得到的xy是900 * 2
z = clf.decision_function(xy).reshape(XX.shape)
# Z:30 * 30
# decision_function返回一个一维的向量,表示每个样本正负
# 样本的置信度,这里可以理解为距离超平面的距离
# plot decision boundary and margins
# matplotlib.puplot.contour用于绘制等高线
# 这里以与超平面的距离来画等高线,只画三个层次的等高线
ax.contour(
XX, YY, z, colors="k", levels=[-1, 0, 1], alpha=0.5, linestyles=["--", "-", "--"]
)
# plot support vectors
ax.scatter(
clf.support_vectors_[:, 0],
clf.support_vectors_[:, 1],
s=100,
linewidth=1,
facecolors="none",
edgecolors="k",
)
plt.show()
3.具体实例:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
data = pd.read_csv("D:\\university\\211term\\数据挖掘\\PDMBook\\第三章 分类模型\\3.8 SVM\\SVM.csv")
print(data.columns)
x = data[[
'Alcohol', 'Malic acid', 'Ash',
'Alcalinity of ash', 'Magnesium',
'Total phenols', 'Flavanoids',
'Nonflavanoid phenols',
'Proanthocyanins', 'Color intensitys',
'Hue', 'OD280/OD315 of diluted wines',
'Proline'
]]
y = data['label']
x_train, x_test, y_train, y_test = train_test_split(
x, y, test_size=0.1
)
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
svc = SVC()
# 网格搜索,寻找最优参数
# 有很多种核函数
paramGrid = dict(
kernel=['linear', 'poly', 'rbf', 'sigmoid']
)
# 三折交叉验证
gridSearchCV = GridSearchCV(
svc, paramGrid,
cv=3, verbose=1, n_jobs=5,
return_train_score=True
)
grid = gridSearchCV.fit(x_train, y_train)
print('最好的得分是: %f' % grid.best_score_)
print('最好的参数是:')
for key in grid.best_params_.keys():
print('%s=%s' % (key, grid.best_params_[key]))
best_model = grid.best_estimator_
print(best_model.predict(x_test))
s = pd . Series(best_model.predict(x_test),
index=y_test.index)
print(s-y_test)
print(np.sum(s-y_test == 0))
print(best_model.score(x_test, y_test))
4.学习到的编程细节:
1.函数:np.concatenate,
2.网格搜索