详解如何用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实现微信模板消息
Dec 21 Python
python字符串连接方法分析
Apr 12 Python
TensorFlow实现Softmax回归模型
Mar 09 Python
Python读取properties配置文件操作示例
Mar 29 Python
python TKinter获取文本框内容的方法
Oct 11 Python
在Python中构建增广矩阵的实现方法
Jul 01 Python
使用django实现一个代码发布系统
Jul 18 Python
Python collections模块使用方法详解
Aug 28 Python
pip install python 快速安装模块的教程图解
Oct 08 Python
Python使用正则实现计算字符串算式
Dec 29 Python
基于Python和C++实现删除链表的节点
Jul 06 Python
Python通过递归函数输出嵌套列表元素
Oct 15 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
世界第一个无线广播电台 KDKA
2021/03/01 无线电
php基础知识:类与对象(4) 范围解析操作符(::)
2006/12/13 PHP
10个可以简化php开发过程的MySQL工具
2010/04/11 PHP
PHP根据IP判断地区名信息的示例代码
2014/03/03 PHP
phpmyadmin打开很慢的解决方法
2014/04/21 PHP
php数组冒泡排序算法实例
2016/05/06 PHP
Thinkphp3.2.3整合phpqrcode生成带logo的二维码
2016/07/21 PHP
PHP单态模式简单用法示例
2016/11/16 PHP
php自定义扩展名获取函数示例
2016/12/12 PHP
JS验证控制输入中英文字节长度(input、textarea等)具体实例
2013/06/21 Javascript
js获取本机的外网/广域网ip地址完整源码
2013/08/12 Javascript
Jquery之Bind方法参数传递与接收的三种方法
2014/06/24 Javascript
详解JavaScript中Date.UTC()方法的使用
2015/06/12 Javascript
js 颜色选择插件
2017/01/23 Javascript
如何通过非数字与字符的方式实现PHP WebShell详解
2017/07/02 Javascript
Angular5给组件本身的标签添加样式class的方法
2018/04/07 Javascript
创建echart多个联动的示例代码
2018/11/23 Javascript
jquery.pager.js实现分页效果
2019/07/29 jQuery
微信小程序实现图片翻转效果的实例代码
2019/09/20 Javascript
es6函数之箭头函数用法实例详解
2020/04/25 Javascript
JavaScript Date对象功能与用法学习记录
2020/04/28 Javascript
Antd-vue Table组件添加Click事件,实现点击某行数据教程
2020/11/17 Javascript
python批量图片处理简单示例
2019/08/06 Python
python列表返回重复数据的下标
2020/02/10 Python
纯CSS实现菜单、导航栏的3D翻转动画效果
2014/04/23 HTML / CSS
数据库什么时候应该被重组
2012/11/02 面试题
采购主管的岗位职责
2013/12/17 职场文书
高一地理教学反思
2014/01/18 职场文书
库房保管员岗位职责
2014/04/07 职场文书
质量管理标语
2014/06/12 职场文书
小学生植树节活动总结
2014/07/04 职场文书
2015年测量员工作总结
2015/05/23 职场文书
2015年学校管理工作总结
2015/07/20 职场文书
Python一行代码实现自动发邮件功能
2021/05/30 Python
MySQL如何解决幻读问题
2021/08/07 MySQL
Go语言怎么使用变长参数函数
2022/07/15 Golang