详解如何用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怎么学好python?
Oct 07 Python
100行python代码实现跳一跳辅助程序
Jan 15 Python
zookeeper python接口实例详解
Jan 18 Python
python如何重载模块实例解析
Jan 25 Python
django 发送手机验证码的示例代码
Apr 25 Python
Python实现修改IE注册表功能示例
May 10 Python
Python实战购物车项目的实现参考
Feb 20 Python
Python3.6.x中内置函数总结及讲解
Feb 22 Python
详解Python列表赋值复制深拷贝及5种浅拷贝
May 15 Python
浅谈Pycharm最有必要改的几个默认设置项
Feb 14 Python
python爬虫使用requests发送post请求示例详解
Aug 05 Python
PyTorch 如何自动计算梯度
May 23 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中判断变量为空的几种方法小结
2013/11/12 PHP
PHP实现的策略模式简单示例
2017/08/25 PHP
js 上传图片预览问题
2010/12/06 Javascript
ajax与302响应代码测试
2013/10/23 Javascript
NodeJS制作爬虫全过程
2014/12/22 NodeJs
JavaScript学习笔记之JS函数
2015/01/22 Javascript
JavaScript实现在页面间传值的方法
2015/04/07 Javascript
jQuery检测返回值的数据类型
2015/07/13 Javascript
js滑动提示效果代码分享
2016/03/10 Javascript
vue.js指令v-model实现方法
2016/12/05 Javascript
微信小程序页面传值实例分析
2017/04/19 Javascript
JavaScript实现换肤功能
2017/09/15 Javascript
Bootstrap标签页(Tab)插件切换echarts不显示问题的解决
2018/07/13 Javascript
vue router 源码概览案例分析
2018/10/09 Javascript
实例分析Array.from(arr)与[...arr]到底有何不同
2019/04/09 Javascript
nodemon实现Typescript项目热更新的示例代码
2019/11/19 Javascript
ES6 十大特性简介
2020/12/09 Javascript
[02:37]TI8勇士令状不朽珍藏II视频展示
2018/06/23 DOTA
Python数组条件过滤filter函数使用示例
2014/07/22 Python
跟老齐学Python之Python文档
2014/10/10 Python
python 3.7.0 下pillow安装方法
2018/08/27 Python
Python sklearn KFold 生成交叉验证数据集的方法
2018/12/11 Python
Django之全局使用request.user.username的实例详解
2020/05/14 Python
Django微信小程序后台开发教程的实现
2020/06/03 Python
10个示例带你掌握python中的元组
2020/11/23 Python
全球速卖通巴西站点:Aliexpress巴西
2016/08/24 全球购物
漫威玩具服装及周边商品官方购物网站:Marvel Shop
2019/05/11 全球购物
英国在线玫瑰专家:InterRose
2019/12/01 全球购物
Nike瑞士官网:Nike CH
2021/01/18 全球购物
WEB控件及HTML服务端控件能否调用客户端方法?如果能,请解释如何调用?
2015/08/25 面试题
入党自我鉴定范文
2013/10/04 职场文书
ktv中秋节活动方案
2014/01/30 职场文书
2014年师德承诺书
2014/05/23 职场文书
素质教育标语
2014/06/27 职场文书
党的群众路线教育实践活动总结
2014/10/30 职场文书
2014年人事工作总结范文
2014/11/19 职场文书