详解如何用Python实现感知器算法
作者:Julyers 发布时间:2023-11-02 13:32:20
标签:Python,感知器,算法
一、题目
二、数学求解过程
该轮迭代分类结果全部正确,判别函数为g(x)=-2x1+1
三、感知器算法原理及步骤
四、python代码实现及结果
(1)由数学求解过程可知:
(2)程序运行结果
(3)绘图结果
'''
20210610 Julyer 感知器
'''
import numpy as np
import matplotlib.pyplot as plt
def get_zgxl(xn, a):
'''
获取增广向量
:param x: 数组
:param a: 1或-1
:return:
'''
temp = []
if a == 1:
xn.append(1)
if a == -1:
for i in range(len(xn)):
temp.append(xn[i]*(-1))
temp.append(-1)
xn = temp
# print('xn:'+ str(np.array(x).reshape(-1, 1)))
return np.array(xn).reshape(-1, 1)
def calculate_w(w, xn):
'''
已知xn和初始值,计算w
:param w: 列向量 --> wT:行向量
:param xn: 列向量
:return:
'''
# wT = w.reshape(1, -1) # 列向量转变为行向量,改变w
wT = w.T # 列向量转变为行向量,不改变w
wTx = np.dot(wT, xn).reshape(-1) # 行向量乘以列向量, 维度降为1。
#wTx = wT@xn # 行向量乘以列向量
if wTx > 0:
w_value = w
else:
w_value = np.add(w, xn)
# print("w_update的shape" + str(w_update.shape))
#print("wTx:" + str(wTx))
return w_value, wTx # w_value为列向量, wTx为一个数
def fit_one(w1, x1, x2, x3, x4):
'''
完成一轮迭代,遍历一次数据,更新到w5。
:param w1: 初始值
:param x1:
:param x2:
:param x3:
:param x4:
:return: 返回w5和wTx的列表。
'''
wTx_list = []
update_w = w1
for i in range(0, len(x_data)): #len计算样本个数,通过循环更新w
update_w, wTx = calculate_w(update_w, x_data[i])
wTx_list.append(wTx)
#print(wTx_list)
return update_w, wTx_list
def draw_plot(class1, class2, update_w):
plt.figure()
x_coordinate = []
y_coordinate = []
for i in range(len(class1)):
x_coordinate.append(class1[i][0])
y_coordinate.append(class1[i][1])
plt.scatter(x_coordinate, y_coordinate, color='orange', label='class1')
x_coordinate = []
y_coordinate = []
for i in range(len(class2)):
x_coordinate.append(class2[i][0])
y_coordinate.append(class2[i][1])
plt.scatter(x_coordinate, y_coordinate, color='green', label='class2')
w_reshape = update_w.reshape(-1)
x = np.linspace(0, 2, 5)
if w_reshape[1] == 0:
plt.axvline(x = (-1) * w_reshape[2]/w_reshape[0])
else:
plt.plot(x, (x*w_reshape[0]*(-1) + w_reshape[2]*(-1))/w_reshape[1])
plt.title('result of perception')
plt.xlabel('x1')
plt.ylabel('x2')
plt.legend()
plt.show()
if __name__ == '__main__':
x1 = [0, 0]
x2 = [0, 1]
x3 = [1, 0]
x4 = [1, 1]
class1 = [x1, x2]
class2 = [x3, x4]
x1 = get_zgxl(x1, 1)
x2 = get_zgxl(x2, 1)
x3 = get_zgxl(x3, -1)
x4 = get_zgxl(x4, -1)
x_data = [x1, x2, x3, x4]
# print(x_data)
w1 = np.zeros((3, 1)) # 初始值w1为列向量
#print('w1:' + str(w1) + '\n')
update_w = w1
update_w, wTx_list = fit_one(update_w, x1, x2, x3, x4)
count = 0
iter_number = 0
for wTx in wTx_list:
if wTx > 0:
count += 1
if count < 4:
update_w, wTx_list = fit_one(update_w, x1, x2, x3, x4)
iter_number += 1
else:
break
print('迭代次数为:' + str(iter_number))
print('迭代终止时的w:'+'\n' + str(update_w))
#print(wTx_list)
draw_plot(class1, class2, update_w)
来源:https://blog.csdn.net/weixin_41631106/article/details/117899297
0
投稿
猜你喜欢
- 不知道算不算DW4的大BUG. DW4实际的运行如下: 读注册表中HKEY_CURRENT_USER/
- 前言 简述bs4:使用pip install beautifulsoup4将bs4包安装到当前的python解释器环境,使用fro
- 最近用php进行文件目录信息读取,在网上弄到下面一段代码://获取指定目录下的文件列表//$path 指定的目录,默认为当前目录//$ifc
- 一般跟踪训练的ground_truth的数据保存在文本文文件中,故每一行的数据为一张图片的标签数据,这个时候读取每一张图片的标签,具体实现如
- Python中可以使用 pickle 模块将对象转化为文件保存在磁盘上,在需要的时候再读取并还原。具体用法如下:pickle是Python库
- Win7或Windows server 2008中IIS7支持ASP+Access解决方法:1. 让IIS7支持ASPWin7或Window
- 或者说有一条命令 hostname [string],当string是不包含2950时,是true,包含2950时是false。使用Exce
- 通过《SQL Server 数据库备份和还原认识和总结(一)》,相信您对数据备份和还原有了一个更深入的认识,在上文中我没有对事务日志做剖析,
- 游戏开始前的注意事项1:游戏《外星人入侵》将包含很多文件,请在你的D盘中新建一个空文件夹,并将其命名为alien_invasion.请务必将
- 2017年底,Tensorflow 推出Lite版本,可实现移动端的快速运行,其中,一个很关键的问题,如何把现有分类模型(.pb) 转换为(
- 看代码吧~# 加载库import pandas as pd# 데이터프레임을 만듭니다.dataframe = pd.DataFrame()
- 本文主要研究的是python读取jpg格式图片并显示为16进制的相关内容,具体如下。代码:>>> aaa = open(&
- 检测自己当前系统环境中python是否已经安装该module,若未安装请自行安装检测自己的pycharm使用的环境变量是否与当前环境一致若不
- 在一些项目中, 我们会使用json 来将字符串转为结构体,但是很多时候,这种结构体只会用一次,基本上只会用于反序列化, 对于这种只用到一次的
- 传入参数一个,为元素的id值或元素本身,返回为元素的真实背景色值(字符串)。 值得一提的是IE里面返回的是16进制的值,而Mozi
- 今天学习到python的读取文件部分。还是以一段代码为例:filename='programming.txt'with op
- 随着互联网的快速发展和数据交换的广泛应用,各种数据格式的处理成为软件开发中的关键问题。JSON 作为一种通用的数据交换格式,在各种应用场景中
- ACCESS数据库在用的过程中,经常不断的进行删除和增加记录的操作,会出现以下问题:1、可能会使Update语句更新失败,明明一条记录存在,
- 在过去的几十年里,机器学习对世界产生了巨大的影响,而且它的普及程度似乎在不断增长。最近,越来越多的人已经熟悉了机器学习的子领域,如神经网络,
- hanxiaolian 为了躲避 lake2 ASP站长管理助手而写.. 一.绕过lake2 Asp木马扫描的小马 代码如下:<%&n