Perceptron Learning Algorithm


In [1]:
import numpy as np

In [2]:
learning_rate = 0.2
iteration = 100

In [3]:
X = np.array([
        [1, 0, 0],
        [1, 1, 0],
        [1, 0, 1],
        [1, 1, 1]
    ])

In [4]:
t = np.array([0,1,1,1])

In [5]:
m, n = X.shape

In [6]:
y = lambda x: 1 if x > 0 else 0

In [7]:
errors = []
weights = np.random.rand(n).reshape(3,1).T
for i in range(iteration):
    random_row = np.random.choice(m)
    X_row = X[random_row].reshape(3,1)
    weights = weights + learning_rate * (t[random_row] - y(np.dot(weights, X_row))) * X[random_row]
    error = t[random_row] - y(np.dot(weights, X_row)) # cost function
    errors.append(error)

In [8]:
list(map(y, np.dot(X, weights.T)))


Out[8]:
[0, 1, 1, 1]

In [ ]: