0

我正在尝试通过制作一条线(感知器)f并使一侧的点+1和另一侧的-1来制作一组数据点。然后制作一条新线 g 并尝试通过更新 w = w+ y(t)x(t) 使其尽可能接近 f,其中 w 是权重,y(t) 是 +1,-1 和 x(t ) 是错误分类点的坐标。实施此操作后,我并没有从 g 到 f 非常合适。这是我的代码和一些示例输出。

import random

random.seed()

points = [ [1, random.randint(-25, 25), random.randint(-25,25), 0] for k in range(1000)]

weights = [.1,.1,.1]

misclassified = []

############################################################# Function f

interceptf = (0,random.randint(-5,5))

slopef = (random.randint(-10, 10),random.randint(-10,10))

point1f = ((interceptf[0] + slopef[0]),(interceptf[1] + slopef[1]))
point2f = ((interceptf[0] - slopef[0]),(interceptf[1] - slopef[1]))

############################################################# Function G starting
interceptg = (-weights[0],weights[2])

slopeg = (-weights[1],weights[2])

point1g = ((interceptg[0] + slopeg[0]),(interceptg[1] + slopeg[1]))
point2g = ((interceptg[0] - slopeg[0]),(interceptg[1] - slopeg[1]))
#############################################################

def isLeft(a, b, c):
      return ((b[0] - a[0])*(c[1] - a[1]) - (b[1] - a[1])*(c[0] - a[0])) > 0


for i in points:
    if isLeft(point1f,point2f,i):
        i[3]=1
    else:
        i[3]=-1

for i in points:
    if (isLeft(point1g,point2g,i)) and (i[3] == -1):
        misclassified.append(i)

    if (not isLeft(point1g,point2g,i)) and (i[3] == 1):
        misclassified.append(i)

print len(misclassified)


while misclassified:
    first = misclassified[0]
    misclassified.pop(0)

    a = [first[0],first[1],first[2]]
    b = first[3]

    a[:] = [x*b for x in a]

    weights = [(x + y) for x, y in zip(weights,a)]

interceptg = (-weights[0],weights[2])

slopeg = (-weights[1],weights[2])

point1g = ((interceptg[0] + slopeg[0]),(interceptg[1] + slopeg[1]))
point2g = ((interceptg[0] - slopeg[0]),(interceptg[1] - slopeg[1]))

check = 0

for i in points:
    if (isLeft(point1g,point2g,i)) and (i[3] == -1):
        check += 1

    if (not isLeft(point1g,point2g,i)) and (i[3] == 1):
        check += 1

print weights
print check

117 <--- 带有 g 的原始错误分类数

[-116.9, -300.9, 190.1] <--- 最终权重

617 <--- 算法后带有 g 的原始错误分类数

956 <--- 带有 g 的原始错误分类数

[-33.9, -12769.9, -572.9] <--- 最终权重

461 <--- 算法后带有 g 的原始错误分类数

4

1 回答 1

0

你的算法至少有几个问题:

  • 你的“while”条件是错误的——感知器学习不是像你现在那样遍历所有错误分类的点。只要其中任何一个被错误分类,算法就应该遍历所有点。特别是 - 每次更新都可以将一些正确分类的点作为错误的点,因此您必须始终遍历所有这些点并检查一切是否正常。

  • 我很确定您真正想要的是更新规则,其形式为预测标签的(y(i)-p(i))x(i)位置,并且是真实标签(但如果您只更新错误分类,这显然会退化为您的方法)p(i)y(i)

于 2013-09-04T06:11:08.360 回答