一、实验代码:
import numpy as np
def pre(x1, y1):# 定义数据集,权重,偏置项result=[]lr = 0.1b = 0.4W = np.array([0.5,0.5])for i in range(len(y1)):if y1[i] * (np.dot(W, x1[i]) + b) <= 0:W = W + lr * y1[i] * x1[i] b = b + lr * y1[i] for j in range(len(x1)):countresult = np.sign(np.dot(W, x1[j]) + b)result.append(countresult)print(f"weight:{W},b:{b},result:{result}")
# and
def AND():x = np.array([[-1, -1], [-1, 1], [1, -1], [1, 1]]) # -1为假 1为真y = np.array([-1, -1, -1, 1])return [x, y]
def OR():x = np.array([[-1, -1], [-1, 1], [1, -1], [1, 1]]) # -1为假 1为真y = np.array([-1, 1, 1, 1])return [x, y]
pre(AND()[0], AND()[1])
pre(OR()[0], OR()[1])
二、运行截图:![]()