sklearn支持向量机(SVM) support vector machines: SVC(linear, rbf)

本文介绍了使用sklearn库的SVM(Support Vector Machines)进行分类,特别是SVC方法。讨论了如何引入核函数解决非线性问题,如径向基函数(RBF)。此外,文章探讨了SVM参数如C和gamma的调节对决策边界的影响,并通过人脸识别实例展示了参数调整的过程。最后,使用交叉验证和PCA降维优化模型性能。
摘要由CSDN通过智能技术生成

在这里插入图片描述

  • 随机数据
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import seaborn as sns
sns.set()
# sns.set(style = 'whitegrid')
# sns.set_style('whitegrid')
% matplotlib inline

from sklearn.datasets.samples_generator import make_blobs

X,y = make_blobs(n_samples=50, centers=2, random_state=0, cluster_std=0.6)
plt.scatter(X[:,0], X[:,1], c=y, s=50, cmap='autumn', edgecolor='black')
plt.plot([0.6], [2.1], 'x', color='r', markeredgewidth=2, markersize=10)

xfit = np.linspace(-1, 3.5)
for m,b in [(1, 0.65), (0.5, 1.6), (-0.2, 2.9)]:
	plt.plot(xfit, m * xfit + b, 'k-')
plt.xlim(-1, 3.5)

哪个分割线好呢?
在这里插入图片描述

  • Suppot Vector Machines: 通道最大化
xfit = np.linspace(-1, 3.5)
plt.scatter(X[:, 0], X[:, 1], c = y, s = 50, cmap = 'autumn', edgecolor='k')

for m, b, d in [(1, 0.65, 0.33), (0.5, 1.6, 0.55), (-0.2, 2.9, 0.2)]:
    yfit = m * xfit + b
    plt.plot(xfit, yfit, 'k-')
    plt.fill_between(xfit, yfit-d, yfit+d, edgecolor='none', color='#AAAAAA', alpha=0.4)
plt.xlim(-1, 3.5)

在这里插入图片描述

SVM

from sklearn.svm import SVC # support vector classifier
model = SVC(kernel = 'linear')
model.fit(X, y)

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape=‘ovr’, degree=3, gamma=‘auto_deprecated’, kernel=‘linear’, max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)

def plot_svc_decision_function(model, ax=None, plot_support=True):
	if ax is None:
		ax = plt.gca()
	xlim = ax.get_xlim()
	ylim = ax.get_ylim()
	
	x = np.linspace(xlim[0], xlim[1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值