绘制超平面线性SVM python

2024-01-10

我正在尝试绘制使用 LinearSVC 和 sklearn 训练的模型的超平面。请注意,我正在使用自然语言;在拟合模型之前,我使用 CountVectorizer 和 TfidfTransformer 提取了特征。

这里是分类器:

from sklearn.svm import LinearSVC
from sklearn import svm

clf = LinearSVC(C=0.2).fit(X_train_tf, y_train)

然后我尝试按照建议绘制在 Scikit-learn 网站上 http://scikit-learn.org/0.18/auto_examples/svm/plot_separating_hyperplane.html:

# get the separating hyperplane
w = clf.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (clf.intercept_[0]) / w[1]

# plot the parallels to the separating hyperplane that pass through the
# support vectors
b = clf.support_vectors_[0]
yy_down = a * xx + (b[1] - a * b[0])
b = clf.support_vectors_[-1]
yy_up = a * xx + (b[1] - a * b[0])

# plot the line, the points, and the nearest vectors to the plane
plt.plot(xx, yy, 'k-')
plt.plot(xx, yy_down, 'k--')
plt.plot(xx, yy_up, 'k--')

plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
            s=80, facecolors='none')
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)

plt.axis('tight')
plt.show()

本示例使用 svm.SVC(kernel='linear'),而我的分类器是 LinearSVC。因此,我收到此错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-39-6e231c530d87> in <module>()
      7 # plot the parallels to the separating hyperplane that pass through the
      8 # support vectors
----> 9 b = clf.support_vectors_[0]
     1 yy_down = a * xx + (b[1] - a * b[0])
     11 b = clf.support_vectors_[-1]

AttributeError: 'LinearSVC' object has no attribute 'support_vectors_'

如何成功绘制 LinearSVC 分类器的超计划?


离开又怎样support_out,这不是为 a 定义的LinearSVC?

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm

np.random.seed(0)
X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
Y = [0] * 20 + [1] * 20

fig, ax = plt.subplots()
clf2 = svm.LinearSVC(C=1).fit(X, Y)

# get the separating hyperplane
w = clf2.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (clf2.intercept_[0]) / w[1]

# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx2, yy2 = np.meshgrid(np.arange(x_min, x_max, .2),
                     np.arange(y_min, y_max, .2))
Z = clf2.predict(np.c_[xx2.ravel(), yy2.ravel()])

Z = Z.reshape(xx2.shape)
ax.contourf(xx2, yy2, Z, cmap=plt.cm.coolwarm, alpha=0.3)
ax.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.coolwarm, s=25)
ax.plot(xx,yy)

ax.axis([x_min, x_max,y_min, y_max])
plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

绘制超平面线性SVM python 的相关文章

