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

2022-09-25 10:36:09 字數 3369 閱讀 9831

目錄

該輪迭代分類結果全部正確,判別函式為g(x)=-2x1+1

(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='clas')

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, updat

python實現多型 如何用python實現多型性

建立乙個名為 func 的函式,它將獲取乙個我們將其命名為 obj 的物件。雖然我們使用的名稱是 obj 但是任何例項化的物件都可以被呼叫到這個函式中。用函式實現多型性 class india def capital self print new delhi is the capital of in...

如何用python實現數字轉漢字?

在大多數情況下,漢字都比數字要顯得正式一些。比如說,二零一九年 就比較有官方檔案的味道,而 2019年 則更有個人日記的味道。另外,漢字還可以用來編繞口令!用數字的話就難得多。還是舉個栗子 14是14,40是40。就顯得很 十四是十四,四十是四十。就有一種唇齒生香的感覺。山前有44只石獅子,山後有4...

python實現感知機

import numpy as np 定義啟用函式 def acti fun x return 1 if x 0 else 0 建立感知器類 class perception object 初始化權重 def init self self.weights 0 self.bias 1 定義訓練函式,包...