Perceptron

Data

In [1]:
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

In [2]:
weights = [0.0 for i in range(len(data[0]))]

Predict Function

In [3]:
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

Training

In [4]:
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))
>epoch=0, lrate=0.010, error=1.000
>epoch=1, lrate=0.010, error=0.000
>epoch=2, lrate=0.010, error=0.000
In [5]:
predict([-5,-2])
Out[5]:
1.0