当前位置: 首页 > news >正文

python学智能算法(二十)|SVM基础概念-感知机算法及代码

引言

前序学习进程中,已经学习了超平面的基础知识,学习链接为:超平面
在此基础上,要想正确绘制超平面,还需要了解感知机的相关概念。

感知机

感知机是对生物神经网络的模拟,当输入信号达到感知机的阈值时,会输出感应信号,否则不会有任何反应。
这里先对感知机建立数学模型。
对于向量F和向量G,有:
Fˉ=(f0,f1,f2,...,fn),Gˉ=(g0,g1,g2,...,gn)\bar {F}=(f0,f1,f2,...,fn),\bar{G}=(g0,g1,g2,...,gn) Fˉ=(f0,f1,f2,...,fn),Gˉ=(g0,g1,g2,...,gn)定义向量点积:H=Fˉ⋅Gˉ\ H=\bar {F} \cdot \bar {G} H=FˉGˉ

如果满足:
H={1if H≥0−1if H<0H=\left\{ \begin{matrix} 1 \quad \text{\quad if } H\geq 0 \\ -1 \quad \text{if } H< 0 \end{matrix} \right.H={1if H01if H<0
相应的,在numpy模块中,也有一个sign()函数可以对感知机的数学模型进行精炼表达,这有一个代码实例:

# 引入numpy模块
import numpy as np
# 引入数据
x=[1,2,3,-6,-8,-10]
# np.sign()函数测试
z=np.sign(x)
print('z=',z)

代码运行后的输出效果为:
np。sign()函数输出效果
非常明显,对于非负数,np.sign()函数输出1,对于负数则直接输出0。

一个关于感知机的项目

这里有一个感知机算法的简单项目:

# 引入模块
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from matplotlib.colors import ListedColormap# 定义随机数种子
# 定义了随机数种子后,每次产生的随机数是一样的
# 使用不同的随机数种子,会得到不同的初始随机数
np.random.seed(88)def perceptron_learning_algorithm(x, y):# W是3个在[0,1)区间上均匀分布的随机数w = np.random.rand(3)# misclassified_examples是由predict()函数计算获得misclassified_examples = predict(hypothesis, x, y, w)while misclassified_examples.any():x_example, expect_y = pick_one_from(misclassified_examples, x, y)# 更新权重w = w + x_example * expect_y# 重新代入predict()函数进行评估misclassified_examples = predict(hypothesis, x, y, w)return w# hypothesis函数对w和x两个向量的点积进行感知机运算
# hypothesis函数返回值是1或者-1
def hypothesis(x, w):return np.sign(np.dot(w, x))# predict函数包括4个变量
def predict(hypothesis_function, x, y, w):# np.apply_along_axis函数先将x按照行取出,这是由axis=1决定的取值方式# hypothesis_function每次接收x的一行和wpredictions = np.apply_along_axis(hypothesis_function, 1, x, w)# y != predictions表示分类错误,这个!代表的不等于判断会输出布尔结果False和True# x[y != predictions]直接输出x[y != predictions]取值为True时对应x行的数据misclassified = x[y != predictions]return misclassified# pick_one_from函数
def pick_one_from(misclassified_examples, x, y):# 随机打乱分类样本的顺序np.random.shuffle(misclassified_examples)t = misclassified_examples[0]index = np.where(np.all(x == t, axis=1))[0][0]return x[index], y[index]# 使用sklearn生成线性可分的数据集
x, y = make_classification(n_samples=100,n_features=2,n_redundant=0,n_informative=2,n_clusters_per_class=1,random_state=42,class_sep=2.0
)# 将标签从0/1转换为-1/1
y = np.where(y == 0, -1, 1)# 添加偏置项
x_augmented = np.c_[np.ones(x.shape[0]), x]# 训练感知机模型
w = perceptron_learning_algorithm(x_augmented, y)
print("感知机权重:", w)# 绘制分类效果图
plt.figure(figsize=(10, 8))# 绘制数据点
plt.scatter(x[y==1][:, 0], x[y==1][:, 1], c='blue', marker='o', label='Class +1', edgecolor='k', s=70)
plt.scatter(x[y==-1][:, 0], x[y==-1][:, 1], c='red', marker='o', label='Class -1', s=70)# 绘制决策边界
x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1
y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),np.arange(y_min, y_max, 0.02))# 预测网格点的分类
Z = np.array([hypothesis(np.array([1, a, b]), w) for a, b in zip(xx.ravel(), yy.ravel())])
Z = Z.reshape(xx.shape)# 绘制分类区域
cmap_light = ListedColormap(['#FFAAAA', '#AAAAFF'])
plt.contourf(xx, yy, Z, cmap=cmap_light, alpha=0.3)# 绘制决策边界
plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors='black')# 添加图例和标题
plt.title('Perceptron Classification Results', fontsize=16)
plt.xlabel('Feature 1', fontsize=14)
plt.ylabel('Feature 2', fontsize=14)
plt.legend(loc='upper right', fontsize=12)
plt.grid(True, linestyle='--', alpha=0.7)
plt.tight_layout()plt.show()

