詳解如何用Python實(shí)現(xiàn)感知器算法
一、題目
二、數(shù)學(xué)求解過(guò)程
該輪迭代分類(lèi)結(jié)果全部正確,判別函數(shù)為g(x)=-2x1+1
三、感知器算法原理及步驟
四、python代碼實(shí)現(xiàn)及結(jié)果
(1)由數(shù)學(xué)求解過(guò)程可知:
(2)程序運(yùn)行結(jié)果
(3)繪圖結(jié)果
''' 20210610 Julyer 感知器 ''' import numpy as np import matplotlib.pyplot as plt def get_zgxl(xn, a): ''' 獲取增廣向量 :param x: 數(shù)組 :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和初始值,計(jì)算w :param w: 列向量 --> wT:行向量 :param xn: 列向量 :return: ''' # wT = w.reshape(1, -1) # 列向量轉(zhuǎn)變?yōu)樾邢蛄?,改變w wT = w.T # 列向量轉(zhuǎn)變?yōu)樾邢蛄浚桓淖僿 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為一個(gè)數(shù) def fit_one(w1, x1, x2, x3, x4): ''' 完成一輪迭代,遍歷一次數(shù)據(jù),更新到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計(jì)算樣本個(gè)數(shù),通過(guò)循環(huán)更新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('迭代次數(shù)為:' + str(iter_number)) print('迭代終止時(shí)的w:'+'\n' + str(update_w)) #print(wTx_list) draw_plot(class1, class2, update_w)
到此這篇關(guān)于詳解如何用Python實(shí)現(xiàn)感知器算法的文章就介紹到這了,更多相關(guān)Python實(shí)現(xiàn)感知器算法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python subprocess模塊常見(jiàn)用法分析
這篇文章主要介紹了Python subprocess模塊常見(jiàn)用法,結(jié)合實(shí)例形式分析了subprocess模塊進(jìn)程操作相關(guān)使用技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06一篇文章帶你了解python標(biāo)準(zhǔn)庫(kù)--random模塊
這篇文章主要給大家介紹了關(guān)于Python中random模塊常用方法的使用教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08簡(jiǎn)單聊聊Python中的鴨子類(lèi)型和猴子補(bǔ)丁
不知不覺(jué)使用python寫(xiě)代碼已經(jīng)很長(zhǎng)時(shí)間了,下面這篇文章主要給大家介紹了關(guān)于python鴨子類(lèi)型(duck?type)和猴子補(bǔ)丁(mokey?patching)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04Python生成指定數(shù)量的優(yōu)惠碼實(shí)操內(nèi)容
在本篇文章里小編給大家整理了關(guān)于Python生成指定數(shù)量的優(yōu)惠碼的實(shí)例內(nèi)容以及相關(guān)代碼,有需要的朋友們學(xué)習(xí)下。2019-06-06利用Python實(shí)現(xiàn)Json序列化庫(kù)的方法步驟
這篇文章主要給大家介紹了關(guān)于利用Python實(shí)現(xiàn)Json序列化庫(kù)的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python使用Pillow將照片轉(zhuǎn)換為1寸報(bào)名照片的教程分享
在現(xiàn)代科技時(shí)代,我們經(jīng)常需要調(diào)整和處理照片以適應(yīng)特定的需求和用途,本文將介紹如何使用wxPython和Pillow庫(kù),通過(guò)一個(gè)簡(jiǎn)單的圖形界面程序,將選擇的照片轉(zhuǎn)換為指定尺寸的JPG格式,并保存在桌面上,需要的朋友可以參考下2023-09-09