【发布时间】:2017-03-15 07:02:23
【问题描述】:
对于某些输入X,例如:
[[ 1.456044 -7.058824]
[-4.478022 -2.072829]
[-7.664835 -6.890756]
[-5.137363 2.352941]
...
还有Y,例如:
[ 1. 1. 1. -1. ...
这是我的感知器训练函数:
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
当我打印错误时,我通常会看到如下内容:
[28, 12, 10, 7, 10, 8, 11, 8, 0]
请注意,我的错误为 0,但数据明显存在偏差:
例如,这里是 b 运行一次:
-28.6778508366
我查看了this SO,但没有发现我们的算法有什么不同。我想也许这就是我解释然后绘制w 和b 的方式?我只是做一些非常简单的事情:
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')
...
【问题讨论】:
标签: python algorithm machine-learning perceptron