详解如何用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 相关文章推荐
haskell实现多线程服务器实例代码
Nov 26 Python
python超简单解决约瑟夫环问题
May 12 Python
python实现BackPropagation算法
Dec 14 Python
python编写弹球游戏的实现代码
Mar 12 Python
对numpy中数组元素的统一赋值实例
Apr 04 Python
Django使用Jinja2模板引擎的示例代码
Aug 09 Python
djano一对一、多对多、分页实例代码
Aug 16 Python
python3 Scrapy爬虫框架ip代理配置的方法
Jan 17 Python
python GUI库图形界面开发之PyQt5不规则窗口实现与显示GIF动画的详细方法与实例
Mar 09 Python
Vs Code中8个好用的python 扩展插件
Oct 12 Python
python调用百度API实现人脸识别
Nov 17 Python
python元组打包和解包过程详解
Aug 02 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 缓存函数代码
2008/08/27 PHP
初学CAKEPHP 基础教程
2009/11/02 PHP
PHP CURL模拟GET及POST函数代码
2010/04/25 PHP
PHP中冒号、endif、endwhile、endfor使用介绍
2010/04/28 PHP
php把数据表导出为Excel表的最简单、最快的方法(不用插件)
2014/05/10 PHP
PHP使用Face++接口开发微信公众平台人脸识别系统的方法
2015/04/17 PHP
日常整理PHP中简单的图形处理(经典)
2015/10/26 PHP
类似CSDN图片切换效果脚本
2009/09/17 Javascript
23个Javascript弹出窗口特效整理
2011/02/25 Javascript
JS Jquery 遍历,筛选页面元素 自动完成(实现代码)
2013/07/08 Javascript
jQuery实现渐变下拉菜单的简单方法
2015/03/11 Javascript
Bootstrap 粘页脚效果
2016/03/28 Javascript
AngularJS创建自定义指令的方法详解
2016/11/03 Javascript
通过bootstrap全面学习less
2016/11/09 Javascript
Vue 刷新当前路由的实现代码
2019/09/26 Javascript
详解Angular Karma测试的持续集成实践
2019/11/15 Javascript
Echarts实现单条折线可拖拽效果
2019/12/19 Javascript
详解element-ui动态限定的日期范围选择器代码片段
2020/07/03 Javascript
DWR内存兼容及无法调用问题解决方案
2020/10/16 Javascript
VUE+Element实现增删改查的示例源码
2020/11/23 Vue.js
Python写的Tkinter程序屏幕居中方法
2015/03/10 Python
对python 命令的-u参数详解
2018/12/03 Python
django的ORM操作 增加和查询
2019/07/26 Python
python实现提取str字符串/json中多级目录下的某个值
2020/02/27 Python
Python实现密钥密码(加解密)实例详解
2020/04/26 Python
html5唤醒APP小记
2019/03/27 HTML / CSS
html5移动端价格输入键盘的实现
2019/09/16 HTML / CSS
苹果美国官方商城:Apple美国
2016/08/24 全球购物
测量实习生自我鉴定
2013/09/19 职场文书
就业协议书样本
2014/08/20 职场文书
物理分数没达标检讨书
2014/09/13 职场文书
2016年六一儿童节开幕词
2016/03/04 职场文书
八年级地理课件资料及考点知识分享
2019/08/30 职场文书
有关信念的名言语录集锦
2019/12/06 职场文书
python用海龟绘图写贪吃蛇游戏
2021/06/18 Python
Mysql中一千万条数据怎么快速查询
2021/12/06 MySQL