前言
本节学习支持向量机SVM
- 决策边界离两边的点都尽可能远
- 模型泛化能力强
1、原理
如图所示
SVM就是希望决策边界离两边的点都尽量远
求最大化的margin=2d
公式推导如下
得到的公式是
正则
2、实现
通过scikit库来实现SVM如下
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
"""使用scikit库实现SVM"""
# 数据
iris = datasets.load_iris()
X = iris.data
y = iris.target
X = X[y<2,:2]
y = y[y<2]
plt.scatter(X[y==0,0], X[y==0,1], color='red')
plt.scatter(X[y==1,0], X[y==1,1], color='blue')
plt.show()
# 数据标准化
standardScaler = StandardScaler()
standardScaler.fit(X)
X_standard = standardScaler.transform(X)
# 线性SVM
svc = LinearSVC(C=1e9) #正则里的C
svc.fit(X_standard, y)
# 决策边界可视化
def plot_decision_boundary(model, axis):
x0, x1 = np.meshgrid(
np.linspace(axis[0], axis[1], int((axis[1] - axis[0]) * 100)).reshape(-1, 1),
np.linspace(axis[2], axis[3], int((axis[3] - axis[2]) * 100)).reshape(-1, 1),
)
X_new = np.c_[x0.ravel(), x1.ravel()]
y_predict = model.predict(X_new)
zz = y_predict.reshape(x0.shape)
from matplotlib.colors import ListedColormap
custom_cmap = ListedColormap(['#EF9A9A', '#FFF59D'<