import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Perceptron learning algorithm
ML
= np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
X = np.array([0, 0, 0, 1])
y_and = np.array([0, 1, 1, 1])
y_or = np.array([0, 1, 1, 0]) y_xor
class Perceptron(object):
def __init__(self, lr=0.01, iterations=100):
self.lr = lr
self.iterations = iterations
def activation(self, z):
= np.zeros_like(z)
ac >0] = 1
ac[zreturn ac
def fit(self, X, y):
= np.append(np.ones((len(X), 1)), X, axis=1)
X_with_one self.W = np.zeros((X_with_one.shape[1], 1))
for i in range(self.iterations):
for j in range(len(X)):
= (X_with_one@self.W).flatten()
summation = self.activation(summation)
y_hat = y - y_hat.flatten()
err self.W = self.W + (self.lr*err[j]*X_with_one[j]).reshape(*(self.W.shape))
def predict(self, X):
= np.append(np.ones((len(X), 1)), X, axis=1)
X_with_one = (X_with_one@self.W).flatten()
summation = self.activation(summation)
y_hat return y_hat
= Perceptron() perceptron
perceptron.fit(X, y_or)
perceptron.W
array([[0. ],
[0.01],
[0.01]])
perceptron.predict(X)
array([0., 1., 1., 1.])
perceptron.fit(X, y_and)
perceptron.W
array([[-0.02],
[ 0.02],
[ 0.01]])
perceptron.predict(X)
array([0., 0., 0., 1.])
perceptron.fit(X, y_xor)
perceptron.W
array([[ 0.01],
[-0.01],
[ 0. ]])
perceptron.predict(X)
array([1., 1., 0., 0.])
XOR using feature transformation
# Transformation: 1
# x1, x2, x1x2
= np.append(X, (X[:, 0]*X[:, 1]).reshape(-1, 1), axis=1) X_xor_1
= Perceptron() perceptron
perceptron.fit(X_xor_1, y_xor)
perceptron.W
array([[ 0. ],
[ 0.01],
[ 0.01],
[-0.04]])
np.allclose(perceptron.predict(X_xor_1), y_xor)
True
0]*X[:, 1]).reshape(-1, 1) (X[:,
array([[0],
[0],
[0],
[1]])
# Transformation: 1
# x1, x2, x1x2
= np.array([(1-X[:, 0])*X[:,1], (1-X[:, 1])*X[:,0]]).T X_xor_2
= Perceptron()
perceptron perceptron.fit(X_xor_2, y_xor)
perceptron.W
array([[0. ],
[0.01],
[0.01]])