详解如何用Python实现感知器算法

 更新时间:2021年06月18日 16:56:31   作者:Julyers  
今天给大家带来的是关于Python的相关知识,文章围绕着如何用Python实现感知器算法展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下

一、题目

在这里插入图片描述

二、数学求解过程

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

该轮迭代分类结果全部正确,判别函数为g(x)=-2x1+1

三、感知器算法原理及步骤

在这里插入图片描述

四、python代码实现及结果

(1)由数学求解过程可知:

在这里插入图片描述

(2)程序运行结果

在这里插入图片描述

(3)绘图结果

在这里插入图片描述

'''
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 降级到3.6终极解决方案

    详解python 降级到3.6终极解决方案

    这篇文章主要介绍了详解python 降级到3.6终极解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • Python-ElasticSearch搜索查询的讲解

    Python-ElasticSearch搜索查询的讲解

    今天小编就为大家分享一篇关于Python-ElasticSearch搜索查询的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • 深入理解python函数递归和生成器

    深入理解python函数递归和生成器

    下面小编就为大家带来一篇深入理解python函数递归和生成器。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • OpenCV3.0+Python3.6实现特定颜色的物体追踪

    OpenCV3.0+Python3.6实现特定颜色的物体追踪

    这篇文章主要为大家详细介绍了OpenCV3.0+Python3.6实现特定颜色的物体追踪,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • Python3分析处理声音数据的例子

    Python3分析处理声音数据的例子

    今天小编就为大家分享一篇Python3分析处理声音数据的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Selenium中的option使用示例

    Selenium中的option使用示例

    这篇文章主要介绍了Selenium中的option用法实例,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • python机器学习案例教程——K最近邻算法的实现

    python机器学习案例教程——K最近邻算法的实现

    本篇文章主要介绍了python机器学习案例教程——K最近邻算法的实现,详细的介绍了K最近邻算法的概念和示例,具有一定的参考价值,有兴趣的可以了解一下
    2017-12-12
  • python3实现网络爬虫之BeautifulSoup使用详解

    python3实现网络爬虫之BeautifulSoup使用详解

    这篇文章主要介绍了python3实现网络爬虫之BeautifulSoup使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • Windows安装Python、pip、easy_install的方法

    Windows安装Python、pip、easy_install的方法

    这篇文章主要介绍了Windows安装Python、pip、easy_install的方法,需要的朋友可以参考下
    2017-03-03
  • Python 常用日期处理 -- calendar 与 dateutil 模块的使用

    Python 常用日期处理 -- calendar 与 dateutil 模块的使用

    这篇文章主要介绍了Python如何使用calendar 与 dateutil 模块处理日期,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下
    2020-09-09

最新评论