详解如何用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的自动化部署模块Fabric的安装及使用指南
Jan 19 Python
Python自定义主从分布式架构实例分析
Sep 19 Python
Python实现脚本锁功能(同时只能执行一个脚本)
May 10 Python
python和shell监控linux服务器的详细代码
Jun 22 Python
Python3 单行多行万能正则匹配方法
Jan 07 Python
Python 单例设计模式用法实例分析
Sep 23 Python
判断Threading.start新线程是否执行完毕的实例
May 02 Python
基于plt.title无法显示中文的快速解决
May 16 Python
Python如何将装饰器定义为类
Jul 30 Python
Python结合Window计划任务监测邮件的示例代码
Aug 05 Python
最新PyCharm 2020.2.3永久激活码(亲测有效)
Nov 26 Python
利用Python判断整数是否是回文数的3种方法总结
Jul 07 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
PHP识别二维码的方法(php-zbarcode安装与使用)
2016/07/07 PHP
PHP基于mcript扩展实现对称加密功能示例
2019/02/21 PHP
TP5(thinkPHP5框架)基于bootstrap实现的单图上传插件用法示例
2019/05/29 PHP
Laravel 前端资源配置教程
2019/10/18 PHP
Javascript中的var_dump函数实现代码
2009/09/07 Javascript
url参数中有+、空格、=、%、&amp;、#等特殊符号的问题解决
2013/05/15 Javascript
弹出最简单的模式化遮罩层的js代码
2013/12/04 Javascript
javascript实现uploadify上传格式以及个数限制
2015/11/23 Javascript
正则表达式基本语法及表单验证操作详解【基于JS】
2017/04/07 Javascript
jQuery zTree树插件动态加载实例代码
2017/05/11 jQuery
JavaScript基于扩展String实现替换字符串中index处字符的方法
2017/06/13 Javascript
详解如何在vue中使用sass
2017/06/21 Javascript
JavaScript数据结构之优先队列与循环队列实例详解
2017/10/27 Javascript
详解ES6通过WeakMap解决内存泄漏问题
2018/03/09 Javascript
vue自动化表单实例分析
2018/05/06 Javascript
详解javascript函数写法大全
2019/03/25 Javascript
vue动态加载SVG文件并修改节点数据的操作代码
2020/08/17 Javascript
Python实现的HTTP并发测试完整示例
2020/04/23 Python
Anaconda多环境多版本python配置操作方法
2017/09/12 Python
Django添加KindEditor富文本编辑器的使用
2018/10/24 Python
TensorFlow车牌识别完整版代码(含车牌数据集)
2019/08/05 Python
深入浅析Python 中的sklearn模型选择
2019/10/12 Python
python3实现用turtle模块画一棵随机樱花树
2019/11/21 Python
Tensorflow tf.dynamic_partition矩阵拆分示例(Python3)
2020/02/07 Python
html5使用canvas实现跟随光标跳动的火焰效果
2014/01/07 HTML / CSS
BONIA官方网站:国际奢侈品牌和皮革专家
2016/11/27 全球购物
新加坡领先的在线生活方式和杂货购物网站:EAMART
2019/04/02 全球购物
澳大利亚运动鞋商店:Platypus Shoes
2019/09/27 全球购物
.NET方向面试题
2014/11/20 面试题
写clone()方法时,通常都有一行代码,是什么?
2012/10/31 面试题
班级入场式解说词
2014/02/01 职场文书
党组织领导班子整改方案
2014/10/25 职场文书
2015年幼儿园卫生保健工作总结
2015/05/12 职场文书
学校工会工作总结2015
2015/05/19 职场文书
创业计划书之美甲店
2019/09/20 职场文书
Golang使用Panic与Recover进行错误捕获
2022/03/22 Golang