机器学习--基础算法--支撑向量机 SVM

1 什么是支撑向量机

在这里插入图片描述

2 如何最大化hard margin

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3 soft margin 和SVM的正则化

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4 scikit-learn中的SVM

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets

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 = 'r')
plt.scatter(x[y == 1, 0], x[y == 1, 1], color = 'b')
plt.show()

输出:
在这里插入图片描述

# 数据归一化
from sklearn.preprocessing import StandardScaler
standardscaler = StandardScaler()
standardscaler.fit(x)
x_standard = standardscaler.transform(x)

# 使用scikit-learn中的SVM
from sklearn.svm import LinearSVC
svc = LinearSVC(C = 1e9)
svc.fit(x_standard, y)
>>>LinearSVC(C=1000000000.0, class_weight=None, dual=True, fit_intercept=True,
          intercept_scaling=1, loss='squared_hinge', max_iter=1000,
          multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
          verbose=0)

# 决策边界绘制函数
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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值