from numpy import *
d = 2
n = 500
x = random.rand(d,n)
x
X = empty((d+1,n))
X[0,:] = 1
X[1:,:] = x
X
W = array([-1,-2,3.])
y = sign(dot(W,X))
y
# let's open up a little space between the classes
halfgap = 0.05
x[-1,][y>0] += halfgap
x[-1,][y<0] -= halfgap
# copy the changes into X
X[1:,:] = x
%pylab inline
xp = x.T[y>0].T
xm = x.T[y<0].T
subplot(111,aspect=1)
plot(xp[0],xp[1],'ro')
plot(xm[0],xm[1],'bo');
Our task is to use the PLA to find a separating W
# We are given X and y ONLY!!
W = array([-1,-1,5.]) # starting guess at separating W
while(True):
if sum(sign(dot(W,X))==y)==n: # correctly classified
break
# index i of misclassified point !!!!!!!!!!!!
W += y[i]*X[:,i]