SVM支持向量机绘图

原文:
http://ihoge.cn/2018/SVM绘图.html

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
class1 = np.array([[1, 1], [1, 3], [2, 1], [1, 2], [2, 2]])
class2 = np.array([[4, 4], [5, 5], [5, 4], [5, 3], [4, 5], [6, 4]])
plt.figure(figsize=(6, 4), dpi=120)plt.title('Decision Boundary')plt.xlim(0, 8)
plt.ylim(0, 6)
ax = plt.gca()                                  # gca 代表当前坐标轴,即 'get current axis'
ax.spines['right'].set_color('none')            # 隐藏坐标轴
ax.spines['top'].set_color('none')plt.scatter(class1[:, 0], class1[:, 1], marker='o')
plt.scatter(class2[:, 0], class2[:, 1], marker='s')
plt.plot([1, 5], [5, 1], '-r')
plt.arrow(4, 4, -1, -1, shape='full', color='r')
plt.plot([3, 3], [0.5, 6], '--b')
plt.arrow(4, 4, -1, 0, shape='full', color='b', linestyle='--')
plt.annotate(r'margin 1',xy=(3.5, 4), xycoords='data',xytext=(3.1, 4.5), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate(r'margin 2',xy=(3.5, 3.5), xycoords='data',xytext=(4, 3.5), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate(r'support vector',xy=(4, 4), xycoords='data',xytext=(5, 4.5), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate(r'support vector',xy=(2, 2), xycoords='data',xytext=(0.5, 1.5), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

plt.figure(figsize=(6, 4), dpi=120)plt.title('Support Vector Machine')plt.xlim(0, 8)
plt.ylim(0, 6)
ax = plt.gca()                                  # gca 代表当前坐标轴,即 'get current axis'
ax.spines['right'].set_color('none')            # 隐藏坐标轴
ax.spines['top'].set_color('none')plt.scatter(class1[:, 0], class1[:, 1], marker='o')
plt.scatter(class2[:, 0], class2[:, 1], marker='s')
plt.plot([1, 5], [5, 1], '-r')
plt.plot([0, 4], [4, 0], '--b', [2, 6], [6, 2], '--b')
plt.arrow(4, 4, -1, -1, shape='full', color='b')
plt.annotate(r'$w^T x + b = 0$',xy=(5, 1), xycoords='data',xytext=(6, 1), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate(r'$w^T x + b = 1$',xy=(6, 2), xycoords='data',xytext=(7, 2), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate(r'$w^T x + b = -1$',xy=(3.5, 0.5), xycoords='data',xytext=(4.5, 0.2), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate(r'd',xy=(3.5, 3.5), xycoords='data',xytext=(2, 4.5), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate(r'A',xy=(4, 4), xycoords='data',xytext=(5, 4.5), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

from sklearn.datasets import make_blobsplt.figure(figsize=(10, 4), dpi=140)# sub plot 1
plt.subplot(1, 2, 1)X, y = make_blobs(n_samples=100, n_features=2, centers=[(1, 1), (2, 2)], random_state=4, shuffle=False,cluster_std=0.4)plt.title('Non-linear Separatable')plt.xlim(0, 3)
plt.ylim(0, 3)
ax = plt.gca()                                  # gca 代表当前坐标轴,即 'get current axis'
ax.spines['right'].set_color('none')            # 隐藏坐标轴
ax.spines['top'].set_color('none')plt.scatter(X[y==0][:, 0], X[y==0][:, 1], marker='o')
plt.scatter(X[y==1][:, 0], X[y==1][:, 1], marker='s')
plt.plot([0.5, 2.5], [2.5, 0.5], '-r')# sub plot 2
plt.subplot(1, 2, 2)class1 = np.array([[1, 1], [1, 3], [2, 1], [1, 2], [2, 2], [1.5, 1.5], [1.2, 1.7]])
class2 = np.array([[4, 4], [5, 5], [5, 4], [5, 3], [4, 5], [6, 4], [5.5, 3.5], [4.5, 4.5], [2, 1.5]])plt.title('Slack Variable')plt.xlim(0, 7)
plt.ylim(0, 7)
ax = plt.gca()                                  # gca 代表当前坐标轴,即 'get current axis'
ax.spines['right'].set_color('none')            # 隐藏坐标轴
ax.spines['top'].set_color('none')plt.scatter(class1[:, 0], class1[:, 1], marker='o')
plt.scatter(class2[:, 0], class2[:, 1], marker='s')
plt.plot([1, 5], [5, 1], '-r')
plt.plot([0, 4], [4, 0], '--b', [2, 6], [6, 2], '--b')
plt.arrow(2, 1.5, 2.25, 2.25, shape='full', color='b')
plt.annotate(r'violate margin rule.',xy=(2, 1.5), xycoords='data',xytext=(0.2, 0.5), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate(r'normal sample. $\epsilon = 0$',xy=(4, 5), xycoords='data',xytext=(4.5, 5.5), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate(r'$\epsilon > 0$',xy=(3, 2.5), xycoords='data',xytext=(3, 1.5), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

plt.figure(figsize=(6, 4), dpi=120)plt.title('Cost')plt.xlim(0, 4)
plt.ylim(0, 2)
plt.xlabel('$y^{(i)} (w^T x^{(i)} + b)$')
plt.ylabel('Cost')
ax = plt.gca()                                  # gca 代表当前坐标轴,即 'get current axis'
ax.spines['right'].set_color('none')            # 隐藏坐标轴
ax.spines['top'].set_color('none')plt.plot([0, 1], [1.5, 0], '-r')
plt.plot([1, 3], [0.015, 0.015], '-r')
plt.annotate(r'$J_i = R \epsilon_i$ for $y^{(i)} (w^T x^{(i)} + b) \geq 1 - \epsilon_i$',xy=(0.7, 0.5), xycoords='data',xytext=(1, 1), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.annotate(r'$J_i = 0$ for $y^{(i)} (w^T x^{(i)} + b) \geq 1$',xy=(1.5, 0), xycoords='data',xytext=(1.8, 0.2), fontsize=10,arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

plt.figure(figsize=(10, 4), dpi=144)class1 = np.array([[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [3, 2], [4, 1], [5, 1]])
class2 = np.array([[2.2, 4], [1.5, 5], [1.8, 4.6], [2.4, 5], [3.2, 5], [3.7, 4], [4.5, 4.5], [5.4, 3]])# sub plot 1
plt.subplot(1, 2, 1)plt.title('Non-linear Separatable in Low Dimension')plt.xlim(0, 6)
plt.ylim(0, 6)
plt.yticks(())
plt.xlabel('X1')
ax = plt.gca()                                  # gca 代表当前坐标轴,即 'get current axis'
ax.spines['right'].set_color('none')            # 隐藏坐标轴
ax.spines['top'].set_color('none')
ax.spines['left'].set_color('none')plt.scatter(class1[:, 0], np.zeros(class1[:, 0].shape[0]) + 0.05, marker='o')
plt.scatter(class2[:, 0], np.zeros(class2[:, 0].shape[0]) + 0.05, marker='s')# sub plot 2
plt.subplot(1, 2, 2)plt.title('Linear Separatable in High Dimension')plt.xlim(0, 6)
plt.ylim(0, 6)
plt.xlabel('X1')
plt.ylabel('X2')
ax = plt.gca()                                  # gca 代表当前坐标轴,即 'get current axis'
ax.spines['right'].set_color('none')            # 隐藏坐标轴
ax.spines['top'].set_color('none')plt.scatter(class1[:, 0], class1[:, 1], marker='o')
plt.scatter(class2[:, 0], class2[:, 1], marker='s')
plt.plot([1, 5], [3.8, 2], '-r')

def gaussian_kernel(x, mean, sigma):return np.exp(- (x - mean)**2 / (2 * sigma**2))x = np.linspace(0, 6, 500)
mean = 1
sigma1 = 0.1
sigma2 = 0.3plt.figure(figsize=(10, 3), dpi=144)# sub plot 1
plt.subplot(1, 2, 1)
plt.title('Gaussian for $\sigma={0}$'.format(sigma1))plt.xlim(0, 2)
plt.ylim(0, 1.1)
ax = plt.gca()                                  # gca 代表当前坐标轴,即 'get current axis'
ax.spines['right'].set_color('none')            # 隐藏坐标轴
ax.spines['top'].set_color('none')plt.plot(x, gaussian_kernel(x, mean, sigma1), 'r-')# sub plot 2
plt.subplot(1, 2, 2)
plt.title('Gaussian for $\sigma={0}$'.format(sigma2))plt.xlim(0, 2)
plt.ylim(0, 1.1)
ax = plt.gca()                                  # gca 代表当前坐标轴,即 'get current axis'
ax.spines['right'].set_color('none')            # 隐藏坐标轴
ax.spines['top'].set_color('none')plt.plot(x, gaussian_kernel(x, mean, sigma2), 'r-')

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/293067.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Hibernate学习——建立一个简单的Hibernate项目

最近老师让做个web小应用,大三的时候学习过一点J2EE的东西,也做过一些web相关的XXX管理系统,都是用servlet,jsp这些完成的,虽然勉强能够完成任务,但其中各种代码掺杂在一起,不好看而且维护起来也…

python列表生成多个号码_python遍历多个列表生成列表或字典

原博文 2017-03-10 18:30 − key[a,b,c,d] value[1,2,3,4] mydictdict(zip(key,value)) print mydict 输出结果: {a: 1, c: 3, b: 2, d: 4} 也可以用zip同时遍历多个列表,生成一个多维列表 key... 相关推荐 2019-12-18 21:27 − 一.zip函数描述和使用 zi…

Cisco 胖AP和瘦AP的区别

无线接入点(AP,Access Point)也称无线网桥、无线网关,也就是所谓的“瘦”AP。此无线设备的传输机制相当于有线网络中的集线器,在无线局域网中不停地接收和传送数据;任 何一台装有无线网卡的PC均可通过AP来…

linux之head命令

head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾。 1.命令格式: head [参数]... [文件]... 2&#xf…

NCC CAP 6.0 发布 —— 新增支持 OpenTelemetry

前言今天,我们很高兴宣布 CAP 发布 6.0 版本正式版,在这个版本中,我们主要致力于对 OpenTelemetry 提供支持,以及更好的适配 .NET 6。那么,接下来我们具体看一下吧。总览可能有些人还不知道 CAP 是什么,老规…

朴素贝叶斯--文档分类

原文:http://ihoge.cn/2018/MultinomialNB.html 把文档转换成向量 TF-IDF是一种统计方法,用以评估一个词语对于一份文档的重要程度。 TF表示词频, 即:词语在一片文档中出现的次数 词语总数IDF表示一个词的逆向文档频率指数&am…

USENIX 最佳论文奖:擦除 Windows Azure 存储编码

我们发表了一篇介绍Windows Azure 存储如何用编码方式擦除数据的论文,此论文在 2012 年 6 月的 USENIX 技术年会上荣获最佳论文奖。这是 MicrosoftResearch 和 Windows Azure 存储团队共同努力的成果。 您可以在此处找到此论文。 Windows …

Linux I/O 模型(待修改)

2019独角兽企业重金招聘Python工程师标准>>> 最近看到“服务器并发处理能力”章节,被里面的“I/O模型“搞得有点头晕,所以这里希望通过概念的辨析和对比,能更好的理解Linux的 I/O模型。 同步(synchronous)…

linux之ls只显示文件或者文件夹

只显示文件夹 ls -l | grep ^d 只显示文件 ls -l | grep ^- 解释一下: ls -l 之后会得到下面的内容 drwx------ 4 jinwang users 4096 2012-02-09 15:00 .xchat2 -rw-r--r-- 1 jinwang users 1690399 2012-06-04 12:16 45s.txt 文件是以 &q…

git代码提交流程

从master创建任务分支1.需要先将master分支代码更新到最新然后再切新分支;2.新需求和hotfix需要从master切分支,若是在QA测试阶段或者预发布阶段的bug,则需要再该功能分支上进行修改;提交代码到自己的任务分支commit之后一定要pus…

PCA主成分分析+SVM实现人脸识别

原文地址: http://ihoge.cn/2018/PCASVM人脸识别.html 加载数据 这里使用的测试数据共包含40位人员照片,每个人10张照片。也可登陆http://www.cl.cam.ac.uk/research/dtg/attarchive/facesataglance.html 查看400张照片的缩略图。 import time impo…

Lua 学习笔记(一)

Lua学习笔记 1、lua的优势 a、可扩张性 b、简单 c、高效率 d、和平台无关 2、注释 a、单行注释 -- b、多行注释 --[[ --]] 3、类型和值 8个基本类型,检测变量类型用type a、nil print(type(nil)) -->nil 全局变量没有…

python inspect模块解析

来源:https://my.oschina.net/taisha/blog/55597 inspect模块主要提供了四种用处: (1) 对是否是模块,框架,函数等进行类型检查。 (2) 获取源码 (3) 获取类或函数的参数的信息 (4) 解析堆栈 使用inspect模块可以提供自省功能&#…

龙芯发布.NET 6.0.100开发者内测版

龙芯在龙芯开源社区发布了LoongArch64-.NET-SDK-6.0.100开发者内测版的新闻 ,龙芯.NET基于上游社区 版本 适配支持龙芯平台架构。目前支持LoongArch64架构和MIPS64架构,LoongArch64架构的.NET-SDK-3.1已完成,安装包下载地址LoongArch64-.NET …

Redis系统性介绍

虽然Redis已经很火了,相信还是有很多同学对Redis只是有所听闻或者了解并不全面,下面是一个比较系统的Redis介绍,对Redis的特性及各种数据类型及操作进行了介绍。是一个很不错的Redis入门教程。 1.介绍 1.1 Redis是什么 REmote DIctionary Ser…

mysql 不支持 select into

替代方案 insert into newTableName(column1,column2) select * from oldTableName INSERT INTO aw_daily_call_task(tenantId,sub_product_name, call_type,call_date,expire_date,username,customer_type,start_date,end_date,budget,product_type) SELECT t1.tenant_id …

linux之wc命令

Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数、字数、行数,并将统计结果显示输出。 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数、字数、行数,并将统计结果显示输…

数据挖掘的9大成熟技术和应用

http://ihoge.cn/2018/DataMining.html 数据挖掘的9大成熟技术和应用 基于数据挖掘的9大主要成熟技术以及在数据化运营中的主要应用: 1、决策树 2、神经网络 3、回归 4、关联规则 5、聚类 6、贝叶斯分类 7、支持向量机 8、主成分分析 9、假设检验 1 决…

LVS:三种负载均衡方式与八种均衡算法

1、什么是LVS? 首先简单介绍一下LVS (Linux Virtual Server)到底是什么东西,其实它是一种集群(Cluster)技术,采用IP负载均衡技术和基于内容请求分发技术。调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行&#xff0…