【机器学习入门】SVM

这篇博客介绍了机器学习中的支持向量机(SVM)概念,包括软间隔的概念和其在处理不纯数据时的作用,以及如何通过调整惩罚参数C来控制容忍度。此外,还探讨了在面对非线性数据时,如何通过高维映射和使用如高斯核(kernel='rbf')等方法实现SVM的非线性分类能力。
摘要由CSDN通过智能技术生成

原理分析:https://zhuanlan.zhihu.com/p/77750026

Demo实践

##  基础函数库
import numpy as np 

## 导入画图库
import matplotlib.pyplot as plt
import seaborn as sns

## 导入逻辑回归模型函数
from sklearn  import svm

## 构造数据集
x_fearures = np.array([[-1, -2], [-2, -1], [-3, -2], [1, 3], [2, 1], [3, 2]])
y_label = np.array([0, 0, 0, 1, 1, 1])

## 调用SVC模型 (支持向量机分类)
svc = svm.SVC(kernel='linear')

## 用SVM模型拟合构造的数据集
svc = svc.fit(x_fearures, y_label) 

## 查看其对应模型的w
print('the weight of Logistic Regression:',svc.coef_)

## 查看其对应模型的w0
print('the intercept(w0) of Logistic Regression:',svc.intercept_)

## 模型预测
y_train_pred = svc.predict(x_fearures)
print('The predction result:',y_train_pred)

# 模型可视化
# 最佳函数
x_range = np.linspace(-3, 3)

w = svc.coef_[0]
a = -w[0] / w[1]
y_3 = a*x_range - (svc.intercept_[0]) / w[1]

# 可视化决策边界
plt.figure()
plt.scatter(x_fearures[:,0],x_fearures[:,1], c=y_label, s=50, cmap='viridis')
plt.plot(x_range, y_3, '-c')
plt.show()

使用sklearn的sample进行模型训练

import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.datasets.samples_generator import make_blobs
%matplotlib inline

# 画图
X, y = make_blobs(n_samples=60, centers=2, random_state=0, cluster_std=0.4)
plt.scatter(X[:, 0], X[:, 1], c=y, s=60, cmap=plt.cm.Paired)
# SVM 函数
clf = SVC(kernel='linear')
clf.fit(X, y)

# 最佳函数
w = clf.coef_[
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值