婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 詳解如何用Python實現感知器算法

詳解如何用Python實現感知器算法

熱門標簽:百度地圖標注位置怎么修改 老人電話機器人 地圖標注視頻廣告 梅州外呼業務系統 北京電信外呼系統靠譜嗎 高德地圖標注是免費的嗎 無錫客服外呼系統一般多少錢 洪澤縣地圖標注 大連crm外呼系統

一、題目

二、數學求解過程



該輪迭代分類結果全部正確,判別函數為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 實踐感知器分類算法
  • python實現感知器算法(批處理)
  • python實現神經網絡感知器算法
  • python實現感知器算法詳解

標簽:安慶 長春 吉林 清遠 岳陽 怒江 洛陽 泉州

巨人網絡通訊聲明:本文標題《詳解如何用Python實現感知器算法》,本文關鍵詞  詳解,如,何用,Python,實現,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《詳解如何用Python實現感知器算法》相關的同類信息!
  • 本頁收集關于詳解如何用Python實現感知器算法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 怀柔区| 宜黄县| 郁南县| 金山区| 北辰区| 乌恰县| 天津市| 镇宁| 城固县| 岳普湖县| 石阡县| 泉州市| 思南县| 鹤岗市| 临洮县| 潮安县| 文成县| 曲松县| 鄂伦春自治旗| 耿马| 藁城市| 老河口市| 雅江县| 庆阳市| 泸溪县| 九龙城区| 敦煌市| 称多县| 湖南省| 霞浦县| 和政县| 田阳县| 锡林郭勒盟| 达尔| 丹凤县| 灌南县| 遵义县| 高淳县| 七台河市| 开江县| 东兴市|