机器学习系列(十) 支持向量机SVM 2020.6.11

本文详细介绍了支持向量机(SVM)的基础知识,包括其追求最大边际的决策边界原理,使用scikit-learn库的实现过程,以及如何通过添加多项式和高斯核函数来增强模型。SVM不仅适用于分类问题,也可应用于回归分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

本节学习支持向量机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'<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值