SVM参数及人脸分类

SVM1、概念及参数调整2、人脸识别1、概念及参数调整1、构造数据集import numpy as npimport matplotlib.pyplot as pltfrom scipy import statsimport seaborn as snssns.set()from sklearn.datasets.samples_generator import make_blo...
摘要由CSDN通过智能技术生成

1、概念及参数调整

1、构造数据集

import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import seaborn as sns
sns.set()

from sklearn.datasets.samples_generator import make_blobs
X, y = make_blobs(n_samples=50, centers=2, random_state=0, cluster_std=0.60)
# print(X, y)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
plt.show()

输出结果:
在这里插入图片描述
可以通过改变cluster_std从而改变生成数据的离散程度
2、随便画出三条线

xfit = np.linspace(-1, 3.5)  # 默认num=50,生成50个点
# print(len(xfit))
plt.plot([0.6], [2.1], 'x', color='red', markeredgewidth=2, markersize=10)  # 设置一个异常点
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)
plt.show()

输出结果:
在这里插入图片描述
3、最小化雷区

# 最小化雷区
xfit = np.linspace(-1, 3.5)  # 生成50个点
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
# yfit + d 表示雷区的宽度
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)
plt.show()

输出:
在这里插入图片描述
4、训练模型并且图形化显示

# 训练模型
from sklearn.svm import SVC
model = SVC(kernel='linear')
model.fit(X, y)

# 绘图函数


def plot_svc_decision_function(model, ax=None, plot_support=True):
    """Plot the decision function for a 2D SVC"""
    if ax is None:
        ax = plt.gca()
    xlim = ax.get_xlim()
    ylim = ax.get_ylim()

    # creat grid to evaluate model
    x = np.linspace(xlim[0], xlim[1], 30)
    y = np.linsp
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值