详解如何用Python实现感知器算法


Posted in Python onJune 18, 2021
目录
  • 一、题目
  • 二、数学求解过程
  • 三、感知器算法原理及步骤
  • 四、python代码实现及结果

 

一、题目

详解如何用Python实现感知器算法

 

二、数学求解过程

详解如何用Python实现感知器算法
详解如何用Python实现感知器算法
详解如何用Python实现感知器算法

该轮迭代分类结果全部正确,判别函数为g(x)=-2x1+1

 

三、感知器算法原理及步骤

详解如何用Python实现感知器算法

 

四、python代码实现及结果

(1)由数学求解过程可知:

详解如何用Python实现感知器算法

(2)程序运行结果

详解如何用Python实现感知器算法

(3)绘图结果

详解如何用Python实现感知器算法

'''
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)
    #print

    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)

到此这篇关于详解如何用Python实现感知器算法的文章就介绍到这了,更多相关Python实现感知器算法内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python学习之Django的管理界面代码示例
Feb 10 Python
python计算两个地址之间的距离方法
Jun 09 Python
Python设计模式之策略模式实例详解
Jan 21 Python
对Python 多线程统计所有csv文件的行数方法详解
Feb 12 Python
Python学习笔记之抓取某只基金历史净值数据实战案例
Jun 03 Python
python如何从文件读取数据及解析
Sep 19 Python
Python 日期时间datetime 加一天,减一天,加减一小时一分钟,加减一年
Apr 16 Python
python 读取二进制 显示图片案例
Apr 24 Python
浅谈Python程序的错误:变量未定义
Jun 02 Python
python thrift 实现 单端口多服务的过程
Jun 08 Python
为什么说python适合写爬虫
Jun 11 Python
python实战之90行代码写个猜数字游戏
Apr 22 Python
python中24小时制转换为12小时制的方法
Jun 18 #Python
用Python selenium实现淘宝抢单机器人
python中pandas对多列进行分组统计的实现
python 常用的异步框架汇总整理
Jun 18 #Python
Opencv中cv2.floodFill算法的使用
Python下opencv使用hough变换检测直线与圆
python 网络编程要点总结
Jun 18 #Python
You might like
定制404错误页面,并发信给管理员的程序
2006/10/09 PHP
PHP读写文件的方法(生成HTML)
2006/11/27 PHP
php 面向对象的一个例子
2011/04/12 PHP
php求两个目录的相对路径示例(php获取相对路径)
2014/03/27 PHP
用Javascript做flash做的事..才完成的一个类.Auntion Action var 0.1
2007/02/23 Javascript
不同浏览器对回车提交表单的处理办法
2010/02/13 Javascript
javascript 词法作用域和闭包分析说明
2010/08/12 Javascript
利用jQuery接受和处理xml数据的代码(.net)
2011/03/28 Javascript
JSON辅助格式化处理方法
2013/03/26 Javascript
jquery对dom的操作常用方法整理
2013/06/25 Javascript
JS简单的图片放大缩小的两种方法
2013/11/11 Javascript
用js提交表单解决一个页面有多个提交按钮的问题
2014/09/01 Javascript
基于Node.js实现nodemailer邮件发送
2016/01/26 Javascript
js+flash实现的5图变换效果广告代码(附演示与demo源码下载)
2016/04/01 Javascript
JS中数据结构之栈
2019/01/01 Javascript
WebSocket的简单介绍及应用
2019/05/23 Javascript
使用imba.io框架得到比 vue 快50倍的性能基准
2019/06/17 Javascript
Django框架下在URLconf中指定视图缓存的方法
2015/07/23 Python
Python常用的内置序列结构(列表、元组、字典)学习笔记
2016/07/08 Python
Python学习小技巧之列表项的推导式与过滤操作
2017/05/20 Python
Python网络编程之TCP与UDP协议套接字用法示例
2018/02/02 Python
python分批定量读取文件内容,输出到不同文件中的方法
2018/12/08 Python
宝拉珍选澳大利亚官方购物网站:Paula’s Choice澳大利亚
2016/09/13 全球购物
希尔顿酒店中国网站:Hilton中国
2017/03/11 全球购物
运动鞋中的劳斯莱斯:索康尼(SAUCONY)
2017/08/09 全球购物
莫斯科高科技在线商店:KremlinStore
2019/03/13 全球购物
李维斯法国官网:Levi’s法国
2019/07/13 全球购物
接口的多继承会带来哪些问题
2015/08/17 面试题
软件生产职位结构化面试主要考察要素及面试题库
2015/06/12 面试题
师范学院毕业生求职信
2014/06/24 职场文书
新兵入伍心得体会
2014/09/04 职场文书
毕业生班级鉴定评语
2015/01/04 职场文书
聘用合同范本
2015/09/21 职场文书
《火烧云》教学反思
2016/02/23 职场文书
只用40行Python代码就能写出pdf转word小工具
2021/05/31 Python
Nginx禁止ip访问或非法域名访问
2022/04/07 Servers