随机推荐

  • C 中的简单 lua_yield 无法从 Lua 正确恢复

    我刚刚开始深入研究 lua 协程与 C 我对我认为应该是我能想到的最简单的例子有疑问 The C include
  • Table2excel 插件不起作用

    我正在开发一个仪表板应用程序 我想实现 下载表为 xls 功能 在此链接上您可以看到表格的样子仪表板 http pasteboard co p82eqze png 我找到了一个library https github com rainabb
  • 按 lubridate 日期 %within% 间隔连接数据框

    我一直在练习和学习使用包含以下内容的列来处理 R 数据框lubridate数据类型 例如我的示例问题其他问题 https stackoverflow com questions 51407177 r lubridate split dura
  • Hadoop start-all.sh错误:没有这样的文件或目录

    成功创建名称节点后 我在尝试启动名称节点时遇到了这个问题 对我来说 它似乎试图记录到一个不存在的文件 如何更改设置以将脚本日志定向到正确的目录 bash 3 2 start all sh starting namenode logging
  • 如何在 Docker 第 3 部分教程中使用curl -4 http://localhost?

    使用 Docker 教程我被困在这部分 https docs docker com get started part3 run your new load balanced app https docs docker com get sta
  • 读取 Hadoop ArrayWritable 中包装的值

    我是 Hadoop 和 Java 的新手 我的映射器输出文本和 Arraywritable 我在读取 ArrayWritable 值时遇到问题 Unbale 将 get 值转换为整数 附上映射器和减速器代码 有人可以帮我纠正我的减速器代码以
  • 计算 PHP 数组中的日期

    我有这个数组 Array 0 gt Array x gt 2016 04 19 1 gt Array x gt 2016 05 25 2 gt Array x gt 2016 05 26 3 gt Array x gt 2016 05 27
  • std::tuple 用于不可复制和不可移动的对象

    我有一门删除了复制和移动向量的课程 struct A A int a data a A std cout lt lt A lt lt this lt lt lt lt data lt lt std endl A A const obj de
  • Maven SCR 插件 - 不生成 OSGI-INF 文件夹

    我的 SCR 插件无法正常工作 我已经尽可能多地进行了搜索 但只找到了与我需要使用的结构不相似的示例 下面是 POM 的片段 这些几乎是 CQ 项目原型生成的默认值 所有依赖项都在那里 所以可能不是这样 这是构建的输出 SLF4J Fail
  • CodeIgniter 2.x 会话和 Internet Explorer

    我在网上阅读了大量有关 CodeIgniter 及其会话和 Internet Explorer 问题的文章 其中很多内容似乎都以会话名称为中心 名称中没有下划线 这些文章似乎都是针对 CI 1 x 的 CI还存在这个问题吗 我尝试删除下划线
  • 英特尔伽利略裸机 UART

    我想编写一些 hello world 程序裸机申请于英特尔伽利略木板 当然 使用 UEFI 打印文本 到 UART 1 效果很好 但我想 手动 访问 UART 而不需要 UEFI 的任何帮助 在 QEMU 中我的代码运行良好 h file
  • 如何将 Spark 数据帧转换为 Polars 数据帧?

    我想知道如何将 Spark 数据帧转换为 Polars 数据帧 假设我在 PySpark 上有这段代码 df spark sql select from tmp 我可以使用以下命令轻松地将其转换为 pandas 数据框 toPandas 极
  • 使用ionic 4,尝试在应用程序使用硬件后退按钮按下事件关闭之前向用户发出退出警报消息

    在我的最初阶段 我试图给出 退出应用程序 是 否 当用户从登录页面或主页 登录后 按下硬件后退按钮时发出警报 我面临的问题是 当我按下后退按钮时 退出警报消息会出现在每个页面上 而不仅仅是在登录或主页上 此外 无论我是否按警报框中的 否 选
  • 如何删除除包含 TRUNCATE - INSERT 的行之外的所有行

    我创建了一个脚本 将所有 PL SQL 文件格式化为一个具有值对的简单文件 它很难解释 我认为如果您只查看该文件会更容易 PROCEDURE VALIDA CAMBIO GPR TRUNCATE TMP MOD PVA INSERT TMP
  • Cocos2d 2.0 - 左下角有 3 个数字

    我的 Cocos2D 2 0 项目屏幕左下角有 3 个数字 82 0 016 60 0 60 可能是 FPS 那么其他两个呢 我记得以前的 Cocos 版本只有 FPS 数字 有什么线索吗 谢谢 82 lt number of draw c
  • 将平面 Python 字典转换为字典列表

    我有一本以下格式的字典 我不知道我将收到的行数或项目数 line 0 item1 a line 0 item2 34 line 1 item1 sd line 1 item2 2 line 1 item3 fg line 2 item1 f
  • 带 twitter bootstrap 的全宽布局

    我正在尝试完成类似于此的布局 http dribbble com shots 829195 Slate attachments 86422 http dribbble com shots 829195 Slate attachments 8
  • BI 与 Django?

    有没有办法使用 Django 开发 Bi 商业智能 解决方案 因此 应该可以使用多个数据源来定义模型 有人用过 Django 体验过 BI 吗 怎么可能行得通呢 我不确定您对 BI 的定义是什么 也不知道为什么您认为 BI 解决方案需要多个
  • 来自具有可变帧速率的图像的视频

    我想从静止图像创建视频 但我没有使用静态 FPS 而是为每个图像都有一个特定的时间戳 图像在时间上的间隔并不完全均匀 我该怎么做呢 我当前的代码 具有静态 FPS 如下 import cv2 import os image folder U
  • 绘制超平面线性SVM python

    我正在尝试绘制使用 LinearSVC 和 sklearn 训练的模型的超平面 请注意 我正在使用自然语言 在拟合模型之前 我使用 CountVectorizer 和 TfidfTransformer 提取了特征 这里是分类器 from s