详解如何用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类的基础入门知识
Nov 24 Python
Python操作sqlite3快速、安全插入数据(防注入)的实例
Apr 26 Python
Python爬虫天气预报实例详解(小白入门)
Jan 24 Python
浅谈pandas中shift和diff函数关系
Apr 08 Python
Python cookbook(数据结构与算法)将多个映射合并为单个映射的方法
Apr 19 Python
python 实现判断ip连通性的方法总结
Apr 22 Python
Python使用selenium实现网页用户名 密码 验证码自动登录功能
May 16 Python
Python实现的json文件读取及中文乱码显示问题解决方法
Aug 06 Python
更新修改后的Python模块方法
Mar 03 Python
Python数据报表之Excel操作模块用法分析
Mar 11 Python
python实现鸢尾花三种聚类算法(K-means,AGNES,DBScan)
Jun 27 Python
python turtle绘制多边形和跳跃和改变速度特效
Mar 16 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
重料打造自己的“宝马”---第三代
2021/03/02 无线电
php验证码生成代码
2015/11/11 PHP
PHP时间戳格式全部汇总 (获取时间、时间戳)
2016/06/13 PHP
JavaScript 权威指南(第四版) 读书笔记
2009/08/11 Javascript
XMLHTTPRequest的属性和方法简介
2010/11/23 Javascript
Three.js源码阅读笔记(物体是如何组织的)
2012/12/27 Javascript
为指定的元素添加遮罩层的示例代码
2014/01/15 Javascript
JavaScript监听和禁用浏览器回车事件实例
2015/01/31 Javascript
JavaScript位移运算符(无符号) &gt;&gt;&gt; 三个大于号 的使用方法详解
2016/03/31 Javascript
浅谈addEventListener和attachEvent的区别
2016/07/14 Javascript
Javascript 跨域知识详细介绍
2016/10/30 Javascript
初识NodeJS服务端开发入门(Express+MySQL)
2017/04/07 NodeJs
基于react框架使用的一些细节要点的思考
2017/05/31 Javascript
原生JS获取元素的位置与尺寸实现方法
2017/10/18 Javascript
用Vue写一个分页器的示例代码
2018/04/22 Javascript
详解Angular中通过$location获取地址栏的参数
2018/08/02 Javascript
element-ui upload组件多文件上传的示例代码
2018/10/17 Javascript
简单的React SSR服务器渲染实现
2018/12/11 Javascript
JavaScript实现动态添加、移除元素或属性的方法分析
2019/01/03 Javascript
利用百度echarts实现图表功能简单入门示例【附源码下载】
2019/06/10 Javascript
node读写Excel操作实例分析
2019/11/06 Javascript
[46:09]2014 DOTA2华西杯精英邀请赛 5 25 LGD VS VG第三场
2014/05/26 DOTA
Python实现简单的四则运算计算器
2016/11/02 Python
解决出现Incorrect integer value: '' for column 'id' at row 1的问题
2017/10/29 Python
Python 25行代码实现的RSA算法详解
2018/04/10 Python
python smtplib发送带附件邮件小程序
2018/05/22 Python
关于python多重赋值的小问题
2019/04/17 Python
Python pip替换为阿里源的方法步骤
2019/07/02 Python
python里的单引号和双引号的有什么作用
2020/06/17 Python
用HTML5实现手机摇一摇的功能的教程
2012/10/30 HTML / CSS
MATCHESFASHION澳大利亚/亚太地区:英国时尚奢侈品电商
2020/01/14 全球购物
某公司Java工程师面试题笔试题
2016/03/27 面试题
商务英语应届生自我鉴定
2013/12/08 职场文书
模具专业毕业生自荐书范文
2014/02/19 职场文书
python实现调用摄像头并拍照发邮箱
2021/04/27 Python
Android studio 简单计算器的编写
2022/05/20 Java/Android