import numpy as np
X = np.array([[0.5, 0.8], [-1, -1]])
label = np.array([1, 0])
w1 = 1
w2 = -1
b = 0
ita = 0.1
Classify = False
count = 0
def perc_cal(x, w1, w2, b):
s = w1 * x[0] + w2 * x[1] + b
if s>=0:
y=1
else:
y=0
return y
while (Classify == False):
count = count + 1
print('\ncount:', count)
Classify = True
for i in range(X.shape[0]):
x = X[i]
pred_label = perc_cal(x, w1, w2, b) #调用上面def函数
if pred_label != label[i]:
Classify = False
w1 = w1 + ita * (label[i] - pred_label) * x[0]
w2 = w2 + ita * (label[i] - pred_label) * x[1]
b = b + ita * (label[i] - pred_label)
print('w1:', w1 )
print('w2:', w2)
print('b:', b)
x = X[0]
print('apple:', perc_cal(x, w1, w2, b))
x = X[1]
print('banana:', perc_cal(x, w1, w2, b))