详解如何用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中每次处理一个字符的5种方法
May 21 Python
Python实现简单的获取图片爬虫功能示例
Jul 12 Python
django用户登录和注销的实现方法
Jul 16 Python
树莓派实现移动拍照
Jun 22 Python
linux下安装python3和对应的pip环境教程详解
Jul 01 Python
python字典嵌套字典的情况下找到某个key的value详解
Jul 10 Python
Python操作Sonqube API获取检测结果并打印过程解析
Nov 27 Python
pandas数据选取:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]
Apr 24 Python
Pycharm 2020.1 版配置优化的详细教程
Aug 07 Python
Python列表嵌套常见坑点及解决方案
Sep 30 Python
只用20行Python代码实现屏幕录制功能
Jun 02 Python
Python+Matplotlib+LaTeX玩转数学公式
Feb 24 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
phpcms中的评论样式修改方法
2016/10/21 PHP
自制PHP框架之模型与数据库
2017/05/07 PHP
jquery插件制作 手风琴Panel效果实现
2012/08/17 Javascript
一个JavaScript防止表单重复提交的实例
2014/10/21 Javascript
用原生JS对AJAX做简单封装的实例代码
2016/07/13 Javascript
JavaScript实现大图轮播效果
2017/01/11 Javascript
vue v-on监听事件详解
2017/05/17 Javascript
详解Angular 中 ngOnInit 和 constructor 使用场景
2017/06/22 Javascript
js 倒计时(高效率服务器时间同步)
2017/09/12 Javascript
angular json对象push到数组中的方法
2018/02/27 Javascript
nodejs简单抓包工具使用详解
2019/08/23 NodeJs
JS Web Flex弹性盒子模型代码实例
2020/03/10 Javascript
IDEA配置jQuery, $符号不再显示黄色波浪线的问题
2020/10/09 jQuery
前端vue如何使用高德地图
2020/11/05 Javascript
浅谈Vue static 静态资源路径 和 style问题
2020/11/07 Javascript
vue3中轻松实现switch功能组件的全过程
2021/01/07 Vue.js
python魔法方法-自定义序列详解
2016/07/21 Python
python递归全排列实现方法
2018/08/18 Python
解决pycharm运行出错,代码正确结果不显示的问题
2018/11/30 Python
Python实现CNN的多通道输入实例
2020/01/17 Python
浅谈pymysql查询语句中带有in时传递参数的问题
2020/06/05 Python
python zip()函数的使用示例
2020/09/23 Python
如果NULL定义成#define NULL((char *)0)难道不就可以向函数传入不加转换的NULL了吗
2012/02/15 面试题
村干部承诺书
2014/03/28 职场文书
网站客服岗位职责
2014/04/05 职场文书
大学生精神文明先进个人事迹材料
2014/05/02 职场文书
招股说明书范本
2014/05/06 职场文书
活动总结模板
2014/05/09 职场文书
无毒社区工作方案
2014/05/23 职场文书
餐厅保洁员岗位职责
2015/04/10 职场文书
离婚承诺书格式范文
2015/05/04 职场文书
2016年小学生清明节广播稿
2015/12/17 职场文书
教育教学工作反思
2016/02/24 职场文书
HTML+VUE分页实现炫酷物联网大屏功能
2021/05/27 Vue.js
电频谱管理的原则是什么
2022/02/18 无线电
TV动画《史上最强大魔王转生为村民A》番宣CM公布
2022/04/01 日漫