import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
For Linear Separable Problem
X = np.array([[1,0,0],
[1,0,1],
[1,1,0],
[1,1,1]])
Y = np.array([-1,1,1,1])
W = np.random.random(3)
lr = 0.01
def update():
global X,Y,W,lr
A = np.dot(X,W)
W += lr*np.dot(Y-A,X)
for _ in range(1000):
update()
x1 = [0,1,1]
y1 = [1,0,1]
x2 = [0]
y2 = [0]
xdata = np.linspace(-0.5,1.5)
slope = -W[1]/W[2]
intercept = -W[0]/W[2]
plt.figure()
plt.plot(xdata, slope*xdata+intercept, 'k-')
plt.plot(x1,y1,'yo')
plt.plot(x2,y2,'go')
plt.show()
For Linear Inseparable Problem (XOR)
X = np.array([[1,0,0,0,0,0],
[1,0,1,0,0,1],
[1,1,0,1,0,0],
[1,1,1,1,1,1]])
Y = np.array([-1,1,1,-1])
W = np.random.random(6)
lr = 0.01
def update():
global X,Y,W,lr
A = np.dot(X,W)
W += lr*np.dot(Y-A,X)
for _ in range(1000):
update()
x1 = [0,1]
y1 = [1,0]
x2 = [0,1]
y2 = [0,1]
xdata = np.linspace(-0.5,1.5)
def get_root(W,x):
a = W[5]
b = W[2]+W[4]*x
c = W[0]+W[1]*x+W[3]*x*x
return ((-b+np.sqrt(b*b-4*a*c))/(2*a),(-b-np.sqrt(b*b-4*a*c))/(2*a))
plt.figure()
plt.plot(xdata, get_root(W,xdata)[0], 'k-')
plt.plot(xdata, get_root(W,xdata)[1], 'k-')
plt.plot(x1,y1,'yo')
plt.plot(x2,y2,'go')
plt.show()