【机器学习|学习笔记】详解支持向量机(Support Vector Machine,SVM)核函数之间的区别以及SVM如何处理多分类问题?
【机器学习|学习笔记】详解支持向量机(Support Vector Machine,SVM)核函数之间的区别以及SVM如何处理多分类问题?
欢迎铁子们点赞、关注、收藏!
祝大家逢考必过!逢投必中!上岸上岸上岸!upupup
大多数高校硕博生毕业要求需要参加学术会议,发表EI或者SCI检索的学术论文会议论文。详细信息可关注VX “学术会议小灵通
”或参考学术信息专栏:https://blog.csdn.net/2401_89898861/article/details/148514108
📌 一、SVM核函数的区别详解(含直观解释 + Python代码)
- SVM 通过核函数将数据从原始空间映射到高维空间,核心目的是 增强可分性。
✅ 常见核函数一览
核函数 | 表达式 | 特点 | 应用场景 |
---|
线性核 linear |
K
(
x
,
x
′
)
=
x
⊤
x
′
K(x, x') = x^\top x'
K(x,x′)=x⊤x′ | 快速,适用于线性可分数据 | 文本分类(如TF-IDF) |
多项式核 poly |
K
(
x
,
x
′
)
=
(
x
⊤
x
′
+
c
)
d
K(x, x') = (x^\top x' + c)^d
K(x,x′)=(x⊤x′+c)d | 增加非线性表达能力 | 中小规模数据 |
RBF核 rbf |
K
(
x
,
x
′
)
=
exp
(
−
γ
∣
x
−
x
′
∣
2
)
K(x, x') = \exp(-\gamma |x - x'|^2)
K(x,x′)=exp(−γ∣x−x′∣2) | 非局部非线性映射 | 默认推荐核,普适性强 |
Sigmoid核 sigmoid |
tanh
(
κ
x
⊤
x
′
+
θ
)
\tanh(\kappa x^\top x' + \theta)
tanh(κx⊤x′+θ) | 类似神经网络的激活函数 | 实际中使用少 |
🧪 Python示例:不同核函数对比效果
from sklearn.datasets import make_moons
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
import numpy as np
# 创建一个非线性可分数据集
X, y = make_moons(n_samples=300, noise=0.2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
kernels = ['linear', 'poly', 'rbf', 'sigmoid']
plt.figure(figsize=(12, 10))
for i, kernel in enumerate(kernels):
clf = SVC(kernel=kernel, degree=3, gamma='scale')
clf.fit(X_train, y_train)
acc = accuracy_score(y_test, clf.predict(X_test))
# 绘图
plt.subplot(2, 2, i+1)
plt.title(f"Kernel: {kernel} (Acc: {acc:.2f})")
# 决策边界可视化
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 500),
np.linspace(y_min, y_max, 500))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap='coolwarm', alpha=0.6)
plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap='coolwarm', edgecolors='k')
plt.tight_layout()
plt.show()
✅ 分析:
核函数 | 特性 | 实践建议 |
---|
Linear | 快速收敛,解释性强 | 文本/高维稀疏数据 |
Poly | 增强特征组合能力 | 小数据集或需拟合交互项时 |
RBF | 最通用,能拟合复杂边界 | 推荐默认选择 |
Sigmoid | 不常用,可能不满足 Mercer 定理 | 除非特殊情况不推荐 |
📌 二、SVM 如何处理多分类问题?
✅ 传统 SVM 是二分类模型:只能分两个类别
策略 | 说明 | 分类器数目 |
---|
One-vs-Rest (OvR) | 针对每一类,构建“该类 vs 其他类”的分类器 |
k
k
k |
One-vs-One (OvO) | 针对每两类之间构建一个分类器 |
k
(
k
−
1
)
/
2
k(k-1)/2
k(k−1)/2 |
🔧 Python示例:SVM 多分类 on iris
数据集
from sklearn import datasets
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
# 加载三分类的 Iris 数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 默认使用 One-vs-One
clf = SVC(kernel='rbf', C=1, gamma='scale')
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print("SVM 多分类结果:")
print(classification_report(y_test, y_pred, target_names=iris.target_names))
- ✅ 默认
SVC
使用 OvO 方案,也可改为 OvR:
from sklearn.multiclass import OneVsRestClassifier
clf_ovr = OneVsRestClassifier(SVC(kernel='rbf', C=1))
clf_ovr.fit(X_train, y_train)
✅ 多分类策略对比分析
策略 | 优点 | 缺点 |
---|
OvR | 训练快,适用于类数多时 | 容易类不均衡导致误判 |
OvO | 每次仅处理两类,边界精细 | 训练分类器数量较多 |
推荐策略:
- 类别数量 < 10:使用 One-vs-One 效果更稳定;
- 类别很多时,可使用 One-vs-Rest 提升效率。
✅ 三、小结
问题 | 解答 |
---|
SVM 为什么引入核函数? | 为了解决线性不可分问题,通过核技巧隐式映射到高维 |
各核函数区别? | 线性适合高维稀疏,RBF适合通用复杂边界,多项式适合组合特征 |
SVM 如何做多分类? | 使用 OvR 或 OvO,将多个二分类组合实现多类判别 |