For some input X
, e.g.:
[[ 1.456044 -7.058824]
[-4.478022 -2.072829]
[-7.664835 -6.890756]
[-5.137363 2.352941]
...
And Y
, e.g.:
[ 1. 1. 1. -1. ...
Here is my perceptron training function:
def train(self, X, Y, iterations=1000):
# Add biases to every sample.
biases = np.ones(X.shape[0])
X = np.vstack((biases, X.T)).T
w = np.random.randn(X.shape[1])
errors = []
for _ in range(iterations):
all_corr = True
num_err = 0
for x, y in zip(X, Y):
correct = np.dot(w, x) * y > 0
if not correct:
num_err += 1
all_corr = False
w += y * x
errors.append(num_err)
# Exit early if all samples are correctly classified.
if all_corr:
break
self.w = perpendicular(w[1:])
self.b = w[0]
return self.w, self.b, errors
When I print the errors, I typically see something like:
[28, 12, 10, 7, 10, 8, 11, 8, 0]
Notice that I am getting 0 error, but the data is clearly off by some bias:
For example, here is b
for one run:
-28.6778508366
I have looked at this SO but do not see a difference in our algorithm. I think maybe it is how I am interpreting and then plotting w
and b
? I just do something very simple:
def plot(X, Y, w, b):
area = 20
fig = plt.figure()
ax = fig.add_subplot(111)
p = X[Y == 1]
n = X[Y == -1]
ax.scatter(p[:, 0], p[:, 1], s=area, c='r', marker="o", label='pos')
ax.scatter(n[:, 0], n[:, 1], s=area, c='b', marker="s", label='neg')
neg_w = -w
xs = [neg_w[0], w[0]]
ys = [neg_w[1], w[1]] # My guess is that this is where the bias goes?
ax.plot(xs, ys, 'r--', label='hyperplane')
...