data = [
[1,1,0],
[1,2,0],
[2,1,0],
[2,2,0],
[-1,-1,1],
[-1,-2,1],
[-2,-1,1],
[-2,-2,1]
]
weights = [0.0 for i in range(len(data[0]))]
def predict(data):
activation = weights[0]
for i in range(len(data)-1):
activation += weights[i+1] * float(data[i])
return 1.0 if activation >= 0.0 else 0.0
l_rate = 0.01
n_epoch = 3
for epoch in range(n_epoch):
sum_error = 0.0
for row in data:
prediction = predict(row)
error = row[-1] - prediction
sum_error += pow(error, 2)
weights[0] = weights[0] + l_rate * error
for i in range(len(row)-1):
weights[i+1] = weights[i+1] + l_rate * error * row[i]
print('>epoch=%d, lrate=%.3f, error=%.3f' % (epoch, l_rate, sum_error))
predict([-5,-2])