代码运行后,会获得直接的分类效果:
感知机
下一件文章将详细解读代码。

总结

学习了感知机的基本概念。


文章转载自:
http://borderer.kjawz.cn
http://centrosymmetric.kjawz.cn
http://antirheumatic.kjawz.cn
http://barycenter.kjawz.cn
http://autotelegraph.kjawz.cn
http://bandsaw.kjawz.cn
http://adidas.kjawz.cn
http://australia.kjawz.cn
http://aponeurosis.kjawz.cn
http://checkrow.kjawz.cn
http://cafe.kjawz.cn
http://chlorometer.kjawz.cn
http://baronetcy.kjawz.cn
http://businesslike.kjawz.cn
http://brawniness.kjawz.cn
http://childmind.kjawz.cn
http://bulla.kjawz.cn
http://calced.kjawz.cn
http://bichlorid.kjawz.cn
http://changeable.kjawz.cn
http://aloud.kjawz.cn
http://carminite.kjawz.cn
http://chandler.kjawz.cn
http://amenity.kjawz.cn
http://charlotte.kjawz.cn
http://chastely.kjawz.cn
http://astonished.kjawz.cn
http://additament.kjawz.cn
http://batdambang.kjawz.cn
http://antinatalist.kjawz.cn
http://www.dtcms.com/a/281249.html

相关文章:

  • SAP把运费加入到物料成本估算
  • 使用 Aerich 进行 FastAPI 数据库迁移指南
  • redis红锁
  • GitHub 上 Star 数量前 8 的开源 Web 应用项目
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘pandas’问题
  • centos8集群部署etcd
  • 【12】MFC入门到精通——MFC 消息对话框 MessageBox()和AfxMessageBox() 解析 示例 及 应用实例
  • 【目标追踪】MUTR3D: A Multi-camera Tracking Framework via 3D-to-2D Queries
  • MongoDB数据问题说明
  • css-css执行的三种方式和css选择器
  • AS32X601 系列 MCU 硬件最小系统设计与调试方案探析
  • Agentic AI 的威胁与缓解措施
  • 如何快速有效地在WordPress中添加Instagram动态
  • 【PTA数据结构 | C语言版】前序遍历二叉树
  • 零基础入门物联网-远程门禁开关:代码调试
  • 过滤数组中null、undefined、‘‘、等非真内容
  • AAAI-2025 | 同济大学面向嘈杂环境的音频视觉导航!BeDAViN:大规模音频-视觉数据集与多声源架构研究
  • OpenCSG QA:您的国产大模型与 Agent 管理平台
  • 变更缓冲池简介
  • 19.1 单元测试框架
  • ssm学习笔记day08mybatis
  • ESP32轻松实现UDP无线通信
  • 使用python的pillow模块将图片转化为灰度图,获取值和修改值
  • 雷军的 IP 革命:人格化力量如何重塑商业规则|创客匠人
  • uniapp微信小程序弹窗
  • 《汇编语言:基于X86处理器》第8章 高级过程(1)
  • 被人工智能激活的哲学
  • 短剧小程序的「技术革命」:从「粗放生长」到「精准运营」
  • Windows内核对象
  • 新方法!家长可用安卓或苹果,远程管理孩子使用iPhone的时长