34.线性SVM代码实现
约 766 字大约 3 分钟
2025-09-20
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
x,y = make_blobs(
n_samples=40,centers=2,random_state=0
)
plt.scatter(x[:,0],x[:,1],c=y)
plt.show()
from sklearn.inspection import DecisionBoundaryDisplay
def plot_decision_boundary(x, y, clf):
"""
绘制分类模型的决策边界
参数:
x (array-like): 特征数据,形状为 (n_samples, n_features)
y (array-like): 标签数据,形状为 (n_samples,)
clf (estimator): 训练好的sklearn分类模型
"""
# 创建图形
fig, ax = plt.subplots(figsize=(10, 6))
# 绘制决策边界
disp = DecisionBoundaryDisplay.from_estimator(
clf,
x,
response_method="predict",
alpha=0.5,
ax=ax,
grid_resolution=300,
cmap=plt.cm.coolwarm
)
# 绘制训练点
scatter = ax.scatter(
x[:, 0], x[:, 1], c=y, edgecolor="k",
alpha=0.8, cmap=plt.cm.coolwarm, s=50
)
# 添加图例
legend1 = ax.legend(*scatter.legend_elements(), title="Classes")
ax.add_artist(legend1)
# 设置标题和坐标轴标签
ax.set_title(f"Decision Boundary of {type(clf).__name__}")
ax.set_xlabel("Feature 1")
ax.set_ylabel("Feature 2")
plt.tight_layout()
plt.show()from sklearn.svm import LinearSVC
clf = LinearSVC(C=1)
clf.fit(x,y)
plot_decision_boundary(x,y,clf)运行结果
c:\Users\32980\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\svm\_classes.py:32: FutureWarning: The default value of `dual` will change from `True` to `'auto'` in 1.5. Set the value of `dual` explicitly to suppress the warning. warnings.warn(

def plot_svm_margin(x,y,clf,ax=None):
from sklearn.inspection import DecisionBoundaryDisplay
DecisionBoundaryDisplay.from_estimator(clf, x, ax=ax,plot_method='contour',colors='k',alpha=0.5,levels=[-1,0,1])
plt.scatter(x[:,0],x[:,1],c=y)
plot_svm_margin(x,y,clf)
plt.show()
plt.rcParams['figure.figsize'] = (12,8)
params = [0.1,1,10,100]
for i,c in enumerate(params):
clf = LinearSVC(C=c,random_state=0)
clf.fit(x,y)
ax = plt.subplot(2,2,i+1)
plt.title("C = "+str(c))
plot_svm_margin(x,y,clf,ax)
plt.show()运行结果
c:\Users\32980\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\svm\_classes.py:32: FutureWarning: The default value of `dual` will change from `True` to `'auto'` in 1.5. Set the value of `dual` explicitly to suppress the warning. warnings.warn( c:\Users\32980\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\svm\_classes.py:32: FutureWarning: The default value of `dual` will change from `True` to `'auto'` in 1.5. Set the value of `dual` explicitly to suppress the warning. warnings.warn( c:\Users\32980\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\svm\_classes.py:32: FutureWarning: The default value of `dual` will change from `True` to `'auto'` in 1.5. Set the value of `dual` explicitly to suppress the warning. warnings.warn( c:\Users\32980\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\svm\_classes.py:32: FutureWarning: The default value of `dual` will change from `True` to `'auto'` in 1.5. Set the value of `dual` explicitly to suppress the warning. warnings.warn(
我们可以看到,随着C增大,容错空间一直在减小
多分类问题
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data
y = iris.target
X.shape, y.shape运行结果
((150, 4), (150,))
clf = LinearSVC(C=0.1,penalty='l2',multi_class='ovr') # 默认不支持OvO策略
clf.fit(X,y)运行结果
c:\Users\32980\AppData\Local\Programs\Python\Python38\lib\site-packages\sklearn\svm\_classes.py:32: FutureWarning: The default value of `dual` will change from `True` to `'auto'` in 1.5. Set the value of `dual` explicitly to suppress the warning. warnings.warn(
运行结果
LinearSVC(C=0.1)
clf.score(X,y)运行结果
0.96
