详解如何用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实现RSA加密(解密)算法
Feb 17 Python
Python存取XML的常见方法实例分析
Mar 21 Python
python实现决策树分类算法
Dec 21 Python
Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容
Feb 23 Python
python: 判断tuple、list、dict是否为空的方法
Oct 22 Python
python Elasticsearch索引建立和数据的上传详解
Aug 04 Python
Python跑循环时内存泄露的解决方法
Jan 13 Python
django实现将后台model对象转换成json对象并传递给前端jquery
Mar 16 Python
学习python需要有编程基础吗
Jun 02 Python
详解使用python爬取抖音app视频(appium可以操控手机)
Jan 26 Python
python如何进行基准测试
Apr 26 Python
Python采集壁纸并实现炫轮播
Apr 30 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/03 咖啡文化
spl_autoload_register与autoload的区别详解
2013/06/03 PHP
解析用PHP实现var_export的详细介绍
2013/06/20 PHP
prototype.js的Ajax对象
2006/09/23 Javascript
基于jQuery实现表格数据的动态添加与统计的代码
2011/01/31 Javascript
Extjs中TabPane如何嵌套在其他网页中实现思路及代码
2013/01/27 Javascript
实例讲解JQuery中this和$(this)区别
2014/12/08 Javascript
jQuery超酷平面式时钟效果代码分享
2020/03/30 Javascript
js获取本机操作系统类型的两种方法
2015/12/19 Javascript
通过jquery实现页面的动画效果(实例代码)
2016/09/18 Javascript
Angular下H5上传图片的方法(可多张上传)
2017/01/09 Javascript
nodejs实现发出蜂鸣声音(系统报警声)的方法
2017/01/18 NodeJs
微信小程序 navbar实例详解
2017/05/11 Javascript
vue2.0全局组件之pdf详解
2017/06/26 Javascript
vue--点击当前增加class,其他删除class的方法
2018/09/15 Javascript
改进 JavaScript 和 Rust 的互操作性并深入认识 wasm-bindgen 组件
2019/07/13 Javascript
jquery实现简单自动轮播图效果
2020/07/29 jQuery
Vue插槽_特殊特性slot,slot-scope与指令v-slot说明
2020/09/04 Javascript
python+ffmpeg批量去视频开头的方法
2019/01/09 Python
详解Python 爬取13个旅游城市,告诉你五一大家最爱去哪玩?
2019/05/07 Python
python实现函数极小值
2019/07/10 Python
python gui开发——制作抖音无水印视频下载工具(附源码)
2021/02/07 Python
Matplotlib animation模块实现动态图
2021/02/25 Python
HTML5触摸事件演化tap事件介绍
2016/03/25 HTML / CSS
贝玲妃英国官网:Benefit英国
2018/02/03 全球购物
Urban Outfitters德国官网:美国跨国生活方式零售公司
2018/05/21 全球购物
Diptyque英国官方网站:源自法国的知名香氛品牌
2019/08/28 全球购物
Vivo俄罗斯官方在线商店:中国智能手机品牌
2019/10/04 全球购物
教师年终个人自我评价
2013/10/04 职场文书
老干部工作先进集体事迹材料
2014/05/21 职场文书
大学生村官考核材料
2014/05/23 职场文书
监督检查工作方案
2014/05/28 职场文书
校庆活动策划方案
2014/06/05 职场文书
毕业生实习期转正自我鉴定
2014/09/26 职场文书
热情服务标语
2014/10/07 职场文书
新西兰:最新留学学习计划书写作指南
2019/07/15 职场文书